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