Skip to content

Commit fe0e24a

Browse files
authoredDec 23, 2020
chore(build): unify client bundling scripts (#3600)
Remove bundling logic from Grunt and introduce watch mode into the scripts/client.js. New script (`npm run build:watch`) allows to watch for the changes in client sources and bundle them automatically. This also fixes a bug where `npm run build` could swallow errors and silently do nothing. Fixes #3599
1 parent 1a65bf1 commit fe0e24a

File tree

5 files changed

+65
-409
lines changed

5 files changed

+65
-409
lines changed
 

‎docs/dev/02-making-changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Here are some tips on how to set up a Karma workspace and how to send a good pul
4949
- Build the client code via:
5050
```bash
5151
$ npm run build
52+
# or use the watch mode
53+
$ npm run build:watch
5254
```
5355

5456
## Changing the Code

‎gruntfile.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,10 @@ module.exports = function (grunt) {
99
context: ['context/**/*.js'],
1010
grunt: ['grunt.js', 'tasks/*.js']
1111
},
12-
browserify: {
13-
client: {
14-
files: {
15-
'static/karma.js': ['client/main.js'],
16-
'static/context.js': ['context/main.js']
17-
}
18-
}
19-
},
2012
test: {
2113
unit: 'mochaTest:unit',
2214
client: 'test/client/karma.conf.js'
2315
},
24-
watch: {
25-
client: {
26-
files: '<%= files.client %>',
27-
tasks: 'browserify:client'
28-
}
29-
},
3016
mochaTest: {
3117
options: {
3218
reporter: 'dot',
@@ -43,7 +29,6 @@ module.exports = function (grunt) {
4329
},
4430
'npm-publish': {
4531
options: {
46-
requires: ['build'],
4732
abortIfDirty: true,
4833
tag: 'latest'
4934
}
@@ -93,16 +78,14 @@ module.exports = function (grunt) {
9378
grunt.loadTasks('tasks')
9479
require('load-grunt-tasks')(grunt)
9580

96-
grunt.registerTask('build', ['browserify:client'])
97-
grunt.registerTask('default', ['build', 'test'])
81+
grunt.registerTask('default', ['test'])
9882
grunt.registerTask('test-appveyor', ['test:unit', 'test:client'])
9983

10084
grunt.registerTask('release', 'Build, bump and publish to NPM.', function (type) {
10185
grunt.task.run([
10286
'check_clean',
10387
'npm-contributors',
10488
'bump:' + (type || 'patch') + ':bump-only',
105-
'build',
10689
'conventionalChangelog',
10790
'bump-commit',
10891
'conventionalGithubReleaser',

‎package-lock.json

+31-387
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@
447447
"eslint-plugin-standard": "^4.0.1",
448448
"grunt": "^1.2.1",
449449
"grunt-auto-release": "^0.0.7",
450-
"grunt-browserify": "^5.0.0",
451450
"grunt-bump": "^0.8.0",
452451
"grunt-check-clean": "^0.1.2",
453452
"grunt-cli": "^1.1.0",
@@ -480,6 +479,7 @@
480479
"sinon-chai": "^3.5.0",
481480
"supertest": "^4.0.2",
482481
"timer-shim": "^0.3.0",
482+
"watchify": "^3.11.1",
483483
"which": "^1.3.1"
484484
},
485485
"main": "./lib/index",
@@ -506,6 +506,7 @@
506506
"test": "npm run test:unit && npm run test:e2e && npm run test:client",
507507
"build": "node scripts/client.js build",
508508
"build:check": "node scripts/client.js check",
509+
"build:watch": "node scripts/client.js watch",
509510
"test:appveyor": "grunt test-appveyor",
510511
"test:integration": "./scripts/integration-tests.sh",
511512
"link": "node --eval \"path=require('path'); require('fs').symlinkSync(path.resolve(__dirname), path.resolve(__dirname, 'node_modules', 'karma'), 'junction')\"",

‎scripts/client.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const browserify = require('browserify')
2-
const fs = require('fs')
2+
const watchify = require('watchify')
3+
const { createWriteStream } = require('fs')
34
const { readFile } = require('fs').promises
45

56
const bundleResourceToFile = (inPath, outPath) => {
67
return new Promise((resolve, reject) => {
78
browserify(inPath).bundle()
8-
.pipe(fs.createWriteStream(outPath))
9-
.once('finish', () => resolve())
109
.once('error', (e) => reject(e))
10+
.pipe(createWriteStream(outPath))
11+
.once('finish', () => resolve())
1112
})
1213
}
1314

@@ -24,6 +25,28 @@ const bundleResource = (inPath) => {
2425
})
2526
}
2627

28+
const watchResourceToFile = (inPath, outPath) => {
29+
const b = browserify({
30+
entries: [inPath],
31+
cache: {},
32+
packageCache: {},
33+
plugin: [watchify]
34+
})
35+
36+
const bundle = () => {
37+
b.bundle()
38+
.once('error', (e) => {
39+
console.error(`Failed to bundle ${inPath} into ${outPath}.`)
40+
console.error(e)
41+
})
42+
.pipe(createWriteStream(outPath))
43+
.once('finish', () => console.log(`Bundled ${inPath} into ${outPath}.`))
44+
}
45+
46+
b.on('update', bundle)
47+
bundle()
48+
}
49+
2750
const main = async () => {
2851
if (process.argv[2] === 'build') {
2952
await bundleResourceToFile('client/main.js', 'static/karma.js')
@@ -39,6 +62,9 @@ const main = async () => {
3962
// eslint-disable-next-line no-throw-literal
4063
throw 'Bundled client assets are outdated. Forgot to run "npm run build"?'
4164
}
65+
} else if (process.argv[2] === 'watch') {
66+
watchResourceToFile('client/main.js', 'static/karma.js')
67+
watchResourceToFile('context/main.js', 'static/context.js')
4268
} else {
4369
// eslint-disable-next-line no-throw-literal
4470
throw `Unknown command: ${process.argv[2]}`

0 commit comments

Comments
 (0)
Please sign in to comment.