js-build-tools 5.3.2 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build.js +63 -63
- package/dist/bundle.js +51 -28
- package/dist/bundle.min.js +1 -1
- package/dist/bundleLint.js +25 -17
- package/dist/bundleLint.min.js +1 -1
- package/dist/bundleMinify.js +22 -14
- package/dist/bundleMinify.min.js +1 -1
- package/dist/partials/imagesFor.js +42 -42
- package/dist/partials/loadPeer.js +70 -70
- package/dist/partials/standardLint.js +73 -73
- package/dist/partials/tsFor.js +70 -70
- package/dist/partials.js +136 -136
- package/dist/typeDocs.js +9 -9
- package/dist/utilities/getBundleConfigs.js +36 -0
- package/functions/bundle.mjs +30 -16
- package/functions/bundle.test.mjs +77 -0
- package/functions/bundleLint.mjs +8 -2
- package/functions/bundleLint.test.mjs +20 -0
- package/functions/bundleMinify.mjs +9 -2
- package/functions/bundleMinify.test.mjs +24 -2
- package/functions/utilities/getBundleConfigs.mjs +29 -0
- package/gulp.config.mjs +15 -0
- package/package.json +2 -1
package/functions/bundle.mjs
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
import browserify from 'browserify'
|
|
2
2
|
import { dest } from 'gulp'
|
|
3
|
+
import mergeStream from 'merge-stream'
|
|
3
4
|
import * as gulpConfig from '../gulp.config.mjs'
|
|
4
5
|
import source from 'vinyl-source-stream'
|
|
6
|
+
import { getBundleConfigs } from './utilities/getBundleConfigs.mjs'
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
|
-
* Starting at
|
|
9
|
+
* Starting at each bundle's own entry point (dist.main for the primary bundle, or its own
|
|
10
|
+
* override from browser.bundles), bundle all the reachable files into a single file per bundle and
|
|
11
|
+
* store them in the specified output directory. A project with no browser.bundles entries gets
|
|
12
|
+
* exactly the one bundle it always did; each additional entry produces its own separate bundle
|
|
13
|
+
* (for example, a render-only variant built from a different entry point than the full bundle).
|
|
14
|
+
*
|
|
15
|
+
* A bundle's name does double duty: it names the output file (name.js) and, via browserify's
|
|
16
|
+
* `standalone` option, is also the name its exports are exposed under - as a global when loaded
|
|
17
|
+
* with a bare <script> tag, or as the module when required via CommonJS/AMD. Without this, a
|
|
18
|
+
* bundle built from a real entry point (one that actually exports something) builds successfully
|
|
19
|
+
* but exposes nothing a consumer can reach.
|
|
8
20
|
*
|
|
9
21
|
* Two escape hatches from browser.config.json keep an unwanted dependency (or a dependency's dependency) out of the
|
|
10
22
|
* bundle: 'ignore' replaces it with an empty object (safe when your code never actually calls into it - for example
|
|
@@ -13,18 +25,20 @@ import source from 'vinyl-source-stream'
|
|
|
13
25
|
* @memberOf module:js-build-tools
|
|
14
26
|
* @returns {stream.Stream}
|
|
15
27
|
*/
|
|
16
|
-
export const bundle = () =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
export const bundle = () => mergeStream(
|
|
29
|
+
getBundleConfigs().map(({ name, main, ignore, exclude }) => {
|
|
30
|
+
const bundler = browserify(main, { standalone: name })
|
|
31
|
+
if (ignore && ignore.length) {
|
|
32
|
+
bundler.ignore(ignore)
|
|
33
|
+
}
|
|
34
|
+
if (exclude && exclude.length) {
|
|
35
|
+
bundler.exclude(exclude)
|
|
36
|
+
}
|
|
37
|
+
return bundler
|
|
38
|
+
.bundle()
|
|
39
|
+
.pipe(source(`${name}.js`))
|
|
40
|
+
})
|
|
41
|
+
// One shared dest() for every bundle, not one per bundle - vinyl-fs's dest() races against
|
|
42
|
+
// itself (a real "write after end" failure) when two separate dest() streams target the same
|
|
43
|
+
// directory concurrently, which every additional bundle's own dest() call otherwise would.
|
|
44
|
+
).pipe(dest(gulpConfig.get('browser.to')))
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
2
3
|
import * as setUp from './test-helpers/setUp.mjs'
|
|
3
4
|
import { bundle } from './bundle.mjs'
|
|
4
5
|
import { countMatches } from 'test-filesystem'
|
|
@@ -66,6 +67,82 @@ describe('bundle', () => {
|
|
|
66
67
|
done()
|
|
67
68
|
})
|
|
68
69
|
})
|
|
70
|
+
|
|
71
|
+
test('exposes browser.name as the standalone export, reachable by a real consumer', done => {
|
|
72
|
+
const destPath = gulpConfig.get('browser.to')
|
|
73
|
+
const bundledFile = `${destPath}/${gulpConfig.get('browser.name')}.js`
|
|
74
|
+
expect.assertions(2)
|
|
75
|
+
bundle()
|
|
76
|
+
.on('finish', () => {
|
|
77
|
+
// A UMD bundle (browserify's `standalone` option) resolves to its CommonJS branch when
|
|
78
|
+
// required like this, and to a `window.<browser.name>` global when loaded via a bare
|
|
79
|
+
// <script> tag in a real browser - both paths hit the same exported value.
|
|
80
|
+
const resolved = path.resolve(bundledFile)
|
|
81
|
+
delete require.cache[resolved]
|
|
82
|
+
const loaded = require(resolved)
|
|
83
|
+
expect(typeof loaded.file1).toBe('function')
|
|
84
|
+
expect(typeof loaded.file2).toBe('function')
|
|
85
|
+
done()
|
|
86
|
+
})
|
|
87
|
+
.on('error', error => done(error))
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
describe('bundle with additional bundles', () => {
|
|
92
|
+
const renderOnlyFileContents = 'const file1 = require(\'./sources/file1\')\n' +
|
|
93
|
+
'\n' +
|
|
94
|
+
'module.exports = {\n' +
|
|
95
|
+
' file1,\n' +
|
|
96
|
+
'}\n'
|
|
97
|
+
|
|
98
|
+
afterEach(() => gulpConfig.set('browser.bundles', []))
|
|
99
|
+
|
|
100
|
+
test('produces one file per additional bundle, each from its own entry point', done => {
|
|
101
|
+
const distPath = gulpConfig.get('dist.to')
|
|
102
|
+
const renderOnlyMain = `${distPath}/renderOnly`
|
|
103
|
+
fs.writeFileSync(`${renderOnlyMain}.js`, renderOnlyFileContents)
|
|
104
|
+
gulpConfig.set('browser.bundles', [{ name: 'renderOnly', main: renderOnlyMain }])
|
|
105
|
+
|
|
106
|
+
const destPath = gulpConfig.get('browser.to')
|
|
107
|
+
const primaryFile = `${destPath}/${gulpConfig.get('browser.name')}.js`
|
|
108
|
+
const renderOnlyFile = `${destPath}/renderOnly.js`
|
|
109
|
+
expect.assertions(4)
|
|
110
|
+
bundle()
|
|
111
|
+
.on('finish', () => {
|
|
112
|
+
// The primary bundle is unaffected - still built from dist.main, still has both files.
|
|
113
|
+
const primaryContents = fs.readFileSync(primaryFile).toString()
|
|
114
|
+
expect(primaryContents).toContain('Hello from file1')
|
|
115
|
+
expect(primaryContents).toContain('Good-bye from file2')
|
|
116
|
+
|
|
117
|
+
// The additional bundle came from its own entry point - file1 only, never file2.
|
|
118
|
+
const renderOnlyContents = fs.readFileSync(renderOnlyFile).toString()
|
|
119
|
+
expect(renderOnlyContents).toContain('Hello from file1')
|
|
120
|
+
expect(renderOnlyContents).not.toContain('Good-bye from file2')
|
|
121
|
+
done()
|
|
122
|
+
})
|
|
123
|
+
.on('error', error => done(error))
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('an additional bundle exposes its own name as its standalone export', done => {
|
|
127
|
+
const distPath = gulpConfig.get('dist.to')
|
|
128
|
+
const renderOnlyMain = `${distPath}/renderOnly`
|
|
129
|
+
fs.writeFileSync(`${renderOnlyMain}.js`, renderOnlyFileContents)
|
|
130
|
+
gulpConfig.set('browser.bundles', [{ name: 'renderOnly', main: renderOnlyMain }])
|
|
131
|
+
|
|
132
|
+
const destPath = gulpConfig.get('browser.to')
|
|
133
|
+
const renderOnlyFile = `${destPath}/renderOnly.js`
|
|
134
|
+
expect.assertions(2)
|
|
135
|
+
bundle()
|
|
136
|
+
.on('finish', () => {
|
|
137
|
+
const resolved = path.resolve(renderOnlyFile)
|
|
138
|
+
delete require.cache[resolved]
|
|
139
|
+
const loaded = require(resolved)
|
|
140
|
+
expect(typeof loaded.file1).toBe('function')
|
|
141
|
+
expect(loaded.file2).toBeUndefined()
|
|
142
|
+
done()
|
|
143
|
+
})
|
|
144
|
+
.on('error', error => done(error))
|
|
145
|
+
})
|
|
69
146
|
})
|
|
70
147
|
|
|
71
148
|
describe('bundle ignore / exclude', () => {
|
package/functions/bundleLint.mjs
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { dest, src } from 'gulp'
|
|
2
2
|
import * as gulpConfig from '../gulp.config.mjs'
|
|
3
3
|
import { standardLint } from './partials/standardLint.mjs'
|
|
4
|
+
import { getBundleConfigs } from './utilities/getBundleConfigs.mjs'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* Applies Standard code style linting to bundled file
|
|
7
|
+
* Applies Standard code style linting to every bundled file (the primary bundle, plus any
|
|
8
|
+
* additional ones from browser.bundles). The lint step itself never varies per bundle, so this is
|
|
9
|
+
* one shared src()/dest() pipeline fed every bundle's filename at once, rather than one pipeline
|
|
10
|
+
* per bundle - gulp's src()/dest() both accept an array of paths natively.
|
|
7
11
|
* @memberOf module:js-build-tools
|
|
8
12
|
* @returns {stream.Stream}
|
|
9
13
|
*/
|
|
10
|
-
export const bundleLint = () => src(
|
|
14
|
+
export const bundleLint = () => src(
|
|
15
|
+
getBundleConfigs().map(({ name }) => `${gulpConfig.get('browser.to')}/${name}.js`)
|
|
16
|
+
)
|
|
11
17
|
.pipe(standardLint({ fix: true }))
|
|
12
18
|
.pipe(dest(gulpConfig.get('browser.to')))
|
|
@@ -62,4 +62,24 @@ describe('bundleLint', () => {
|
|
|
62
62
|
done()
|
|
63
63
|
})
|
|
64
64
|
}, 10000)
|
|
65
|
+
|
|
66
|
+
test('lints every additional bundle too, not just the primary one', done => {
|
|
67
|
+
const browserPath = gulpConfig.get('browser.to')
|
|
68
|
+
const renderOnlyFile = `${browserPath}/renderOnly.js`
|
|
69
|
+
fs.writeFileSync(renderOnlyFile, lintableContents)
|
|
70
|
+
gulpConfig.set('browser.bundles', [{ name: 'renderOnly' }])
|
|
71
|
+
expect.assertions(2)
|
|
72
|
+
bundleLint()
|
|
73
|
+
.on('finish', () => {
|
|
74
|
+
const lintedContents = fs.readFileSync(renderOnlyFile).toString()
|
|
75
|
+
expect(countMatches(lintedContents, '"')).toEqual(0)
|
|
76
|
+
expect(countMatches(lintedContents, ';')).toEqual(0)
|
|
77
|
+
gulpConfig.set('browser.bundles', [])
|
|
78
|
+
done()
|
|
79
|
+
})
|
|
80
|
+
.on('error', error => {
|
|
81
|
+
console.error('Encountered error', error)
|
|
82
|
+
done(error)
|
|
83
|
+
})
|
|
84
|
+
}, 10000)
|
|
65
85
|
})
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import * as gulpConfig from '../gulp.config.mjs'
|
|
2
2
|
import { minifyFor } from './partials/minifyFor.mjs'
|
|
3
|
+
import { getBundleConfigs } from './utilities/getBundleConfigs.mjs'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* Creates the minified bundle
|
|
6
|
+
* Creates the minified file for every bundle (the primary bundle, plus any additional ones from
|
|
7
|
+
* browser.bundles). The minify step itself never varies per bundle, so this is one shared
|
|
8
|
+
* src()/dest() pipeline (inside minifyFor) fed every bundle's filename at once, rather than one
|
|
9
|
+
* pipeline per bundle - gulp's src() accepts an array of paths natively.
|
|
6
10
|
* @memberOf module:js-build-tools
|
|
7
11
|
* @returns {*}
|
|
8
12
|
*/
|
|
9
|
-
export const bundleMinify = () => minifyFor(
|
|
13
|
+
export const bundleMinify = () => minifyFor(
|
|
14
|
+
getBundleConfigs().map(({ name }) => `${gulpConfig.get('browser.to')}/${name}.js`),
|
|
15
|
+
gulpConfig.get('browser.to')
|
|
16
|
+
)
|
|
@@ -7,9 +7,31 @@ const gulpConfig = testHelpers.gulpConfig
|
|
|
7
7
|
jest.mock('./partials/minifyFor.mjs', () => ({ minifyFor: jest.fn(() => Promise.resolve(true)) }))
|
|
8
8
|
|
|
9
9
|
describe('bundleMinify', () => {
|
|
10
|
-
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
gulpConfig.set('browser.bundles', [])
|
|
12
|
+
minifyFor.mockClear()
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('calls minifyFor with the primary bundle', () => {
|
|
11
16
|
expect.assertions(1)
|
|
12
17
|
bundleMinify()
|
|
13
|
-
expect(minifyFor).toHaveBeenCalledWith(
|
|
18
|
+
expect(minifyFor).toHaveBeenCalledWith(
|
|
19
|
+
[`${gulpConfig.get('browser.to')}/${gulpConfig.get('browser.name')}.js`],
|
|
20
|
+
gulpConfig.get('browser.to')
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('calls minifyFor once, with every additional bundle included in the same array', () => {
|
|
25
|
+
gulpConfig.set('browser.bundles', [{ name: 'renderOnly' }])
|
|
26
|
+
expect.assertions(2)
|
|
27
|
+
bundleMinify()
|
|
28
|
+
expect(minifyFor).toHaveBeenCalledTimes(1)
|
|
29
|
+
expect(minifyFor).toHaveBeenCalledWith(
|
|
30
|
+
[
|
|
31
|
+
`${gulpConfig.get('browser.to')}/${gulpConfig.get('browser.name')}.js`,
|
|
32
|
+
`${gulpConfig.get('browser.to')}/renderOnly.js`
|
|
33
|
+
],
|
|
34
|
+
gulpConfig.get('browser.to')
|
|
35
|
+
)
|
|
14
36
|
})
|
|
15
37
|
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as gulpConfig from '../../gulp.config.mjs'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Build the full list of bundles to produce for this project: the primary bundle (browser.name /
|
|
5
|
+
* dist.main / browser.ignore / browser.exclude, exactly as before browser.bundles existed) plus
|
|
6
|
+
* any additional bundles from browser.bundles - each falling back to the primary's main / ignore /
|
|
7
|
+
* exclude whenever its own isn't given, so a render-only (or any other variant) bundle only has to
|
|
8
|
+
* specify what's actually different about it.
|
|
9
|
+
* @memberOf module:utilities
|
|
10
|
+
* @returns {Array<{name: string, main: string, ignore: Array, exclude: Array}>}
|
|
11
|
+
*/
|
|
12
|
+
export const getBundleConfigs = () => {
|
|
13
|
+
const primary = {
|
|
14
|
+
name: gulpConfig.get('browser.name'),
|
|
15
|
+
main: gulpConfig.get('dist.main'),
|
|
16
|
+
ignore: gulpConfig.get('browser.ignore'),
|
|
17
|
+
exclude: gulpConfig.get('browser.exclude')
|
|
18
|
+
}
|
|
19
|
+
const additional = gulpConfig.get('browser.bundles') || []
|
|
20
|
+
return [
|
|
21
|
+
primary,
|
|
22
|
+
...additional.map((bundleConfig) => ({
|
|
23
|
+
name: bundleConfig.name,
|
|
24
|
+
main: bundleConfig.main || primary.main,
|
|
25
|
+
ignore: bundleConfig.ignore || primary.ignore,
|
|
26
|
+
exclude: bundleConfig.exclude || primary.exclude
|
|
27
|
+
}))
|
|
28
|
+
]
|
|
29
|
+
}
|
package/gulp.config.mjs
CHANGED
|
@@ -60,6 +60,20 @@ import { fileExists } from 'test-filesystem'
|
|
|
60
60
|
* @property {ArrayableSetting} exclude - Module names or file paths left out of the bundle entirely; requiring one at
|
|
61
61
|
* runtime throws unless something else provides it (for example a script tag loading it separately). See
|
|
62
62
|
* browserify's b.exclude(file).
|
|
63
|
+
* @property {Array<module:gulpConfig~AdditionalBundleConfig>} bundles - Additional bundles to produce alongside the
|
|
64
|
+
* primary one (name/dist.main/ignore/exclude above) - for example a render-only variant built from a different
|
|
65
|
+
* entry point than the full bundle. Empty by default: a project that never sets this gets exactly the one bundle it
|
|
66
|
+
* always did.
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* One additional bundle - everything but `name` falls back to the primary bundle's own value when omitted, so an
|
|
70
|
+
* additional bundle only has to specify what's actually different about it.
|
|
71
|
+
* @typedef {Object<string, Setting>} module:gulpConfig~AdditionalBundleConfig
|
|
72
|
+
* @property {StringSetting} name - The name for this bundle's output file and standalone export - must be unique
|
|
73
|
+
* among all of a project's bundles.
|
|
74
|
+
* @property {StringSetting} [main] - This bundle's own entry point. Falls back to dist.main when omitted.
|
|
75
|
+
* @property {ArrayableSetting} [ignore] - This bundle's own ignore list. Falls back to browser.ignore when omitted.
|
|
76
|
+
* @property {ArrayableSetting} [exclude] - This bundle's own exclude list. Falls back to browser.exclude when omitted.
|
|
63
77
|
*/
|
|
64
78
|
/**
|
|
65
79
|
* Configurations for building the node distribution files.
|
|
@@ -150,6 +164,7 @@ import { readFileSync } from 'fs'
|
|
|
150
164
|
|
|
151
165
|
const setDefaults = {
|
|
152
166
|
browser: {
|
|
167
|
+
bundles: [],
|
|
153
168
|
enabled: true,
|
|
154
169
|
exclude: [],
|
|
155
170
|
from: 'dist/**/*.js',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-build-tools",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Standardised build process with gulp.",
|
|
5
5
|
"main": "gulpfile.base.js",
|
|
6
6
|
"exports": {
|
|
@@ -68,6 +68,7 @@
|
|
|
68
68
|
"jest": "^30.5.2",
|
|
69
69
|
"jest-environment-jsdom": "^30.5.2",
|
|
70
70
|
"jsdoc-to-markdown": "^9.1.3",
|
|
71
|
+
"merge-stream": "^2.0.0",
|
|
71
72
|
"postcss": "^8.5.28",
|
|
72
73
|
"regenerator-runtime": "^0.14.1",
|
|
73
74
|
"sass": "^1.105.0",
|