Skip to content

Commit 67e2017

Browse files
committed
extract function to bundle JavaScript files
1 parent 65236e0 commit 67e2017

File tree

3 files changed

+37
-47
lines changed

3 files changed

+37
-47
lines changed

gulp.d/tasks/build.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const autoprefixer = require('autoprefixer')
44
const browserify = require('browserify')
5-
const buffer = require('vinyl-buffer')
65
const concat = require('gulp-concat')
76
const cssnano = require('cssnano')
87
const fs = require('fs')
@@ -60,43 +59,19 @@ module.exports = (src, dest, preview) => () => {
6059

6160
return merge(
6261
vfs
63-
.src('js/+([0-9])-*.js', { ...opts, sourcemaps })
64-
.pipe(uglify())
62+
.src('js/+([0-9])-*.js', { ...opts, read: false, sourcemaps })
63+
.pipe(bundle(opts))
64+
.pipe(uglify({ output: { comments: /^! / } }))
6565
// NOTE concat already uses stat from newest combined file
6666
.pipe(concat('js/site.js')),
6767
vfs
68-
.src('js/vendor/*.js', { ...opts, read: false })
69-
.pipe(
70-
// see https://gulpjs.org/recipes/browserify-multiple-destination.html
71-
map((file, enc, next) => {
72-
if (file.relative.endsWith('.bundle.js')) {
73-
const mtimePromises = []
74-
const bundlePath = file.path
75-
browserify(file.relative, { basedir: src, detectGlobals: false })
76-
.plugin('browser-pack-flat/plugin')
77-
.on('file', (bundledPath) => {
78-
if (bundledPath !== bundlePath) mtimePromises.push(fsp.stat(bundledPath).then(({ mtime }) => mtime))
79-
})
80-
.bundle((bundleError, bundleBuffer) =>
81-
Promise.all(mtimePromises).then((mtimes) => {
82-
const newestMtime = mtimes.reduce((max, curr) => (curr > max ? curr : max), file.stat.mtime)
83-
if (newestMtime > file.stat.mtime) file.stat.mtimeMs = +(file.stat.mtime = newestMtime)
84-
if (bundleBuffer !== undefined) file.contents = bundleBuffer
85-
file.path = file.path.slice(0, file.path.length - 10) + '.js'
86-
next(bundleError, file)
87-
})
88-
)
89-
} else {
90-
fsp.readFile(file.path, 'UTF-8').then((contents) => {
91-
file.contents = Buffer.from(contents)
92-
next(null, file)
93-
})
94-
}
95-
})
96-
)
97-
.pipe(buffer())
98-
.pipe(uglify()),
99-
// NOTE use this statement to bundle a JavaScript library that cannot be browserified, like jQuery
68+
.src('js/vendor/*([^.])?(.bundle).js', { ...opts, read: false })
69+
.pipe(bundle(opts))
70+
.pipe(uglify({ output: { comments: /^! / } })),
71+
vfs
72+
.src('js/vendor/*.min.js', opts)
73+
.pipe(map((file, enc, next) => next(null, Object.assign(file, { extname: '' }, { extname: '.js' })))),
74+
// NOTE use the next line to bundle a JavaScript library that cannot be browserified, like jQuery
10075
//vfs.src(require.resolve('<package-name-or-require-path>'), opts).pipe(concat('js/vendor/<library-name>.js')),
10176
vfs
10277
.src(['css/site.css', 'css/home.css', 'css/vendor/*.css'], { ...opts, sourcemaps })
@@ -126,6 +101,32 @@ module.exports = (src, dest, preview) => () => {
126101
).pipe(vfs.dest(dest, { sourcemaps: sourcemaps && '.' }))
127102
}
128103

104+
function bundle ({ base: basedir, ext: bundleExt = '.bundle.js' }) {
105+
return map((file, enc, next) => {
106+
if (bundleExt && file.relative.endsWith(bundleExt)) {
107+
const mtimePromises = []
108+
const bundlePath = file.path
109+
browserify(file.relative, { basedir, detectGlobals: false })
110+
.plugin('browser-pack-flat/plugin')
111+
.on('file', (bundledPath) => {
112+
if (bundledPath !== bundlePath) mtimePromises.push(fsp.stat(bundledPath).then(({ mtime }) => mtime))
113+
})
114+
.bundle((bundleError, bundleBuffer) =>
115+
Promise.all(mtimePromises).then((mtimes) => {
116+
const newestMtime = mtimes.reduce((max, curr) => (curr > max ? curr : max), file.stat.mtime)
117+
if (newestMtime > file.stat.mtime) file.stat.mtimeMs = +(file.stat.mtime = newestMtime)
118+
if (bundleBuffer !== undefined) file.contents = bundleBuffer
119+
next(bundleError, Object.assign(file, { path: file.path.slice(0, file.path.length - 10) + '.js' }))
120+
})
121+
)
122+
return
123+
}
124+
fsp.readFile(file.path, 'UTF-8').then((contents) => {
125+
next(null, Object.assign(file, { contents: Buffer.from(contents) }))
126+
})
127+
})
128+
}
129+
129130
function postcssPseudoElementFixer (css, result) {
130131
css.walkRules(/(?:^|[^:]):(?:before|after)/, (rule) => {
131132
rule.selector = rule.selectors.map((it) => it.replace(/(^|[^:]):(before|after)$/, '$1::$2')).join(',')

package-lock.json

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"require-from-string": "~2.0",
5959
"stylelint": "~13.8",
6060
"stylelint-config-standard": "~20.0",
61-
"vinyl-buffer": "~1.0",
6261
"vinyl-fs": "~3.0"
6362
}
6463
}

0 commit comments

Comments
 (0)