|
2 | 2 |
|
3 | 3 | const autoprefixer = require('autoprefixer')
|
4 | 4 | const browserify = require('browserify')
|
5 |
| -const buffer = require('vinyl-buffer') |
6 | 5 | const concat = require('gulp-concat')
|
7 | 6 | const cssnano = require('cssnano')
|
8 | 7 | const fs = require('fs')
|
@@ -60,43 +59,19 @@ module.exports = (src, dest, preview) => () => {
|
60 | 59 |
|
61 | 60 | return merge(
|
62 | 61 | 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: /^! / } })) |
65 | 65 | // NOTE concat already uses stat from newest combined file
|
66 | 66 | .pipe(concat('js/site.js')),
|
67 | 67 | 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 |
100 | 75 | //vfs.src(require.resolve('<package-name-or-require-path>'), opts).pipe(concat('js/vendor/<library-name>.js')),
|
101 | 76 | vfs
|
102 | 77 | .src(['css/site.css', 'css/home.css', 'css/vendor/*.css'], { ...opts, sourcemaps })
|
@@ -126,6 +101,32 @@ module.exports = (src, dest, preview) => () => {
|
126 | 101 | ).pipe(vfs.dest(dest, { sourcemaps: sourcemaps && '.' }))
|
127 | 102 | }
|
128 | 103 |
|
| 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 | + |
129 | 130 | function postcssPseudoElementFixer (css, result) {
|
130 | 131 | css.walkRules(/(?:^|[^:]):(?:before|after)/, (rule) => {
|
131 | 132 | rule.selector = rule.selectors.map((it) => it.replace(/(^|[^:]):(before|after)$/, '$1::$2')).join(',')
|
|
0 commit comments