-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
JSRuntime.js
698 lines (627 loc) Β· 20.1 KB
/
JSRuntime.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
// @flow strict-local
import type {
BundleGraph,
BundleGroup,
Dependency,
Environment,
PluginOptions,
NamedBundle,
RuntimeAsset,
} from '@parcel/types';
import {Runtime} from '@parcel/plugin';
import {
relativeBundlePath,
validateSchema,
type SchemaEntity,
} from '@parcel/utils';
import {encodeJSONKeyComponent} from '@parcel/diagnostic';
import path from 'path';
import nullthrows from 'nullthrows';
// Used for as="" in preload/prefetch
const TYPE_TO_RESOURCE_PRIORITY = {
css: 'style',
js: 'script',
};
const BROWSER_PRELOAD_LOADER = './helpers/browser/preload-loader';
const BROWSER_PREFETCH_LOADER = './helpers/browser/prefetch-loader';
const LOADERS = {
browser: {
css: './helpers/browser/css-loader',
html: './helpers/browser/html-loader',
js: './helpers/browser/js-loader',
wasm: './helpers/browser/wasm-loader',
IMPORT_POLYFILL: './helpers/browser/import-polyfill',
},
worker: {
js: './helpers/worker/js-loader',
wasm: './helpers/worker/wasm-loader',
IMPORT_POLYFILL: false,
},
node: {
css: './helpers/node/css-loader',
html: './helpers/node/html-loader',
js: './helpers/node/js-loader',
wasm: './helpers/node/wasm-loader',
IMPORT_POLYFILL: null,
},
};
function getLoaders(
ctx: Environment,
): ?{[string]: string, IMPORT_POLYFILL: null | false | string, ...} {
if (ctx.isWorker()) return LOADERS.worker;
if (ctx.isBrowser()) return LOADERS.browser;
if (ctx.isNode()) return LOADERS.node;
return null;
}
// This cache should be invalidated if new dependencies get added to the bundle without the bundle objects changing
// This can happen when we reuse the BundleGraph between subsequent builds
let bundleDependencies = new WeakMap<
NamedBundle,
{|
asyncDependencies: Array<Dependency>,
otherDependencies: Array<Dependency>,
|},
>();
type JSRuntimeConfig = {|
splitManifestThreshold: number,
|};
let defaultConfig: JSRuntimeConfig = {
splitManifestThreshold: 100000,
};
const CONFIG_SCHEMA: SchemaEntity = {
type: 'object',
properties: {
splitManifestThreshold: {
type: 'number',
},
},
additionalProperties: false,
};
export default (new Runtime({
async loadConfig({config, options}): Promise<JSRuntimeConfig> {
let packageKey = '@parcel/runtime-js';
let conf = await config.getConfig<JSRuntimeConfig>([], {
packageKey,
});
if (!conf) {
return defaultConfig;
}
validateSchema.diagnostic(
CONFIG_SCHEMA,
{
data: conf?.contents,
source: await options.inputFS.readFile(conf.filePath, 'utf8'),
filePath: conf.filePath,
prependKey: `/${encodeJSONKeyComponent(packageKey)}`,
},
packageKey,
`Invalid config for ${packageKey}`,
);
return {
...defaultConfig,
...conf?.contents,
};
},
apply({bundle, bundleGraph, options, config}) {
// Dependency ids in code replaced with referenced bundle names
// Loader runtime added for bundle groups that don't have a native loader (e.g. HTML/CSS/Worker - isURL?),
// and which are not loaded by a parent bundle.
// Loaders also added for modules that were moved to a separate bundle because they are a different type
// (e.g. WASM, HTML). These should be preloaded prior to the bundle being executed. Replace the entry asset(s)
// with the preload module.
if (bundle.type !== 'js') {
return;
}
let {asyncDependencies, otherDependencies} = getDependencies(bundle);
let assets = [];
for (let dependency of asyncDependencies) {
let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
if (resolved == null) {
continue;
}
if (resolved.type === 'asset') {
if (!bundle.env.shouldScopeHoist) {
// If this bundle already has the asset this dependency references,
// return a simple runtime of `Promise.resolve(internalRequire(assetId))`.
// The linker handles this for scope-hoisting.
assets.push({
filePath: __filename,
code: `module.exports = Promise.resolve(module.bundle.root(${JSON.stringify(
bundleGraph.getAssetPublicId(resolved.value),
)}))`,
dependency,
env: {sourceType: 'module'},
});
}
} else {
// Resolve the dependency to a bundle. If inline, export the dependency id,
// which will be replaced with the contents of that bundle later.
let referencedBundle = bundleGraph.getReferencedBundle(
dependency,
bundle,
);
if (referencedBundle?.bundleBehavior === 'inline') {
assets.push({
filePath: path.join(
__dirname,
`/bundles/${referencedBundle.id}.js`,
),
code: `module.exports = Promise.resolve(${JSON.stringify(
dependency.id,
)});`,
dependency,
env: {sourceType: 'module'},
});
continue;
}
let loaderRuntime = getLoaderRuntime({
bundle,
dependency,
bundleGraph,
bundleGroup: resolved.value,
options,
});
if (loaderRuntime != null) {
assets.push(loaderRuntime);
}
}
}
for (let dependency of otherDependencies) {
// Resolve the dependency to a bundle. If inline, export the dependency id,
// which will be replaced with the contents of that bundle later.
let referencedBundle = bundleGraph.getReferencedBundle(
dependency,
bundle,
);
if (referencedBundle?.bundleBehavior === 'inline') {
assets.push({
filePath: path.join(__dirname, `/bundles/${referencedBundle.id}.js`),
code: `module.exports = ${JSON.stringify(dependency.id)};`,
dependency,
env: {sourceType: 'module'},
});
continue;
}
// Otherwise, try to resolve the dependency to an external bundle group
// and insert a URL to that bundle.
let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
if (dependency.specifierType === 'url' && resolved == null) {
// If a URL dependency was not able to be resolved, add a runtime that
// exports the original specifier.
assets.push({
filePath: __filename,
code: `module.exports = ${JSON.stringify(dependency.specifier)}`,
dependency,
env: {sourceType: 'module'},
});
continue;
}
if (resolved == null || resolved.type !== 'bundle_group') {
continue;
}
let bundleGroup = resolved.value;
let mainBundle = nullthrows(
bundleGraph.getBundlesInBundleGroup(bundleGroup).find(b => {
let entries = b.getEntryAssets();
return entries.some(e => bundleGroup.entryAssetId === e.id);
}),
);
// Skip URL runtimes for library builds. This is handled in packaging so that
// the url is inlined and statically analyzable.
if (bundle.env.isLibrary && dependency.meta?.placeholder != null) {
continue;
}
// URL dependency or not, fall back to including a runtime that exports the url
assets.push(getURLRuntime(dependency, bundle, mainBundle, options));
}
// In development, bundles can be created lazily. This means that the parent bundle may not
// know about all of the sibling bundles of a child when it is written for the first time.
// Therefore, we need to also ensure that the siblings are loaded when the child loads.
if (options.shouldBuildLazily && bundle.env.outputFormat === 'global') {
let referenced = bundleGraph.getReferencedBundles(bundle);
for (let referencedBundle of referenced) {
let loaders = getLoaders(bundle.env);
if (!loaders) {
continue;
}
let loader = loaders[referencedBundle.type];
if (!loader) {
continue;
}
let relativePathExpr = getRelativePathExpr(
bundle,
referencedBundle,
options,
);
let loaderCode = `require(${JSON.stringify(
loader,
)})( ${getAbsoluteUrlExpr(relativePathExpr, bundle)})`;
assets.push({
filePath: __filename,
code: loaderCode,
isEntry: true,
env: {sourceType: 'module'},
});
}
}
if (
shouldUseRuntimeManifest(bundle, options) &&
bundleGraph
.getChildBundles(bundle)
.some(b => b.bundleBehavior !== 'inline') &&
isNewContext(bundle, bundleGraph)
) {
assets.push({
filePath: __filename,
code: getRegisterCode(bundle, bundleGraph),
isEntry: true,
env: {sourceType: 'module'},
priority: getManifestBundlePriority(
bundleGraph,
bundle,
config.splitManifestThreshold,
),
});
}
return assets;
},
}): Runtime);
function getDependencies(bundle: NamedBundle): {|
asyncDependencies: Array<Dependency>,
otherDependencies: Array<Dependency>,
|} {
let cachedDependencies = bundleDependencies.get(bundle);
if (cachedDependencies) {
return cachedDependencies;
} else {
let asyncDependencies = [];
let otherDependencies = [];
bundle.traverse(node => {
if (node.type !== 'dependency') {
return;
}
let dependency = node.value;
if (
dependency.priority === 'lazy' &&
dependency.specifierType !== 'url'
) {
asyncDependencies.push(dependency);
} else {
otherDependencies.push(dependency);
}
});
bundleDependencies.set(bundle, {asyncDependencies, otherDependencies});
return {asyncDependencies, otherDependencies};
}
}
function getLoaderRuntime({
bundle,
dependency,
bundleGroup,
bundleGraph,
options,
}: {|
bundle: NamedBundle,
dependency: Dependency,
bundleGroup: BundleGroup,
bundleGraph: BundleGraph<NamedBundle>,
options: PluginOptions,
|}): ?RuntimeAsset {
let loaders = getLoaders(bundle.env);
if (loaders == null) {
return;
}
let externalBundles = bundleGraph.getBundlesInBundleGroup(bundleGroup);
let mainBundle = nullthrows(
externalBundles.find(
bundle => bundle.getMainEntry()?.id === bundleGroup.entryAssetId,
),
);
// CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
// Importing of the other bundles will be handled by the bundle group entry.
// Do the same thing in library mode for ES modules, as we are building for another bundler
// and the imports for sibling bundles will be in the target bundle.
// Also do this when building lazily or the runtime itself could get deduplicated and only
// exist in the parent. This causes errors if an old version of the parent without the runtime
// is already loaded.
if (
bundle.env.outputFormat === 'commonjs' ||
bundle.env.isLibrary ||
options.shouldBuildLazily
) {
externalBundles = [mainBundle];
} else {
// Otherwise, load the bundle group entry after the others.
externalBundles.splice(externalBundles.indexOf(mainBundle), 1);
externalBundles.reverse().push(mainBundle);
}
// Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
let needsDynamicImportPolyfill =
!bundle.env.isLibrary && !bundle.env.supports('dynamic-import', true);
let loaderModules = externalBundles
.map(to => {
let loader = loaders[to.type];
if (!loader) {
return;
}
let relativePathExpr = getRelativePathExpr(bundle, to, options);
// Use esmodule loader if possible
if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
if (!needsDynamicImportPolyfill) {
return `__parcel__import__("./" + ${relativePathExpr})`;
}
loader = nullthrows(
loaders.IMPORT_POLYFILL,
`No import() polyfill available for context '${bundle.env.context}'`,
);
} else if (to.type === 'js' && to.env.outputFormat === 'commonjs') {
return `Promise.resolve(__parcel__require__("./" + ${relativePathExpr}))`;
}
let code = `require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(
relativePathExpr,
bundle,
)})`;
// In development, clear the require cache when an error occurs so the
// user can try again (e.g. after fixing a build error).
if (
options.mode === 'development' &&
bundle.env.outputFormat === 'global'
) {
code +=
'.catch(err => {delete module.bundle.cache[module.id]; throw err;})';
}
return code;
})
.filter(Boolean);
if (bundle.env.context === 'browser' && !options.shouldBuildLazily) {
loaderModules.push(
...externalBundles
// TODO: Allow css to preload resources as well
.filter(to => to.type === 'js')
.flatMap(from => {
let {preload, prefetch} = getHintedBundleGroups(bundleGraph, from);
return [
...getHintLoaders(
bundleGraph,
bundle,
preload,
BROWSER_PRELOAD_LOADER,
options,
),
...getHintLoaders(
bundleGraph,
bundle,
prefetch,
BROWSER_PREFETCH_LOADER,
options,
),
];
}),
);
}
if (loaderModules.length === 0) {
return;
}
let loaderCode = loaderModules.join(', ');
if (loaderModules.length > 1) {
loaderCode = `Promise.all([${loaderCode}])`;
} else {
loaderCode = `(${loaderCode})`;
}
if (mainBundle.type === 'js') {
let parcelRequire = bundle.env.shouldScopeHoist
? 'parcelRequire'
: 'module.bundle.root';
loaderCode += `.then(() => ${parcelRequire}('${bundleGraph.getAssetPublicId(
bundleGraph.getAssetById(bundleGroup.entryAssetId),
)}'))`;
}
return {
filePath: __filename,
code: `module.exports = ${loaderCode};`,
dependency,
env: {sourceType: 'module'},
};
}
function getHintedBundleGroups(
bundleGraph: BundleGraph<NamedBundle>,
bundle: NamedBundle,
): {|preload: Array<BundleGroup>, prefetch: Array<BundleGroup>|} {
let preload = [];
let prefetch = [];
let {asyncDependencies} = getDependencies(bundle);
for (let dependency of asyncDependencies) {
let attributes = dependency.meta?.importAttributes;
if (
typeof attributes === 'object' &&
attributes != null &&
// $FlowFixMe
(attributes.preload || attributes.prefetch)
) {
let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
if (resolved?.type === 'bundle_group') {
// === true for flow
if (attributes.preload === true) {
preload.push(resolved.value);
}
if (attributes.prefetch === true) {
prefetch.push(resolved.value);
}
}
}
}
return {preload, prefetch};
}
function getHintLoaders(
bundleGraph: BundleGraph<NamedBundle>,
from: NamedBundle,
bundleGroups: Array<BundleGroup>,
loader: string,
options: PluginOptions,
): Array<string> {
let hintLoaders = [];
for (let bundleGroupToPreload of bundleGroups) {
let bundlesToPreload =
bundleGraph.getBundlesInBundleGroup(bundleGroupToPreload);
for (let bundleToPreload of bundlesToPreload) {
let relativePathExpr = getRelativePathExpr(
from,
bundleToPreload,
options,
);
let priority = TYPE_TO_RESOURCE_PRIORITY[bundleToPreload.type];
hintLoaders.push(
`require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(
relativePathExpr,
from,
)}, ${priority ? JSON.stringify(priority) : 'null'}, ${JSON.stringify(
bundleToPreload.target.env.outputFormat === 'esmodule',
)})`,
);
}
}
return hintLoaders;
}
function isNewContext(
bundle: NamedBundle,
bundleGraph: BundleGraph<NamedBundle>,
): boolean {
let parents = bundleGraph.getParentBundles(bundle);
let isInEntryBundleGroup = bundleGraph
.getBundleGroupsContainingBundle(bundle)
.some(g => bundleGraph.isEntryBundleGroup(g));
return (
isInEntryBundleGroup ||
parents.length === 0 ||
parents.some(
parent =>
parent.env.context !== bundle.env.context || parent.type !== 'js',
)
);
}
function getURLRuntime(
dependency: Dependency,
from: NamedBundle,
to: NamedBundle,
options: PluginOptions,
): RuntimeAsset {
let relativePathExpr = getRelativePathExpr(from, to, options);
let code;
if (dependency.meta.webworker === true && !from.env.isLibrary) {
code = `let workerURL = require('./helpers/get-worker-url');\n`;
if (
from.env.outputFormat === 'esmodule' &&
from.env.supports('import-meta-url')
) {
code += `let url = new __parcel__URL__(${relativePathExpr});\n`;
code += `module.exports = workerURL(url.toString(), url.origin, ${String(
from.env.outputFormat === 'esmodule',
)});`;
} else {
code += `let bundleURL = require('./helpers/bundle-url');\n`;
code += `let url = bundleURL.getBundleURL('${from.publicId}') + ${relativePathExpr};`;
code += `module.exports = workerURL(url, bundleURL.getOrigin(url), ${String(
from.env.outputFormat === 'esmodule',
)});`;
}
} else {
code = `module.exports = ${getAbsoluteUrlExpr(relativePathExpr, from)};`;
}
return {
filePath: __filename,
code,
dependency,
env: {sourceType: 'module'},
};
}
function getRegisterCode(
entryBundle: NamedBundle,
bundleGraph: BundleGraph<NamedBundle>,
): string {
let idToName = {};
bundleGraph.traverseBundles((bundle, _, actions) => {
if (bundle.bundleBehavior === 'inline') {
return;
}
idToName[bundle.publicId] = path.basename(nullthrows(bundle.name));
if (bundle !== entryBundle && isNewContext(bundle, bundleGraph)) {
for (let referenced of bundleGraph.getReferencedBundles(bundle)) {
idToName[referenced.publicId] = path.basename(
nullthrows(referenced.name),
);
}
// New contexts have their own manifests, so there's no need to continue.
actions.skipChildren();
}
}, entryBundle);
return (
"require('./helpers/bundle-manifest').register(JSON.parse(" +
JSON.stringify(JSON.stringify(idToName)) +
'));'
);
}
function getRelativePathExpr(
from: NamedBundle,
to: NamedBundle,
options: PluginOptions,
): string {
let relativePath = relativeBundlePath(from, to, {leadingDotSlash: false});
if (shouldUseRuntimeManifest(from, options)) {
// Get the relative part of the path. This part is not in the manifest, only the basename is.
let relativeBase = path.posix.dirname(relativePath);
if (relativeBase === '.') {
relativeBase = '';
} else {
relativeBase = `${JSON.stringify(relativeBase + '/')} + `;
}
return (
relativeBase +
`require('./helpers/bundle-manifest').resolve(${JSON.stringify(
to.publicId,
)})`
);
}
let res = JSON.stringify(relativePath);
if (options.hmrOptions) {
res += ' + "?" + Date.now()';
}
return res;
}
function getAbsoluteUrlExpr(relativePathExpr: string, bundle: NamedBundle) {
if (
(bundle.env.outputFormat === 'esmodule' &&
bundle.env.supports('import-meta-url')) ||
bundle.env.outputFormat === 'commonjs'
) {
// This will be compiled to new URL(url, import.meta.url) or new URL(url, 'file:' + __filename).
return `new __parcel__URL__(${relativePathExpr}).toString()`;
} else {
return `require('./helpers/bundle-url').getBundleURL('${bundle.publicId}') + ${relativePathExpr}`;
}
}
function shouldUseRuntimeManifest(
bundle: NamedBundle,
options: PluginOptions,
): boolean {
let env = bundle.env;
return (
!env.isLibrary &&
bundle.bundleBehavior !== 'inline' &&
env.isBrowser() &&
options.mode === 'production'
);
}
function getManifestBundlePriority(
bundleGraph: BundleGraph<NamedBundle>,
bundle: NamedBundle,
threshold: number,
): $PropertyType<RuntimeAsset, 'priority'> {
let bundleSize = 0;
bundle.traverseAssets((asset, _, actions) => {
bundleSize += asset.stats.size;
if (bundleSize > threshold) {
actions.stop();
}
});
return bundleSize > threshold ? 'parallel' : 'sync';
}