-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgulpfile.js
401 lines (331 loc) · 11 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
const { Readable, Transform } = require('stream'); // Built-in
const browserify = require('browserify');
const browserSync = require('browser-sync');
const del = require('del');
const fs = require('fs'); // Built-in
const gulp = require('gulp');
const gulpBabel = require('gulp-babel');
const gulpBrotli = require('gulp-brotli');
const gulpFile = require('gulp-file');
const gulpGzip = require('gulp-gzip');
const gulpRename = require('gulp-rename');
const gulpTape = require('gulp-tape');
const gulpTerser = require('gulp-terser');
const gulpZopfliGreen = require('gulp-zopfli-green');
const path = require('path'); // Built-in
const rollup = require('rollup');
const rollupPluginRootImport = require('rollup-plugin-root-import');
const vinylBuffer = require('vinyl-buffer');
const vinylSourceStream = require('vinyl-source-stream');
const p = {
src: `${__dirname}/src`,
testSrc: './src/tests',
dist: './dist',
testBrowser: './_testing/browser',
testNode: './_testing/node',
compression: './_testing/compression',
};
function onError(e) {
console.error(e); // eslint-disable-line no-console
this.emit('end');
}
const updateContents = (fn) => {
const transformer = new Transform({
objectMode: true,
});
transformer._transform = (file, enc, cb) => {
if (file.isBuffer()) {
const changes = fn(String(file.contents), file);
if (changes || changes === '') {
file.contents = Buffer.from(changes);
}
}
cb(null, file);
};
return transformer;
};
const babelModern = (stream) => {
stream = stream.pipe(gulpBabel({
sourceType: 'script', // Prevents adding of strict mode automatically, which can break some require declarations
presets: [
['@babel/env', {
targets: {
browsers: [
'> 0.2%, ie >= 9, not ie <= 8, not op_mini all',
],
},
loose: true, // Safe because typeof x === 'object' will never be called relative to a Symbol
}],
],
}));
stream = stream.on('error', onError);
return stream;
};
const babelNodePassthrough = (stream) => {
stream = stream.pipe(gulpBabel({
plugins: [
['babel-plugin-module-resolver', { root: [p.src] }],
'babel-plugin-transform-esm-to-cjs'
],
}));
return stream;
};
const jsBuild = (options) => {
const format = options.format || 'iife';
const outputOptions = {
format: format,
};
if (format === 'cjs') {
outputOptions.output = {
exports: 'default',
};
}
return rollup.rollup({
input: options.src,
plugins: [
rollupPluginRootImport({
root: p.src,
}),
],
}).then((bundler) => {
return bundler.generate(outputOptions);
}).then((bundlerOutput) => {
const { output } = bundlerOutput;
// Can't just return the stream in this case
return new Promise((resolve) => {
let stream = gulpFile(options.filename, output[0].code, { src: true });
stream = stream.pipe(updateContents((contents) => {
// Because json-complete is constructed such that the `export default` is exactly the same as `module.exports`, we can use them interchangably.
// Unfortunately, Rollup can't know this. We added the `output.exports` of default to silence a warning, but that causes the output to not add `module.exports` at all.
// Rather than file a bug and wait, and to avoid future incompatibility with Rollup changes, we just manually change the file to do what it should do when exporting cjs files.
if (format === 'cjs') {
contents = contents.replace(/export default /, 'module.exports = ');
}
// Add the license info to the top of the file
return ['/* @license BSL-1.0 https://git.io/fpQEc */', contents].join('\n');
}));
// ESM output guarantees a certain level of ES support, which the library itself is tied to
if (format !== 'esm') {
stream = babelModern(stream);
}
// Export unminified
stream = stream.pipe(gulp.dest(options.dest));
// Export minified
if (options.minify) {
stream = stream.pipe(gulpTerser({
mangle: {
toplevel: true,
properties: {
regex: /_\w+/, // Compress all properties that start with _, but contain more than just an underscore
},
},
compress: {
inline: true,
},
output: {
comments: 'some',
},
}));
stream = stream.on('error', onError);
stream = stream.pipe(gulpRename({
suffix: '.min',
}));
stream = stream.pipe(gulp.dest(options.dest));
}
stream = stream.on('end', resolve);
return stream;
});
});
};
const genCompression = (fn) => {
let stream = gulp.src(`${p.dist}/*.min.js`);
stream = stream.pipe(gulp.dest(p.compression));
stream = fn(stream);
stream = stream.pipe(gulp.dest(p.compression));
return stream;
};
gulp.task('clear-browser', () => {
return del([p.testBrowser]);
});
gulp.task('build-browser-js-tape', () => {
// http://stackoverflow.com/a/36042506
const s = new Readable();
s.push('"";'); // No contents required, because require option adds the exported dependencies anyway
s.push(null);
let stream = browserify(s, {
require: 'tape',
debug: false,
}).bundle();
stream = stream.on('error', onError);
stream = stream.pipe(vinylSourceStream('tapeImporter.js'));
stream = stream.pipe(vinylBuffer());
stream = stream.on('error', onError);
stream = babelModern(stream);
stream = stream.pipe(gulp.dest(p.testBrowser));
return stream;
});
gulp.task('build-browser-js-tests', () => {
return jsBuild({
src: `${p.testSrc}/tests.js`,
format: 'iife',
minify: false,
filename: 'tests.js',
dest: p.testBrowser,
});
});
gulp.task('build-browser-html', () => {
let stream = gulp.src(`${p.testSrc}/index.html`);
stream = stream.pipe(gulp.dest(p.testBrowser));
return stream;
});
gulp.task('build-browser', gulp.parallel('build-browser-js-tape', 'build-browser-js-tests', 'build-browser-html'));
gulp.task('test-serve', () => {
browserSync.init({
files: [`${p.testBrowser}/**/*`],
reloadDebounce: 1000,
port: 4000,
ui: {
port: 5001,
},
server: {
baseDir: p.testBrowser,
},
ghostMode: false,
snippetOptions: {
rule: {
match: /(?:<\/body>)|$/i,
fn: (snippet, match) => {
return '\n\n' + snippet + match;
},
},
},
});
gulp.watch(`${p.src}/**/*.*`, gulp.series('build-browser')).on('change', browserSync.reload);
});
gulp.task('test-browser', gulp.series(
'clear-browser',
'build-browser',
'test-serve',
));
gulp.task('clear-node', () => {
return del([p.testNode]);
});
gulp.task('build-node-js', () => {
let stream = gulp.src([
`${p.src}/**/*.js`,
`!${p.testSrc}/tests.js`, // Don't need a specific start file to import all the other tests for node, unlike the Browser
]);
stream = babelNodePassthrough(stream);
stream = stream.pipe(gulp.dest(p.testNode));
return stream;
});
gulp.task('test-node', () => {
return gulp.src(`${p.testNode}/tests/FeatureTests/*.js`)
.pipe(gulpTape({
bail: true,
nyc: true,
}))
.on('error', onError);
});
gulp.task('test', gulp.series(
'clear-node',
'build-node-js',
'test-node',
'clear-node'
));
gulp.task('clear-compression', () => {
return del([p.compression]);
});
gulp.task('compress-gzip', () => {
return genCompression((stream) => {
return stream.pipe(gulpGzip({
extension: 'zip',
}));
});
});
gulp.task('compress-zopfli', () => {
return genCompression((stream) => {
return stream.pipe(gulpZopfliGreen());
});
});
gulp.task('compress-brotli', () => {
return genCompression((stream) => {
return stream.pipe(gulpBrotli.compress());
});
});
gulp.task('compress-calculate', (end) => {
const onEnd = (manifest) => {
const extensionToCompression = {
js: 'base',
zip: 'gzip',
gz: 'zopfli',
br: 'brotli',
};
// Convert data to usable form
const types = {};
Object.keys(manifest).forEach((file) => {
const type = file.replace(/^[^.]+\.|\.min.+$/g, '');
types[type] = types[type] || {};
types[type][extensionToCompression[file.match(/\.(.{1,4})$/)[1]]] = manifest[file];
});
// Convert data to displayable form
const esmBaseSize = types.esm.base;
const cjsBaseSize = types.cjs.base;
const table = `
| Compression | ES Module | CommonJS |
|-------------|------------|----------|
| Minified | ${esmBaseSize} bytes | ${cjsBaseSize} bytes |
| gzip | ${types.esm.gzip} bytes | ${types.cjs.gzip} bytes |
| zopfli | ${types.esm.zopfli} bytes | ${types.cjs.zopfli} bytes |
| brotli | ${types.esm.brotli} bytes | ${types.cjs.brotli} bytes |
`;
console.log(table); // eslint-disable-line no-console
end();
};
fs.readdir(p.compression, (err, files) => {
let fileCount = files.length;
const manifest = {};
files.forEach((file) => {
fs.stat(path.join(p.compression, file), (err, stats) => {
manifest[file] = stats.size;
fileCount -= 1;
if (fileCount === 0) {
onEnd(manifest);
}
});
});
});
});
gulp.task('compression-report', gulp.series(
'clear-compression',
gulp.parallel('compress-gzip', 'compress-zopfli', 'compress-brotli'),
'compress-calculate',
'clear-compression'
));
gulp.task('clear-dist', () => {
return del([`${p.dist}/**/*`]);
});
gulp.task('prod-esm', () => {
return jsBuild({
src: `${p.src}/main.js`,
filename: 'json_complete.esm.js',
format: 'esm',
unminify: true,
minify: true,
dest: p.dist,
});
});
gulp.task('prod-cjs', () => {
return jsBuild({
src: `${p.src}/main.js`,
filename: 'json_complete.cjs.js',
format: 'cjs',
unminify: true,
minify: true,
dest: p.dist,
});
});
gulp.task('prod', gulp.series(
'clear-dist',
gulp.parallel('prod-esm', 'prod-cjs'),
));