-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
helpers.js
820 lines (756 loc) · 23.4 KB
/
helpers.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
const argv = require('minimist')(process.argv.slice(2));
const debounce = require('../common/debounce');
const esbuild = require('esbuild');
/** @type {Object} */
const experimentDefines = require('../global-configs/experiments-const.json');
const fs = require('fs-extra');
const magicstring = require('magic-string');
const open = require('open');
const path = require('path');
const Remapping = require('@ampproject/remapping');
const terser = require('terser');
const wrappers = require('../compile/compile-wrappers');
const {
VERSION: internalRuntimeVersion,
} = require('../compile/internal-version');
const {closureCompile} = require('../compile/compile');
const {cyan, green, red} = require('kleur/colors');
const {getAmpConfigForFile} = require('./prepend-global');
const {getEsbuildBabelPlugin} = require('../common/esbuild-babel');
const {getSourceRoot} = require('../compile/helpers');
const {isCiBuild} = require('../common/ci');
const {jsBundles} = require('../compile/bundles.config');
const {log, logLocalDev} = require('../common/logging');
const {thirdPartyFrames} = require('../test-configs/config');
const {watch} = require('chokidar');
/** @type {Remapping.default} */
const remapping = /** @type {*} */ (Remapping);
/** @type {magicstring.default} */
const MagicString = /** @type {*} */ (magicstring);
/**
* Tasks that should print the `--nobuild` help text.
* @private @const {!Set<string>}
*/
const NOBUILD_HELP_TASKS = new Set(['e2e', 'integration', 'visual-diff']);
/**
* Used during minification to concatenate modules
*/
const MODULE_SEPARATOR = ';';
/**
* Used during minification to concatenate extension bundles
*/
const EXTENSION_BUNDLE_MAP = {
'amp-inputmask.js': ['third_party/inputmask/bundle.js'],
'amp-date-picker.js': ['third_party/react-dates/bundle.js'],
'amp-shadow-dom-polyfill.js': [
'node_modules/@webcomponents/webcomponentsjs/bundles/webcomponents-sd.install.js',
],
};
/**
* Used while building the 3p frame
**/
const hostname3p = argv.hostname3p || '3p.ampproject.net';
/**
* Used to debounce file edits during watch to prevent races.
*/
const watchDebounceDelay = 1000;
/**
* Stores esbuild's watch mode rebuilders.
* @private @const {!Map<string, {rebuild: function():!Promise<void>}>}
*/
const watchedTargets = new Map();
/**
* @param {!Object} jsBundles
* @param {string} name
* @param {?Object} extraOptions
* @return {!Promise}
*/
function doBuildJs(jsBundles, name, extraOptions) {
const target = jsBundles[name];
if (target) {
return compileJs(
target.srcDir,
target.srcFilename,
extraOptions.minify ? target.minifiedDestDir : target.destDir,
{...target.options, ...extraOptions}
);
} else {
return Promise.reject(
[red('Error:'), 'Could not find', cyan(name)].join(' ')
);
}
}
/**
* Generates frames.html
*
* @param {!Object} options
* @return {Promise<void>}
*/
async function bootstrapThirdPartyFrames(options) {
const startTime = Date.now();
if (options.watch) {
thirdPartyFrames.forEach((frameObject) => {
const watchFunc = async () => {
await thirdPartyBootstrap(frameObject.max, frameObject.min, options);
};
watch(frameObject.max).on(
'change',
debounce(watchFunc, watchDebounceDelay)
);
});
}
await Promise.all(
thirdPartyFrames.map(async (frameObject) => {
await thirdPartyBootstrap(frameObject.max, frameObject.min, options);
})
);
endBuildStep(
'Bootstrapped 3p frames into',
`dist.3p/${options.minify ? internalRuntimeVersion : 'current'}/`,
startTime
);
}
/**
* Compile and optionally minify the core runtime.
*
* @param {!Object} options
* @return {Promise<void>}
*/
async function compileCoreRuntime(options) {
await doBuildJs(jsBundles, 'amp.js', options);
}
/**
* Compile and optionally minify the stylesheets and the scripts for the runtime
* and drop them in the dist folder
*
* @param {!Object} options
* @return {!Promise}
*/
async function compileAllJs(options) {
const {minify} = options;
if (minify) {
log('Minifying multi-pass JS with', cyan('closure-compiler') + '...');
} else {
log('Compiling JS with', cyan('esbuild'), 'and', cyan('babel') + '...');
}
const startTime = Date.now();
await Promise.all([
minify ? Promise.resolve() : doBuildJs(jsBundles, 'polyfills.js', options),
doBuildJs(jsBundles, 'bento.js', options),
doBuildJs(jsBundles, 'alp.max.js', options),
doBuildJs(jsBundles, 'integration.js', options),
doBuildJs(jsBundles, 'ampcontext-lib.js', options),
doBuildJs(jsBundles, 'iframe-transport-client-lib.js', options),
doBuildJs(jsBundles, 'recaptcha.js', options),
doBuildJs(jsBundles, 'amp-viewer-host.max.js', options),
doBuildJs(jsBundles, 'video-iframe-integration.js', options),
doBuildJs(jsBundles, 'amp-story-entry-point.js', options),
doBuildJs(jsBundles, 'amp-story-player.js', options),
doBuildJs(jsBundles, 'amp-inabox-host.js', options),
doBuildJs(jsBundles, 'amp-shadow.js', options),
doBuildJs(jsBundles, 'amp-inabox.js', options),
]);
await compileCoreRuntime(options);
endBuildStep(
minify ? 'Minified' : 'Compiled',
'all runtime JS files',
startTime
);
}
/**
* Returns compiled file to prepend within wrapper and empty string if none.
*
* @param {string} srcFilename
* @return {Promise<string>}
*/
async function getCompiledFile(srcFilename) {
const bundleFiles = EXTENSION_BUNDLE_MAP[srcFilename];
if (!bundleFiles) {
return '';
}
const filesContents = await Promise.all(
bundleFiles.map((file) => fs.readFile(file, 'utf8'))
);
return filesContents.join('\n');
}
/**
* Allows pending inside the compile wrapper to the already minified JS file.
* @param {string} srcFilename Name of the JS source file
* @param {string} destFilePath File path to the minified JS file
* @param {?Object} options
*/
function combineWithCompiledFile(srcFilename, destFilePath, options) {
const bundleFiles = EXTENSION_BUNDLE_MAP[srcFilename];
if (!bundleFiles) {
return;
}
const bundle = new MagicString.Bundle({
separator: '\n',
});
// We need to inject the code _inside_ the extension wrapper
const destFileName = path.basename(destFilePath);
/**
* TODO (rileyajones) This should be import('magic-string').MagicStringOptions but
* is invalid until https://github.com/Rich-Harris/magic-string/pull/183
* is merged.
* @type {Object}
*/
const mapMagicStringOptions = {filename: destFileName};
const contents = new MagicString(
fs.readFileSync(destFilePath, 'utf8'),
mapMagicStringOptions
);
const map = JSON.parse(fs.readFileSync(`${destFilePath}.map`, 'utf8'));
const {sourceRoot} = map;
map.sourceRoot = undefined;
// The wrapper may have been minified further. Search backwards from the
// expected <%=contents%> location to find the start of the `{` in the
// wrapping function.
const wrapperIndex = options.wrapper.indexOf('<%= contents %>');
const index = contents.original.lastIndexOf('{', wrapperIndex) + 1;
const wrapperOpen = contents.snip(0, index);
const remainingContents = contents.snip(index, contents.length());
bundle.addSource(wrapperOpen);
for (const bundleFile of bundleFiles) {
const contents = fs.readFileSync(bundleFile, 'utf8');
/**
* TODO (rileyajones) This should be import('magic-string').MagicStringOptions but
* is invalid until https://github.com/Rich-Harris/magic-string/pull/183
* is merged.
* @type {Object}
*/
const bundleMagicStringOptions = {filename: bundleFile};
bundle.addSource(new MagicString(contents, bundleMagicStringOptions));
bundle.append(MODULE_SEPARATOR);
}
bundle.addSource(remainingContents);
const bundledMap = bundle.generateDecodedMap({
file: destFileName,
hires: true,
});
const remapped = remapping(
bundledMap,
(file) => {
if (file === destFileName) {
return map;
}
return null;
},
!argv.full_sourcemaps
);
remapped.sourceRoot = sourceRoot;
fs.writeFileSync(destFilePath, bundle.toString(), 'utf8');
fs.writeFileSync(`${destFilePath}.map`, remapped.toString(), 'utf8');
}
/**
* @param {string} name
* @return {string}
*/
function toEsmName(name) {
return name.replace(/\.js$/, '.mjs');
}
/**
* @param {string} name
* @return {string}
*/
function maybeToEsmName(name) {
// Npm esm names occur at an earlier stage.
if (name.includes('.module')) {
return name;
}
return argv.esm ? toEsmName(name) : name;
}
/**
* @param {string} name
* @return {string}
*/
function maybeToNpmEsmName(name) {
return argv.esm ? name.replace(/\.js$/, '.module.js') : name;
}
/**
* Minifies a given JavaScript file entry point.
* @param {string} srcDir
* @param {string} srcFilename
* @param {string} destDir
* @param {?Object} options
* @return {!Promise}
*/
async function compileMinifiedJs(srcDir, srcFilename, destDir, options) {
const timeInfo = {};
const entryPoint = path.join(srcDir, srcFilename);
const minifiedName = maybeToEsmName(options.minifiedName);
options.errored = false;
await closureCompile(entryPoint, destDir, minifiedName, options, timeInfo);
// If an incremental watch build fails, simply return.
if (options.watch && options.errored) {
return;
}
const destPath = path.join(destDir, minifiedName);
combineWithCompiledFile(srcFilename, destPath, options);
if (options.latestName) {
fs.copySync(
destPath,
path.join(destDir, maybeToEsmName(options.latestName))
);
}
let name = minifiedName;
if (options.latestName) {
name += ` → ${maybeToEsmName(options.latestName)}`;
}
endBuildStep('Minified', name, timeInfo.startTime);
}
/**
* Handles a bundling error
* @param {Error} err
* @param {boolean} continueOnError
* @param {string} destFilename
*/
function handleBundleError(err, continueOnError, destFilename) {
let message = err.toString();
if (err.stack) {
// Drop the node_modules call stack, which begins with ' at'.
message = err.stack.replace(/ at[^]*/, '').trim();
}
log(red('ERROR:'), message, '\n');
const reasonMessage = `Could not compile ${cyan(destFilename)}`;
if (continueOnError) {
log(red('ERROR:'), reasonMessage);
} else {
throw new Error(reasonMessage);
}
}
/**
* Performs the final steps after a JS file is bundled and optionally minified
* with esbuild and babel.
* @param {string} destDir
* @param {string} destFilename
* @param {?Object} options
* @param {number} startTime
* @return {Promise<void>}
*/
async function finishBundle(destDir, destFilename, options, startTime) {
const logPrefix = options.minify ? 'Minified' : 'Compiled';
let {latestName} = options;
if (latestName) {
if (!options.minify) {
latestName = latestName.replace(/\.js$/, '.max.js');
}
latestName = maybeToEsmName(latestName);
fs.copySync(
path.join(destDir, destFilename),
path.join(destDir, latestName)
);
endBuildStep(logPrefix, `${destFilename} → ${latestName}`, startTime);
} else {
const loggingName =
options.npm && !destFilename.startsWith('amp-')
? `${options.name} → ${destFilename}`
: destFilename;
endBuildStep(logPrefix, loggingName, startTime);
}
}
/**
* Transforms a given JavaScript file entry point with esbuild and babel, and
* watches it for changes (if required).
*
* @param {string} srcDir
* @param {string} srcFilename
* @param {string} destDir
* @param {?Object} options
* @return {!Promise}
*/
async function esbuildCompile(srcDir, srcFilename, destDir, options) {
const startTime = Date.now();
const entryPoint = path.join(srcDir, srcFilename);
const filename = options.minify
? options.minifiedName
: options.toName ?? srcFilename;
const destFilename = maybeToEsmName(filename);
const destFile = path.join(destDir, destFilename);
if (watchedTargets.has(entryPoint)) {
return watchedTargets.get(entryPoint).rebuild();
}
/**
* Splits up the wrapper to compute the banner and footer
* @return {Object}
*/
function splitWrapper() {
const wrapper = options.wrapper ?? wrappers.none;
const sentinel = '<%= contents %>';
const start = wrapper.indexOf(sentinel);
return {
banner: {js: wrapper.slice(0, start)},
footer: {js: wrapper.slice(start + sentinel.length)},
};
}
const {banner, footer} = splitWrapper();
const config = await getAmpConfigForFile(destFilename, options);
const compiledFile = await getCompiledFile(srcFilename);
banner.js = config + banner.js + compiledFile;
const babelPlugin = getEsbuildBabelPlugin(
options.minify ? 'minified' : 'unminified',
/* enableCache */ true
);
const plugins = [babelPlugin];
if (options.remapDependencies) {
plugins.unshift(remapDependenciesPlugin());
}
let result = null;
/**
* @param {number} startTime
* @return {Promise<void>}
*/
async function build(startTime) {
if (!result) {
result = await esbuild.build({
entryPoints: [entryPoint],
bundle: true,
sourcemap: true,
outfile: destFile,
define: experimentDefines,
plugins,
format: options.outputFormat,
banner,
footer,
// For es5 builds, ensure esbuild-injected code is transpiled.
target: argv.esm ? 'es6' : 'es5',
incremental: !!options.watch,
logLevel: 'silent',
external: options.externalDependencies,
write: false,
});
} else {
result = await result.rebuild();
}
let code = result.outputFiles.find(({path}) => !path.endsWith('.map')).text;
let map = result.outputFiles.find(({path}) => path.endsWith('.map')).text;
if (options.minify) {
({code, map} = await minify(code, map));
map = await massageSourcemaps(map, options);
}
await Promise.all([
fs.outputFile(destFile, code),
fs.outputFile(`${destFile}.map`, map),
]);
await finishBundle(destDir, destFilename, options, startTime);
}
/**
* Generates a plugin to remap the dependencies of a JS bundle.
* @return {Object}
*/
function remapDependenciesPlugin() {
const remapDependencies = {__proto__: null, ...options.remapDependencies};
const external = options.externalDependencies;
return {
name: 'remap-dependencies',
setup(build) {
build.onResolve({filter: /.*/}, (args) => {
const dep = args.path;
const remap = remapDependencies[dep];
if (remap) {
const isExternal = external.includes(remap);
return {
path: isExternal ? remap : require.resolve(remap),
external: isExternal,
};
}
});
},
};
}
await build(startTime).catch((err) =>
handleBundleError(err, !!options.watch, destFilename)
);
if (options.watch) {
watchedTargets.set(entryPoint, {
rebuild: async () => {
const startTime = Date.now();
const buildPromise = build(startTime).catch((err) =>
handleBundleError(err, !!options.watch, destFilename)
);
if (options.onWatchBuild) {
options.onWatchBuild(buildPromise);
}
await buildPromise;
},
});
}
}
/**
* Name cache to help terser perform cross-binary property mangling.
*
* TODO: ensure this is actually necessary.
*/
const nameCache = {};
/**
* Minify the code with Terser. Only used by the ESBuild.
*
* @param {string} code
* @param {string} map
* @return {!Promise<{code: string, map: *, error?: Error}>}
*/
async function minify(code, map) {
const terserOptions = {
mangle: {
properties: {
regex: '_AMP_PRIVATE_$',
// eslint-disable-next-line google-camelcase/google-camelcase
keep_quoted: /** @type {'strict'} */ ('strict'),
},
},
compress: {
// Settled on this count by incrementing number until there was no more
// effect on minification quality.
passes: 3,
},
output: {
beautify: !!argv.pretty_print,
// eslint-disable-next-line google-camelcase/google-camelcase
keep_quoted_props: true,
},
sourceMap: {content: map},
module: !!argv.esm,
nameCache,
};
// TS complains if defined inline, since it sees type `string` but needs type ("strict" | boolean).
const minified = await terser.minify(code, terserOptions);
return {code: minified.code ?? '', map: minified.map};
}
/**
* The set of entrypoints currently watched by compileJs.
* @type {Set<string>}
*/
const watchedEntryPoints = new Set();
/**
* Bundles (max) or compiles (min) a given JavaScript file entry point.
*
* @param {string} srcDir Path to the src directory
* @param {string} srcFilename Name of the JS source file
* @param {string} destDir Destination folder for output script
* @param {?Object} options
* @return {!Promise}
*/
async function compileJs(srcDir, srcFilename, destDir, options) {
options = options || {};
const entryPoint = path.join(srcDir, srcFilename);
if (watchedEntryPoints.has(entryPoint)) {
return;
}
if (options.watch) {
watchedEntryPoints.add(entryPoint);
const deps = await getDependencies(entryPoint, options);
const watchFunc = async () => {
await doCompileJs({...options, continueOnError: true});
};
watch(deps).on('change', debounce(watchFunc, watchDebounceDelay));
}
/**
* Actually performs the steps to compile the entry point.
* @param {Object} options
* @return {Promise<void>}
*/
async function doCompileJs(options) {
const buildResult =
options.minify && shouldUseClosure()
? compileMinifiedJs(srcDir, srcFilename, destDir, options)
: esbuildCompile(srcDir, srcFilename, destDir, options);
if (options.onWatchBuild) {
options.onWatchBuild(buildResult);
}
await buildResult;
}
await doCompileJs(options);
}
/**
* Stops the timer for the given build step and prints the execution time.
* @param {string} stepName Name of the action, like 'Compiled' or 'Minified'
* @param {string} targetName Name of the target, like a filename or path
* @param {DOMHighResTimeStamp} startTime Start time of build step
*/
function endBuildStep(stepName, targetName, startTime) {
const endTime = Date.now();
const executionTime = new Date(endTime - startTime);
const mins = executionTime.getMinutes();
const secs = executionTime.getSeconds();
const ms = ('000' + executionTime.getMilliseconds().toString()).slice(-3);
let timeString = '(';
if (mins > 0) {
timeString += mins + ' m ' + secs + '.' + ms + ' s)';
} else if (secs === 0) {
timeString += ms + ' ms)';
} else {
timeString += secs + '.' + ms + ' s)';
}
log(stepName, cyan(targetName), green(timeString));
}
/**
* Prints a helpful message that lets the developer know how to switch configs.
* @param {string} command Command being run.
*/
function printConfigHelp(command) {
log(
green('Building version'),
cyan(internalRuntimeVersion),
green('of the runtime with the'),
cyan(argv.config === 'canary' ? 'canary' : 'prod'),
green('AMP config.')
);
logLocalDev(
green('⤷ Use'),
cyan('--config={canary|prod}'),
green('with your'),
cyan(command),
green('command to specify which config to apply.')
);
}
/**
* Prints a message that could help speed up local development.
*/
function printNobuildHelp() {
for (const task of NOBUILD_HELP_TASKS) {
if (argv._.includes(task)) {
log(
green('To skip building during future'),
cyan(task),
green('runs, use'),
cyan('--nobuild'),
green('with your'),
cyan(`amp ${task}`),
green('command.')
);
return;
}
}
}
/**
* @param {string=} covPath
* @return {!Promise}
*/
async function maybePrintCoverageMessage(covPath = '') {
if (!argv.coverage || isCiBuild()) {
return;
}
const url = 'file://' + path.resolve(covPath);
log(green('INFO:'), 'Generated code coverage report at', cyan(url));
await open(url, {wait: false});
}
/**
* Copies frame.html to output folder, replaces js references to minified
* copies, and generates symlink to it.
*
* @param {string} input
* @param {string} outputName
* @param {!Object} options
* @return {!Promise}
*/
async function thirdPartyBootstrap(input, outputName, options) {
const {fortesting, minify} = options;
const destDir = `dist.3p/${minify ? internalRuntimeVersion : 'current'}`;
await fs.ensureDir(destDir);
if (!minify) {
await fs.copy(input, `${destDir}/${path.basename(input)}`);
return;
}
// By default we use an absolute URL, that is independent of the
// actual frame host for the JS inside the frame.
// But during testing we need a relative reference because the
// version is not available on the absolute path.
const integrationJs = fortesting
? './f.js'
: `https://${hostname3p}/${internalRuntimeVersion}/f.js`;
// Convert default relative URL to absolute min URL.
const html = fs
.readFileSync(input, 'utf8')
.replace(/\.\/integration\.js/g, integrationJs);
await fs.writeFile(`${destDir}/${outputName}`, html);
const aliasToLatestBuild = 'dist.3p/current-min';
if (fs.existsSync(aliasToLatestBuild)) {
fs.unlinkSync(aliasToLatestBuild);
}
fs.symlinkSync('./' + internalRuntimeVersion, aliasToLatestBuild, 'dir');
}
/**
*Creates directory in sync manner
*
* @param {string} path
*/
function mkdirSync(path) {
try {
fs.mkdirSync(path);
} catch (e) {
if (e.code != 'EEXIST') {
throw e;
}
}
}
/**
* Returns the list of dependencies for a given JS entrypoint by having esbuild
* generate a metafile for it. Uses the set of babel plugins that would've been
* used to compile the entrypoint.
*
* @param {string} entryPoint
* @param {!Object} options
* @return {Promise<Array<string>>}
*/
async function getDependencies(entryPoint, options) {
const caller = options.minify ? 'minified' : 'unminified';
const babelPlugin = getEsbuildBabelPlugin(caller, /* enableCache */ true);
const result = await esbuild.build({
entryPoints: [entryPoint],
bundle: true,
write: false,
metafile: true,
plugins: [babelPlugin],
});
return Object.keys(result.metafile?.inputs ?? {});
}
/**
* @param {*} sourcemapsFile
* @param {*} options
* @return {*}
*/
function massageSourcemaps(sourcemapsFile, options) {
const sourcemaps = JSON.parse(sourcemapsFile);
sourcemaps.sources = sourcemaps.sources.map((source) => {
if (source.startsWith('../')) {
return source.slice('../'.length);
}
return source;
});
sourcemaps.sourceRoot = getSourceRoot(options);
if (sourcemaps.file) {
sourcemaps.file = path.basename(sourcemaps.file);
}
if (!argv.full_sourcemaps) {
delete sourcemaps.sourcesContent;
}
return JSON.stringify(sourcemaps);
}
/**
* Returns whether or not we should compile with Closure Compiler.
* @return {boolean}
*/
function shouldUseClosure() {
// Normally setting this server-side experiment flag would be handled by
// the release process automatically. Since this experiment is actually on the build system
// itself instead of runtime, it is never run through babel (where the replacements usually happen).
// Therefore we must compute this one by hand.
return argv.define_experiment_constant !== 'ESBUILD_COMPILATION';
}
module.exports = {
bootstrapThirdPartyFrames,
compileAllJs,
compileCoreRuntime,
compileJs,
esbuildCompile,
doBuildJs,
endBuildStep,
maybePrintCoverageMessage,
maybeToEsmName,
maybeToNpmEsmName,
mkdirSync,
printConfigHelp,
printNobuildHelp,
watchDebounceDelay,
shouldUseClosure,
};