@atlaspack/runtime-js 2.14.5-canary.21 → 2.14.5-canary.211

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +277 -0
  2. package/dist/JSRuntime.js +661 -0
  3. package/lib/JSRuntime.js +80 -40
  4. package/lib/helpers/browser/analytics/analytics.d.js +1 -0
  5. package/lib/helpers/browser/css-loader.js +4 -3
  6. package/lib/helpers/browser/html-loader.js +1 -1
  7. package/lib/helpers/browser/import-polyfill.js +1 -1
  8. package/lib/helpers/browser/js-loader.js +4 -3
  9. package/lib/helpers/browser/prefetch-loader.js +1 -1
  10. package/lib/helpers/browser/preload-loader.js +1 -1
  11. package/lib/helpers/browser/sync-js-loader.js +33 -0
  12. package/lib/helpers/browser/wasm-loader.js +1 -1
  13. package/lib/helpers/cacheLoader.js +1 -1
  14. package/lib/helpers/conditional-loader-dev.js +10 -3
  15. package/lib/helpers/conditional-loader.js +15 -2
  16. package/lib/helpers/get-worker-url.js +1 -1
  17. package/lib/helpers/node/css-loader.js +1 -1
  18. package/lib/helpers/node/html-loader.js +1 -1
  19. package/lib/helpers/node/js-loader.js +1 -1
  20. package/lib/helpers/node/wasm-loader.js +1 -1
  21. package/lib/helpers/worker/js-loader.js +1 -1
  22. package/lib/helpers/worker/wasm-loader.js +1 -1
  23. package/lib/types/JSRuntime.d.ts +9 -0
  24. package/package.json +15 -10
  25. package/src/{JSRuntime.js → JSRuntime.ts} +184 -113
  26. package/src/helpers/browser/preload-loader.js +1 -2
  27. package/src/helpers/browser/sync-js-loader.js +39 -0
  28. package/src/helpers/conditional-loader-dev.js +11 -3
  29. package/src/helpers/conditional-loader.js +18 -2
  30. package/test/analytics.test.ts +4 -1
  31. package/test/{bundle-url.test.js → bundle-url.test.ts} +1 -2
  32. package/test/esm-js-loader-retry.test.ts +4 -7
  33. package/tsconfig.json +27 -0
  34. package/tsconfig.tsbuildinfo +1 -0
  35. package/src/helpers/browser/analytics/analytics.js.flow +0 -10
@@ -0,0 +1,661 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const plugin_1 = require("@atlaspack/plugin");
7
+ const utils_1 = require("@atlaspack/utils");
8
+ const diagnostic_1 = require("@atlaspack/diagnostic");
9
+ const path_1 = __importDefault(require("path"));
10
+ const nullthrows_1 = __importDefault(require("nullthrows"));
11
+ const feature_flags_1 = require("@atlaspack/feature-flags");
12
+ // Used for as="" in preload/prefetch
13
+ const TYPE_TO_RESOURCE_PRIORITY = {
14
+ css: 'style',
15
+ js: 'script',
16
+ };
17
+ const BROWSER_PRELOAD_LOADER = './helpers/browser/preload-loader';
18
+ const BROWSER_PREFETCH_LOADER = './helpers/browser/prefetch-loader';
19
+ const LOADERS = {
20
+ browser: {
21
+ css: './helpers/browser/css-loader',
22
+ html: './helpers/browser/html-loader',
23
+ js: './helpers/browser/js-loader',
24
+ wasm: './helpers/browser/wasm-loader',
25
+ IMPORT_POLYFILL: './helpers/browser/import-polyfill',
26
+ },
27
+ worker: {
28
+ js: './helpers/worker/js-loader',
29
+ wasm: './helpers/worker/wasm-loader',
30
+ IMPORT_POLYFILL: false,
31
+ },
32
+ node: {
33
+ css: './helpers/node/css-loader',
34
+ html: './helpers/node/html-loader',
35
+ js: './helpers/node/js-loader',
36
+ wasm: './helpers/node/wasm-loader',
37
+ IMPORT_POLYFILL: null,
38
+ },
39
+ };
40
+ function getLoaders(ctx) {
41
+ // @ts-expect-error TS2322
42
+ if (ctx.isWorker() || ctx.isTesseract())
43
+ return LOADERS.worker;
44
+ if (ctx.isBrowser())
45
+ return LOADERS.browser;
46
+ // @ts-expect-error TS2322
47
+ if (ctx.isNode())
48
+ return LOADERS.node;
49
+ return null;
50
+ }
51
+ // This cache should be invalidated if new dependencies get added to the bundle without the bundle objects changing
52
+ // This can happen when we reuse the BundleGraph between subsequent builds
53
+ let bundleDependencies = new WeakMap();
54
+ let defaultConfig = {
55
+ splitManifestThreshold: 100000,
56
+ };
57
+ const CONFIG_SCHEMA = {
58
+ type: 'object',
59
+ properties: {
60
+ splitManifestThreshold: {
61
+ type: 'number',
62
+ },
63
+ domainSharding: {
64
+ type: 'object',
65
+ properties: {
66
+ maxShards: {
67
+ type: 'number',
68
+ },
69
+ },
70
+ additionalProperties: false,
71
+ required: ['maxShards'],
72
+ },
73
+ },
74
+ additionalProperties: false,
75
+ };
76
+ exports.default = new plugin_1.Runtime({
77
+ async loadConfig({ config, options }) {
78
+ let packageKey = '@atlaspack/runtime-js';
79
+ let conf = await config.getConfig([], {
80
+ packageKey,
81
+ });
82
+ if (!conf) {
83
+ return defaultConfig;
84
+ }
85
+ utils_1.validateSchema.diagnostic(CONFIG_SCHEMA, {
86
+ data: conf?.contents,
87
+ source: await options.inputFS.readFile(conf.filePath, 'utf8'),
88
+ filePath: conf.filePath,
89
+ prependKey: `/${(0, diagnostic_1.encodeJSONKeyComponent)(packageKey)}`,
90
+ }, packageKey, `Invalid config for ${packageKey}`);
91
+ return {
92
+ ...defaultConfig,
93
+ ...conf?.contents,
94
+ };
95
+ },
96
+ apply({ bundle, bundleGraph, options, config }) {
97
+ // Dependency ids in code replaced with referenced bundle names
98
+ // Loader runtime added for bundle groups that don't have a native loader (e.g. HTML/CSS/Worker - isURL?),
99
+ // and which are not loaded by a parent bundle.
100
+ // Loaders also added for modules that were moved to a separate bundle because they are a different type
101
+ // (e.g. WASM, HTML). These should be preloaded prior to the bundle being executed. Replace the entry asset(s)
102
+ // with the preload module.
103
+ if (bundle.type !== 'js') {
104
+ return;
105
+ }
106
+ let { asyncDependencies, conditionalDependencies, otherDependencies } = getDependencies(bundle);
107
+ let assets = [];
108
+ for (let dependency of asyncDependencies) {
109
+ let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
110
+ if (resolved == null) {
111
+ continue;
112
+ }
113
+ if (resolved.type === 'asset') {
114
+ if (!bundle.env.shouldScopeHoist) {
115
+ // If this bundle already has the asset this dependency references,
116
+ // return a simple runtime of `Promise.resolve(internalRequire(assetId))`.
117
+ // The linker handles this for scope-hoisting.
118
+ const requireName = (0, feature_flags_1.getFeatureFlag)('hmrImprovements')
119
+ ? 'parcelRequire'
120
+ : 'module.bundle.root';
121
+ assets.push({
122
+ filePath: __filename,
123
+ code: `module.exports = Promise.resolve(${requireName}(${JSON.stringify(bundleGraph.getAssetPublicId(resolved.value))}))`,
124
+ dependency,
125
+ env: { sourceType: 'module' },
126
+ });
127
+ }
128
+ }
129
+ else {
130
+ // Resolve the dependency to a bundle. If inline, export the dependency id,
131
+ // which will be replaced with the contents of that bundle later.
132
+ let referencedBundle = bundleGraph.getReferencedBundle(dependency, bundle);
133
+ if (referencedBundle?.bundleBehavior === 'inline' ||
134
+ referencedBundle?.bundleBehavior === 'inlineIsolated') {
135
+ assets.push({
136
+ filePath: path_1.default.join(__dirname, `/bundles/${referencedBundle.id}.js`),
137
+ code: `module.exports = Promise.resolve(${JSON.stringify(dependency.id)});`,
138
+ dependency,
139
+ env: { sourceType: 'module' },
140
+ });
141
+ continue;
142
+ }
143
+ let loaderRuntime = getLoaderRuntime({
144
+ bundle,
145
+ dependency,
146
+ bundleGraph,
147
+ bundleGroup: resolved.value,
148
+ options,
149
+ shardingConfig: config.domainSharding,
150
+ });
151
+ if (loaderRuntime != null) {
152
+ assets.push(loaderRuntime);
153
+ }
154
+ }
155
+ }
156
+ if ((0, feature_flags_1.getFeatureFlag)('conditionalBundlingApi')) {
157
+ // For any conditions that are used in this bundle, we want to produce a runtime asset that is used to
158
+ // select the correct dependency that condition maps to at runtime - the conditions in the bundle will then be
159
+ // replaced with a reference to this asset to implement the selection.
160
+ const conditions = bundleGraph.getConditionsForDependencies(conditionalDependencies, bundle);
161
+ for (const cond of conditions) {
162
+ const requireName = (0, feature_flags_1.getFeatureFlag)('hmrImprovements') || bundle.env.shouldScopeHoist
163
+ ? 'parcelRequire'
164
+ : '__parcel__require__';
165
+ // @ts-expect-error TS7006
166
+ const fallbackUrls = (cond) => {
167
+ return `[${[...cond.ifTrueBundles, ...cond.ifFalseBundles]
168
+ .map((target) => {
169
+ let relativePathExpr = getRelativePathExpr(bundle, target, options);
170
+ return getAbsoluteUrlExpr(relativePathExpr, bundle, config.domainSharding);
171
+ })
172
+ .join(',')}]`;
173
+ };
174
+ const shouldUseFallback = options.mode === 'development'
175
+ ? (0, feature_flags_1.getFeatureFlag)('condbDevFallbackDev')
176
+ : (0, feature_flags_1.getFeatureFlag)('condbDevFallbackProd');
177
+ const loaderPath = `./helpers/conditional-loader${options.mode === 'development' ? '-dev' : ''}`;
178
+ const ifTrue = `function (){return ${requireName}('${cond.ifTrueAssetId}')}`;
179
+ const ifFalse = `function (){return ${requireName}('${cond.ifFalseAssetId}')}`;
180
+ const assetCode = `module.exports = require('${loaderPath}')('${cond.key}', ${ifTrue}, ${ifFalse}${shouldUseFallback
181
+ ? `, {loader: require('./helpers/browser/sync-js-loader'), urls: ${fallbackUrls(cond)}}`
182
+ : ''})`;
183
+ assets.push({
184
+ filePath: path_1.default.join(__dirname, `/conditions-${cond.publicId}.js`),
185
+ code: assetCode,
186
+ // This dependency is important, as it's the last symbol handled in scope hoisting.
187
+ // That means that scope hoisting will use the module id for this asset to replace the symbol
188
+ // (rather than the actual conditional deps)
189
+ dependency: cond.ifFalseDependency,
190
+ env: { sourceType: 'module' },
191
+ });
192
+ }
193
+ }
194
+ for (let dependency of otherDependencies) {
195
+ // Resolve the dependency to a bundle. If inline, export the dependency id,
196
+ // which will be replaced with the contents of that bundle later.
197
+ let referencedBundle = bundleGraph.getReferencedBundle(dependency, bundle);
198
+ if (referencedBundle?.bundleBehavior === 'inline' ||
199
+ referencedBundle?.bundleBehavior === 'inlineIsolated') {
200
+ assets.push({
201
+ filePath: path_1.default.join(__dirname, `/bundles/${referencedBundle.id}.js`),
202
+ code: `module.exports = ${JSON.stringify(dependency.id)};`,
203
+ dependency,
204
+ env: { sourceType: 'module' },
205
+ });
206
+ continue;
207
+ }
208
+ // Otherwise, try to resolve the dependency to an external bundle group
209
+ // and insert a URL to that bundle.
210
+ let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
211
+ if (dependency.specifierType === 'url' && resolved == null) {
212
+ // If a URL dependency was not able to be resolved, add a runtime that
213
+ // exports the original specifier.
214
+ assets.push({
215
+ filePath: __filename,
216
+ code: `module.exports = ${JSON.stringify(dependency.specifier)}`,
217
+ dependency,
218
+ env: { sourceType: 'module' },
219
+ });
220
+ continue;
221
+ }
222
+ if (resolved == null || resolved.type !== 'bundle_group') {
223
+ continue;
224
+ }
225
+ let bundleGroup = resolved.value;
226
+ let mainBundle = (0, nullthrows_1.default)(bundleGraph.getBundlesInBundleGroup(bundleGroup).find((b) => {
227
+ let entries = b.getEntryAssets();
228
+ return entries.some((e) => bundleGroup.entryAssetId === e.id);
229
+ }));
230
+ // Skip URL runtimes for library builds. This is handled in packaging so that
231
+ // the url is inlined and statically analyzable.
232
+ if (bundle.env.isLibrary &&
233
+ mainBundle.bundleBehavior !== 'isolated' &&
234
+ mainBundle.bundleBehavior !== 'inlineIsolated') {
235
+ continue;
236
+ }
237
+ // URL dependency or not, fall back to including a runtime that exports the url
238
+ assets.push(getURLRuntime(dependency, bundle, mainBundle, options, config.domainSharding));
239
+ }
240
+ // In development, bundles can be created lazily. This means that the parent bundle may not
241
+ // know about all of the sibling bundles of a child when it is written for the first time.
242
+ // Therefore, we need to also ensure that the siblings are loaded when the child loads.
243
+ if (options.shouldBuildLazily && bundle.env.outputFormat === 'global') {
244
+ let referenced = bundleGraph.getReferencedBundles(bundle);
245
+ for (let referencedBundle of referenced) {
246
+ let loaders = getLoaders(bundle.env);
247
+ if (!loaders) {
248
+ continue;
249
+ }
250
+ let loader = loaders[referencedBundle.type];
251
+ if (!loader) {
252
+ continue;
253
+ }
254
+ let relativePathExpr = getRelativePathExpr(bundle, referencedBundle, options);
255
+ let loaderCode = `require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(relativePathExpr, bundle, config.domainSharding)})`;
256
+ assets.push({
257
+ filePath: __filename,
258
+ code: loaderCode,
259
+ isEntry: true,
260
+ env: { sourceType: 'module' },
261
+ });
262
+ }
263
+ }
264
+ if (shouldUseRuntimeManifest(bundle, options) &&
265
+ bundleGraph
266
+ .getChildBundles(bundle)
267
+ .some((b) => b.bundleBehavior !== 'inline' &&
268
+ b.bundleBehavior !== 'inlineIsolated') &&
269
+ isNewContext(bundle, bundleGraph)) {
270
+ assets.push({
271
+ filePath: __filename,
272
+ code: getRegisterCode(bundle, bundleGraph),
273
+ isEntry: true,
274
+ env: { sourceType: 'module' },
275
+ runtimeAssetRequiringExecutionOnLoad: true,
276
+ priority: getManifestBundlePriority(bundleGraph, bundle, config.splitManifestThreshold),
277
+ });
278
+ }
279
+ return assets;
280
+ },
281
+ });
282
+ function getDependencies(bundle) {
283
+ let cachedDependencies = bundleDependencies.get(bundle);
284
+ if (cachedDependencies) {
285
+ return cachedDependencies;
286
+ }
287
+ else {
288
+ let asyncDependencies = [];
289
+ let otherDependencies = [];
290
+ let conditionalDependencies = [];
291
+ bundle.traverse((node) => {
292
+ if (node.type !== 'dependency') {
293
+ return;
294
+ }
295
+ let dependency = node.value;
296
+ if (dependency.priority === 'lazy' &&
297
+ dependency.specifierType !== 'url') {
298
+ asyncDependencies.push(dependency);
299
+ }
300
+ else if (dependency.priority === 'conditional') {
301
+ conditionalDependencies.push(dependency);
302
+ }
303
+ else {
304
+ otherDependencies.push(dependency);
305
+ }
306
+ });
307
+ bundleDependencies.set(bundle, {
308
+ asyncDependencies,
309
+ conditionalDependencies,
310
+ otherDependencies,
311
+ });
312
+ return { asyncDependencies, conditionalDependencies, otherDependencies };
313
+ }
314
+ }
315
+ function getLoaderRuntime({ bundle, dependency, bundleGroup, bundleGraph, options, shardingConfig, }) {
316
+ let loaders = getLoaders(bundle.env);
317
+ if (loaders == null) {
318
+ return;
319
+ }
320
+ let externalBundles = bundleGraph.getBundlesInBundleGroup(bundleGroup);
321
+ let potentialMainBundle;
322
+ if ((0, feature_flags_1.getFeatureFlag)('supportWebpackChunkName')) {
323
+ potentialMainBundle = externalBundles.find((bundle) => bundle
324
+ .getEntryAssets()
325
+ .some((asset) => asset.id === bundleGroup.entryAssetId));
326
+ }
327
+ else {
328
+ potentialMainBundle = externalBundles.find((bundle) => bundle.getMainEntry()?.id === bundleGroup.entryAssetId);
329
+ }
330
+ let mainBundle = (0, nullthrows_1.default)(potentialMainBundle);
331
+ // CommonJS is a synchronous module system, so there is no need to load bundles in parallel.
332
+ // Importing of the other bundles will be handled by the bundle group entry.
333
+ // Do the same thing in library mode for ES modules, as we are building for another bundler
334
+ // and the imports for sibling bundles will be in the target bundle.
335
+ // Previously we also did this when building lazily, however it seemed to cause issues in some cases.
336
+ // The original comment as to why is left here, in case a future traveller is trying to fix that issue:
337
+ // > [...] the runtime itself could get deduplicated and only exist in the parent. This causes errors if an
338
+ // > old version of the parent without the runtime
339
+ // > is already loaded.
340
+ if (bundle.env.outputFormat === 'commonjs' || bundle.env.isLibrary) {
341
+ externalBundles = [mainBundle];
342
+ }
343
+ else {
344
+ // Otherwise, load the bundle group entry after the others.
345
+ externalBundles.splice(externalBundles.indexOf(mainBundle), 1);
346
+ externalBundles.reverse().push(mainBundle);
347
+ }
348
+ // Determine if we need to add a dynamic import() polyfill, or if all target browsers support it natively.
349
+ let needsDynamicImportPolyfill = !bundle.env.isLibrary && !bundle.env.supports('dynamic-import', true);
350
+ let needsEsmLoadPrelude = false;
351
+ let loaderModules = [];
352
+ function getLoaderForBundle(bundle, to, shardingConfig) {
353
+ // @ts-expect-error TS18049
354
+ let loader = loaders[to.type];
355
+ if (!loader) {
356
+ return;
357
+ }
358
+ if (to.type === 'js' &&
359
+ to.env.outputFormat === 'esmodule' &&
360
+ !needsDynamicImportPolyfill &&
361
+ shouldUseRuntimeManifest(bundle, options)) {
362
+ needsEsmLoadPrelude = true;
363
+ return `load(${JSON.stringify(to.publicId)})`;
364
+ }
365
+ let relativePathExpr = getRelativePathExpr(bundle, to, options);
366
+ // Use esmodule loader if possible
367
+ if (to.type === 'js' && to.env.outputFormat === 'esmodule') {
368
+ if (!needsDynamicImportPolyfill) {
369
+ return `__parcel__import__("./" + ${relativePathExpr})`;
370
+ }
371
+ // @ts-expect-error TS2322
372
+ loader = (0, nullthrows_1.default)(
373
+ // @ts-expect-error TS18049
374
+ loaders.IMPORT_POLYFILL, `No import() polyfill available for context '${bundle.env.context}'`);
375
+ }
376
+ else if (to.type === 'js' && to.env.outputFormat === 'commonjs') {
377
+ return `Promise.resolve(__parcel__require__("./" + ${relativePathExpr}))`;
378
+ }
379
+ let absoluteUrlExpr;
380
+ if (shouldUseRuntimeManifest(bundle, options)) {
381
+ let publicId = JSON.stringify(to.publicId);
382
+ absoluteUrlExpr = `require('./helpers/bundle-manifest').resolve(${publicId})`;
383
+ if (shardingConfig) {
384
+ absoluteUrlExpr = `require('@atlaspack/domain-sharding').shardUrl(${absoluteUrlExpr}, ${shardingConfig.maxShards})`;
385
+ }
386
+ }
387
+ else {
388
+ absoluteUrlExpr = getAbsoluteUrlExpr(relativePathExpr, bundle, shardingConfig);
389
+ }
390
+ let code = `require(${JSON.stringify(loader)})(${absoluteUrlExpr})`;
391
+ // In development, clear the require cache when an error occurs so the
392
+ // user can try again (e.g. after fixing a build error).
393
+ if (options.mode === 'development' &&
394
+ bundle.env.outputFormat === 'global') {
395
+ code +=
396
+ '.catch(err => {delete module.bundle.cache[module.id]; throw err;})';
397
+ }
398
+ return code;
399
+ }
400
+ function getConditionalLoadersForCondition(dependencies, sourceBundle) {
401
+ if (dependencies.length === 0) {
402
+ // Avoid extra work if there are no dependencies, so we don't have to traverse conditions
403
+ return [];
404
+ }
405
+ // Get all the condition objects for the conditional dependencies
406
+ const conditions = bundleGraph.getConditionsForDependencies(dependencies, sourceBundle);
407
+ const loaders = [];
408
+ for (const cond of conditions) {
409
+ // This bundle has a conditional dependency, we need to load the bundle group
410
+ const ifTrueLoaders = cond.ifTrueBundles
411
+ .flatMap((targetBundle) => getConditionalLoadersForCondition(getDependencies(targetBundle).conditionalDependencies, targetBundle))
412
+ .concat(
413
+ // @ts-expect-error TS2769
414
+ cond.ifTrueBundles.map((targetBundle) =>
415
+ // @ts-expect-error TS2554
416
+ getLoaderForBundle(sourceBundle, targetBundle)));
417
+ const ifFalseLoaders = cond.ifFalseBundles
418
+ .flatMap((targetBundle) => getConditionalLoadersForCondition(getDependencies(targetBundle).conditionalDependencies, targetBundle))
419
+ .concat(
420
+ // @ts-expect-error TS2769
421
+ cond.ifFalseBundles.map((targetBundle) =>
422
+ // @ts-expect-error TS2554
423
+ getLoaderForBundle(sourceBundle, targetBundle)));
424
+ if (ifTrueLoaders.length > 0 || ifFalseLoaders.length > 0) {
425
+ // Load conditional bundles with helper (and a dev mode with additional hints)
426
+ loaders.push(`require('./helpers/conditional-loader${options.mode === 'development' ? '-dev' : ''}')('${cond.key}', function (){return Promise.all([${ifTrueLoaders.join(',')}]);}, function (){return Promise.all([${ifFalseLoaders.join(',')}]);})`);
427
+ }
428
+ }
429
+ return loaders;
430
+ }
431
+ if ((0, feature_flags_1.getFeatureFlag)('conditionalBundlingApi')) {
432
+ let conditionalDependencies = externalBundles.flatMap((to) => getDependencies(to).conditionalDependencies);
433
+ loaderModules.push(...getConditionalLoadersForCondition(conditionalDependencies, bundle));
434
+ }
435
+ for (let to of externalBundles) {
436
+ let loaderModule = getLoaderForBundle(bundle, to, shardingConfig);
437
+ if (loaderModule !== undefined)
438
+ loaderModules.push(loaderModule);
439
+ }
440
+ // Similar to the comment above, this also used to be skipped when shouldBuildLazily was true,
441
+ // however it caused issues where a bundle group contained multiple bundles.
442
+ if (bundle.env.context === 'browser') {
443
+ loaderModules.push(...externalBundles
444
+ // TODO: Allow css to preload resources as well
445
+ .filter((to) => to.type === 'js')
446
+ .flatMap((from) => {
447
+ let { preload, prefetch } = getHintedBundleGroups(bundleGraph, from);
448
+ return [
449
+ ...getHintLoaders(bundleGraph, bundle, preload, BROWSER_PRELOAD_LOADER, options),
450
+ ...getHintLoaders(bundleGraph, bundle, prefetch, BROWSER_PREFETCH_LOADER, options),
451
+ ];
452
+ }));
453
+ }
454
+ if (loaderModules.length === 0) {
455
+ return;
456
+ }
457
+ let loaderCode = loaderModules.join(', ');
458
+ if (loaderModules.length > 1) {
459
+ loaderCode = `Promise.all([${loaderCode}])`;
460
+ }
461
+ else {
462
+ loaderCode = `(${loaderCode})`;
463
+ }
464
+ if (mainBundle.type === 'js') {
465
+ let parcelRequire = (0, feature_flags_1.getFeatureFlag)('hmrImprovements') || bundle.env.shouldScopeHoist
466
+ ? 'parcelRequire'
467
+ : 'module.bundle.root';
468
+ loaderCode += `.then(() => ${parcelRequire}('${bundleGraph.getAssetPublicId(bundleGraph.getAssetById(bundleGroup.entryAssetId))}'))`;
469
+ }
470
+ if (needsEsmLoadPrelude && options.featureFlags.importRetry) {
471
+ loaderCode = `
472
+ Object.defineProperty(module, 'exports', { get: () => {
473
+ let load = require('./helpers/browser/esm-js-loader-retry');
474
+ return ${loaderCode}.then((v) => {
475
+ Object.defineProperty(module, "exports", { value: Promise.resolve(v) })
476
+ return v
477
+ });
478
+ }})`;
479
+ return {
480
+ filePath: __filename,
481
+ code: loaderCode,
482
+ dependency,
483
+ env: { sourceType: 'module' },
484
+ };
485
+ }
486
+ let code = [];
487
+ if (needsEsmLoadPrelude) {
488
+ let preludeLoad = shardingConfig
489
+ ? `let load = require('./helpers/browser/esm-js-loader-shards')(${shardingConfig.maxShards});`
490
+ : `let load = require('./helpers/browser/esm-js-loader');`;
491
+ code.push(preludeLoad);
492
+ }
493
+ code.push(`module.exports = ${loaderCode};`);
494
+ return {
495
+ filePath: __filename,
496
+ code: code.join('\n'),
497
+ dependency,
498
+ env: { sourceType: 'module' },
499
+ };
500
+ }
501
+ function getHintedBundleGroups(bundleGraph, bundle) {
502
+ let preload = [];
503
+ let prefetch = [];
504
+ let { asyncDependencies } = getDependencies(bundle);
505
+ for (let dependency of asyncDependencies) {
506
+ let attributes = dependency.meta?.importAttributes;
507
+ if (typeof attributes === 'object' &&
508
+ attributes != null &&
509
+ // @ts-expect-error TS2339
510
+ (attributes.preload || attributes.prefetch)) {
511
+ let resolved = bundleGraph.resolveAsyncDependency(dependency, bundle);
512
+ if (resolved?.type === 'bundle_group') {
513
+ // === true for flow
514
+ // @ts-expect-error TS2339
515
+ if (attributes.preload === true) {
516
+ preload.push(resolved.value);
517
+ }
518
+ // @ts-expect-error TS2339
519
+ if (attributes.prefetch === true) {
520
+ prefetch.push(resolved.value);
521
+ }
522
+ }
523
+ }
524
+ }
525
+ return { preload, prefetch };
526
+ }
527
+ function getHintLoaders(bundleGraph, from, bundleGroups, loader, options) {
528
+ let hintLoaders = [];
529
+ for (let bundleGroupToPreload of bundleGroups) {
530
+ let bundlesToPreload = bundleGraph.getBundlesInBundleGroup(bundleGroupToPreload);
531
+ for (let bundleToPreload of bundlesToPreload) {
532
+ let relativePathExpr = getRelativePathExpr(from, bundleToPreload, options);
533
+ // @ts-expect-error TS7053
534
+ let priority = TYPE_TO_RESOURCE_PRIORITY[bundleToPreload.type];
535
+ hintLoaders.push(
536
+ // @ts-expect-error TS2554
537
+ `require(${JSON.stringify(loader)})(${getAbsoluteUrlExpr(relativePathExpr, from)}, ${priority ? JSON.stringify(priority) : 'null'}, ${JSON.stringify(bundleToPreload.target.env.outputFormat === 'esmodule')})`);
538
+ }
539
+ }
540
+ return hintLoaders;
541
+ }
542
+ function isNewContext(bundle, bundleGraph) {
543
+ let parents = bundleGraph.getParentBundles(bundle);
544
+ let isInEntryBundleGroup = bundleGraph
545
+ .getBundleGroupsContainingBundle(bundle)
546
+ .some((g) => bundleGraph.isEntryBundleGroup(g));
547
+ return (isInEntryBundleGroup ||
548
+ parents.length === 0 ||
549
+ parents.some((parent) => parent.env.context !== bundle.env.context || parent.type !== 'js'));
550
+ }
551
+ function getURLRuntime(dependency, from, to, options, shardingConfig) {
552
+ let relativePathExpr;
553
+ if ((0, feature_flags_1.getFeatureFlag)('hmrImprovements')) {
554
+ relativePathExpr = getRelativePathExpr(from, to, options, true);
555
+ }
556
+ else {
557
+ relativePathExpr = getRelativePathExpr(from, to, options);
558
+ }
559
+ let code;
560
+ if (dependency.meta.webworker === true && !from.env.isLibrary) {
561
+ code = `let workerURL = require('./helpers/get-worker-url');\n`;
562
+ if (from.env.outputFormat === 'esmodule' &&
563
+ from.env.supports('import-meta-url')) {
564
+ code += `let url = new __parcel__URL__(${relativePathExpr});\n`;
565
+ code += `module.exports = workerURL(url.toString(), url.origin, ${String(from.env.outputFormat === 'esmodule')});`;
566
+ }
567
+ else {
568
+ code += `let bundleURL = require('./helpers/bundle-url');\n`;
569
+ code += `let url = bundleURL.getBundleURL('${from.publicId}') + ${relativePathExpr};`;
570
+ if (shardingConfig) {
571
+ code += `url = require('@atlaspack/domain-sharding').shardUrl(url, ${shardingConfig.maxShards});`;
572
+ }
573
+ code += `module.exports = workerURL(url, bundleURL.getOrigin(url), ${String(from.env.outputFormat === 'esmodule')});`;
574
+ }
575
+ }
576
+ else {
577
+ code = `module.exports = ${getAbsoluteUrlExpr(relativePathExpr, from, shardingConfig)};`;
578
+ }
579
+ return {
580
+ filePath: __filename,
581
+ code,
582
+ dependency,
583
+ env: { sourceType: 'module' },
584
+ };
585
+ }
586
+ function getRegisterCode(entryBundle, bundleGraph) {
587
+ // @ts-expect-error TS2304
588
+ let mappings = [];
589
+ bundleGraph.traverseBundles((bundle, _, actions) => {
590
+ if (bundle.bundleBehavior === 'inline' ||
591
+ bundle.bundleBehavior === 'inlineIsolated') {
592
+ return;
593
+ }
594
+ // To make the manifest as small as possible all bundle key/values are
595
+ // serialised into a single array e.g. ['id', 'value', 'id2', 'value2'].
596
+ // `./helpers/bundle-manifest` accounts for this by iterating index by 2
597
+ mappings.push(bundle.publicId, (0, utils_1.relativeBundlePath)(entryBundle, (0, nullthrows_1.default)(bundle), {
598
+ leadingDotSlash: false,
599
+ }));
600
+ if (bundle !== entryBundle && isNewContext(bundle, bundleGraph)) {
601
+ for (let referenced of bundleGraph.getReferencedBundles(bundle)) {
602
+ mappings.push(referenced.publicId, (0, utils_1.relativeBundlePath)(entryBundle, (0, nullthrows_1.default)(referenced), {
603
+ leadingDotSlash: false,
604
+ }));
605
+ }
606
+ // New contexts have their own manifests, so there's no need to continue.
607
+ actions.skipChildren();
608
+ }
609
+ }, entryBundle);
610
+ let baseUrl = entryBundle.env.outputFormat === 'esmodule' &&
611
+ entryBundle.env.supports('import-meta-url')
612
+ ? 'new __parcel__URL__("").toString()' // <-- this isn't ideal. We should use `import.meta.url` directly but it gets replaced currently
613
+ : `require('./helpers/bundle-url').getBundleURL('${entryBundle.publicId}')`;
614
+ return `require('./helpers/bundle-manifest').register(${baseUrl},JSON.parse(${JSON.stringify(JSON.stringify(mappings))}));`;
615
+ }
616
+ function getRelativePathExpr(from, to, options, isURL = to.type !== 'js') {
617
+ let relativePath = (0, utils_1.relativeBundlePath)(from, to, { leadingDotSlash: false });
618
+ let res = JSON.stringify(relativePath);
619
+ if ((0, feature_flags_1.getFeatureFlag)('hmrImprovements')) {
620
+ if (isURL && options.hmrOptions) {
621
+ res += ' + "?" + Date.now()';
622
+ }
623
+ }
624
+ else {
625
+ if (options.hmrOptions) {
626
+ res += ' + "?" + Date.now()';
627
+ }
628
+ }
629
+ return res;
630
+ }
631
+ function getAbsoluteUrlExpr(relativePathExpr, fromBundle, shardingConfig) {
632
+ if ((fromBundle.env.outputFormat === 'esmodule' &&
633
+ fromBundle.env.supports('import-meta-url')) ||
634
+ fromBundle.env.outputFormat === 'commonjs') {
635
+ // This will be compiled to new URL(url, import.meta.url) or new URL(url, 'file:' + __filename).
636
+ return `new __parcel__URL__(${relativePathExpr}).toString()`;
637
+ }
638
+ const regularBundleUrl = `require('./helpers/bundle-url').getBundleURL('${fromBundle.publicId}') + ${relativePathExpr}`;
639
+ if (!shardingConfig) {
640
+ return regularBundleUrl;
641
+ }
642
+ return `require('@atlaspack/domain-sharding').shardUrl(${regularBundleUrl}, ${shardingConfig.maxShards})`;
643
+ }
644
+ function shouldUseRuntimeManifest(bundle, options) {
645
+ let env = bundle.env;
646
+ return (!env.isLibrary &&
647
+ bundle.bundleBehavior !== 'inline' &&
648
+ bundle.bundleBehavior !== 'inlineIsolated' &&
649
+ env.isBrowser() &&
650
+ options.mode === 'production');
651
+ }
652
+ function getManifestBundlePriority(bundleGraph, bundle, threshold) {
653
+ let bundleSize = 0;
654
+ bundle.traverseAssets((asset, _, actions) => {
655
+ bundleSize += asset.stats.size;
656
+ if (bundleSize > threshold) {
657
+ actions.stop();
658
+ }
659
+ });
660
+ return bundleSize > threshold ? 'parallel' : 'sync';
661
+ }