@atlaspack/packager-js 2.14.5-canary.201 → 2.14.5-canary.203
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/dist/ESMOutputFormat.js +1 -1
- package/dist/ScopeHoistingPackager.js +43 -47
- package/lib/ESMOutputFormat.js +1 -1
- package/lib/ScopeHoistingPackager.js +41 -52
- package/lib/types/ScopeHoistingPackager.d.ts +6 -8
- package/package.json +8 -8
- package/src/ESMOutputFormat.ts +1 -1
- package/src/ScopeHoistingPackager.ts +49 -60
- package/tsconfig.tsbuildinfo +1 -1
package/dist/ESMOutputFormat.js
CHANGED
|
@@ -70,7 +70,7 @@ class ESMOutputFormat {
|
|
|
70
70
|
let lines = 0;
|
|
71
71
|
let exportSpecifiers = [];
|
|
72
72
|
for (let { asset, exportSymbol, local, exportAs, } of this.packager.exportedSymbols.values()) {
|
|
73
|
-
if (this.packager.wrappedAssets.has(asset
|
|
73
|
+
if (this.packager.wrappedAssets.has(asset)) {
|
|
74
74
|
let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
|
|
75
75
|
res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
|
|
76
76
|
lines++;
|
|
@@ -86,6 +86,7 @@ class ScopeHoistingPackager {
|
|
|
86
86
|
this.topLevelNames = new Map();
|
|
87
87
|
this.seenAssets = new Set();
|
|
88
88
|
this.wrappedAssets = new Set();
|
|
89
|
+
this.constantAssets = new Set();
|
|
89
90
|
this.hoistedRequires = new Map();
|
|
90
91
|
this.seenHoistedRequires = new Set();
|
|
91
92
|
this.needsPrelude = false;
|
|
@@ -109,7 +110,7 @@ class ScopeHoistingPackager {
|
|
|
109
110
|
this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
|
|
110
111
|
}
|
|
111
112
|
async package() {
|
|
112
|
-
|
|
113
|
+
await this.loadAssets();
|
|
113
114
|
this.buildExportedSymbols();
|
|
114
115
|
// If building a library, the target is actually another bundler rather
|
|
115
116
|
// than the final output that could be loaded in a browser. So, loader
|
|
@@ -144,23 +145,23 @@ class ScopeHoistingPackager {
|
|
|
144
145
|
if ((0, feature_flags_1.getFeatureFlag)('inlineConstOptimisationFix') ||
|
|
145
146
|
this.useBothScopeHoistingImprovements) {
|
|
146
147
|
// Write out all constant modules used by this bundle
|
|
147
|
-
for (let asset of constantAssets) {
|
|
148
|
-
if (!this.seenAssets.has(asset
|
|
148
|
+
for (let asset of this.constantAssets) {
|
|
149
|
+
if (!this.seenAssets.has(asset)) {
|
|
149
150
|
processAsset(asset);
|
|
150
151
|
}
|
|
151
152
|
}
|
|
152
153
|
}
|
|
153
154
|
// Hoist wrapped asset to the top of the bundle to ensure that they are registered
|
|
154
155
|
// before they are used.
|
|
155
|
-
for (let asset of wrappedAssets) {
|
|
156
|
-
if (!this.seenAssets.has(asset
|
|
156
|
+
for (let asset of this.wrappedAssets) {
|
|
157
|
+
if (!this.seenAssets.has(asset)) {
|
|
157
158
|
processAsset(asset);
|
|
158
159
|
}
|
|
159
160
|
}
|
|
160
161
|
// Add each asset that is directly connected to the bundle. Dependencies will be handled
|
|
161
162
|
// by replacing `import` statements in the code.
|
|
162
163
|
this.bundle.traverseAssets((asset, _, actions) => {
|
|
163
|
-
if (this.seenAssets.has(asset
|
|
164
|
+
if (this.seenAssets.has(asset)) {
|
|
164
165
|
actions.skipChildren();
|
|
165
166
|
return;
|
|
166
167
|
}
|
|
@@ -193,7 +194,7 @@ class ScopeHoistingPackager {
|
|
|
193
194
|
let needsBundleQueue = this.shouldBundleQueue(this.bundle);
|
|
194
195
|
// If any of the entry assets are wrapped, call parcelRequire so they are executed.
|
|
195
196
|
for (let entry of entries) {
|
|
196
|
-
if (this.wrappedAssets.has(entry
|
|
197
|
+
if (this.wrappedAssets.has(entry) && !this.isScriptEntry(entry)) {
|
|
197
198
|
let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
|
|
198
199
|
let entryExports = entry.symbols.get('*')?.local;
|
|
199
200
|
if (entryExports &&
|
|
@@ -222,7 +223,7 @@ class ScopeHoistingPackager {
|
|
|
222
223
|
res += '\n';
|
|
223
224
|
lineCount++;
|
|
224
225
|
let mainEntry = (0, nullthrows_1.default)(this.bundle.getMainEntry());
|
|
225
|
-
let { code, map: mapBuffer } = (0, nullthrows_1.default)(this.assetOutputs.get(mainEntry
|
|
226
|
+
let { code, map: mapBuffer } = (0, nullthrows_1.default)(this.assetOutputs.get(mainEntry));
|
|
226
227
|
let map;
|
|
227
228
|
if (mapBuffer) {
|
|
228
229
|
map = new source_map_1.default(this.options.projectRoot, mapBuffer);
|
|
@@ -295,16 +296,16 @@ class ScopeHoistingPackager {
|
|
|
295
296
|
return `$parcel$global.rwr(${params.join(', ')});`;
|
|
296
297
|
}
|
|
297
298
|
async loadAssets() {
|
|
298
|
-
let queue = new utils_1.PromiseQueue({
|
|
299
|
-
|
|
300
|
-
|
|
299
|
+
let queue = new utils_1.PromiseQueue({
|
|
300
|
+
maxConcurrent: 32,
|
|
301
|
+
});
|
|
301
302
|
this.bundle.traverseAssets((asset) => {
|
|
302
303
|
queue.add(async () => {
|
|
303
304
|
let [code, map] = await Promise.all([
|
|
304
305
|
asset.getCode(),
|
|
305
306
|
this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
|
|
306
307
|
]);
|
|
307
|
-
return [asset
|
|
308
|
+
return [asset, { code, map }];
|
|
308
309
|
});
|
|
309
310
|
if (asset.meta.shouldWrap ||
|
|
310
311
|
this.bundle.env.sourceType === 'script' ||
|
|
@@ -317,13 +318,12 @@ class ScopeHoistingPackager {
|
|
|
317
318
|
this.bundleGraph
|
|
318
319
|
.getIncomingDependencies(asset)
|
|
319
320
|
.some((dep) => dep.priority === 'lazy')) {
|
|
320
|
-
this.wrappedAssets.add(asset
|
|
321
|
-
wrapped.push(asset);
|
|
321
|
+
this.wrappedAssets.add(asset);
|
|
322
322
|
}
|
|
323
323
|
else if (((0, feature_flags_1.getFeatureFlag)('inlineConstOptimisationFix') ||
|
|
324
324
|
this.useBothScopeHoistingImprovements) &&
|
|
325
325
|
asset.meta.isConstantModule) {
|
|
326
|
-
|
|
326
|
+
this.constantAssets.add(asset);
|
|
327
327
|
}
|
|
328
328
|
}
|
|
329
329
|
});
|
|
@@ -335,18 +335,19 @@ class ScopeHoistingPackager {
|
|
|
335
335
|
if (!(0, feature_flags_1.getFeatureFlag)('applyScopeHoistingImprovementV2')) {
|
|
336
336
|
// Make all entry assets wrapped, to avoid any top level hoisting
|
|
337
337
|
for (let entryAsset of this.bundle.getEntryAssets()) {
|
|
338
|
-
if (!this.wrappedAssets.has(entryAsset
|
|
339
|
-
this.wrappedAssets.add(entryAsset
|
|
340
|
-
wrapped.push(entryAsset);
|
|
338
|
+
if (!this.wrappedAssets.has(entryAsset)) {
|
|
339
|
+
this.wrappedAssets.add(entryAsset);
|
|
341
340
|
}
|
|
342
341
|
}
|
|
343
342
|
}
|
|
344
|
-
|
|
343
|
+
// We need to make a new copy here so that we can add to the list and
|
|
344
|
+
// iterate the newly added items, without mutating the wrappedAssets set
|
|
345
|
+
let moduleGroupParents = [...this.wrappedAssets.values()];
|
|
345
346
|
if ((0, feature_flags_1.getFeatureFlag)('applyScopeHoistingImprovementV2')) {
|
|
346
347
|
// The main entry needs to be check to find assets that would have gone in
|
|
347
348
|
// the top level scope
|
|
348
349
|
let mainEntry = this.bundle.getMainEntry();
|
|
349
|
-
if (mainEntry && !this.wrappedAssets.has(mainEntry
|
|
350
|
+
if (mainEntry && !this.wrappedAssets.has(mainEntry)) {
|
|
350
351
|
moduleGroupParents.unshift(mainEntry);
|
|
351
352
|
}
|
|
352
353
|
}
|
|
@@ -355,14 +356,13 @@ class ScopeHoistingPackager {
|
|
|
355
356
|
if (asset === moduleGroupParentAsset) {
|
|
356
357
|
return;
|
|
357
358
|
}
|
|
358
|
-
if (this.wrappedAssets.has(asset
|
|
359
|
+
if (this.wrappedAssets.has(asset)) {
|
|
359
360
|
actions.skipChildren();
|
|
360
361
|
return;
|
|
361
362
|
}
|
|
362
363
|
if (!asset.meta.isConstantModule &&
|
|
363
364
|
(assignedAssets.has(asset) || this.isReExported(asset))) {
|
|
364
|
-
|
|
365
|
-
this.wrappedAssets.add(asset.id);
|
|
365
|
+
this.wrappedAssets.add(asset);
|
|
366
366
|
// This also needs to be added to the traversal so that we iterate
|
|
367
367
|
// it during this check.
|
|
368
368
|
moduleGroupParents.push(asset);
|
|
@@ -374,25 +374,22 @@ class ScopeHoistingPackager {
|
|
|
374
374
|
}
|
|
375
375
|
}
|
|
376
376
|
else {
|
|
377
|
-
for (let wrappedAssetRoot of
|
|
377
|
+
for (let wrappedAssetRoot of this.wrappedAssets) {
|
|
378
378
|
this.bundle.traverseAssets((asset, _, actions) => {
|
|
379
379
|
if (asset === wrappedAssetRoot) {
|
|
380
380
|
return;
|
|
381
381
|
}
|
|
382
|
-
if (this.wrappedAssets.has(asset
|
|
382
|
+
if (this.wrappedAssets.has(asset)) {
|
|
383
383
|
actions.skipChildren();
|
|
384
384
|
return;
|
|
385
385
|
}
|
|
386
386
|
if (!asset.meta.isConstantModule) {
|
|
387
|
-
this.wrappedAssets.add(asset
|
|
388
|
-
wrapped.push(asset);
|
|
387
|
+
this.wrappedAssets.add(asset);
|
|
389
388
|
}
|
|
390
389
|
}, wrappedAssetRoot);
|
|
391
390
|
}
|
|
392
391
|
}
|
|
393
|
-
// @ts-expect-error TS2769
|
|
394
392
|
this.assetOutputs = new Map(await queue.run());
|
|
395
|
-
return { wrapped, constant };
|
|
396
393
|
}
|
|
397
394
|
isReExported(asset) {
|
|
398
395
|
let parentSymbols = this.bundleGraph
|
|
@@ -414,7 +411,7 @@ class ScopeHoistingPackager {
|
|
|
414
411
|
}
|
|
415
412
|
// TODO: handle ESM exports of wrapped entry assets...
|
|
416
413
|
let entry = this.bundle.getMainEntry();
|
|
417
|
-
if (entry && !this.wrappedAssets.has(entry
|
|
414
|
+
if (entry && !this.wrappedAssets.has(entry)) {
|
|
418
415
|
let hasNamespace = entry.symbols.hasExportSymbol('*');
|
|
419
416
|
for (let { asset, exportAs, symbol, exportSymbol, } of this.bundleGraph.getExportedSymbols(entry)) {
|
|
420
417
|
if (typeof symbol === 'string') {
|
|
@@ -479,16 +476,16 @@ class ScopeHoistingPackager {
|
|
|
479
476
|
return `${obj}[${JSON.stringify(property)}]`;
|
|
480
477
|
}
|
|
481
478
|
visitAsset(asset) {
|
|
482
|
-
(0, assert_1.default)(!this.seenAssets.has(asset
|
|
483
|
-
this.seenAssets.add(asset
|
|
484
|
-
let { code, map } = (0, nullthrows_1.default)(this.assetOutputs.get(asset
|
|
479
|
+
(0, assert_1.default)(!this.seenAssets.has(asset), 'Already visited asset');
|
|
480
|
+
this.seenAssets.add(asset);
|
|
481
|
+
let { code, map } = (0, nullthrows_1.default)(this.assetOutputs.get(asset));
|
|
485
482
|
return this.buildAsset(asset, code, map);
|
|
486
483
|
}
|
|
487
484
|
getAssetFilePath(asset) {
|
|
488
485
|
return path_1.default.relative(this.options.projectRoot, asset.filePath);
|
|
489
486
|
}
|
|
490
487
|
buildAsset(asset, code, map) {
|
|
491
|
-
let shouldWrap = this.wrappedAssets.has(asset
|
|
488
|
+
let shouldWrap = this.wrappedAssets.has(asset);
|
|
492
489
|
let deps = this.bundleGraph.getDependencies(asset);
|
|
493
490
|
let sourceMap = this.bundle.env.sourceMap && map
|
|
494
491
|
? new source_map_1.default(this.options.projectRoot, map)
|
|
@@ -509,8 +506,7 @@ class ScopeHoistingPackager {
|
|
|
509
506
|
}
|
|
510
507
|
continue;
|
|
511
508
|
}
|
|
512
|
-
if (this.bundle.hasAsset(resolved) &&
|
|
513
|
-
!this.seenAssets.has(resolved.id)) {
|
|
509
|
+
if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved)) {
|
|
514
510
|
let [code, map, lines] = this.visitAsset(resolved);
|
|
515
511
|
depCode += code + '\n';
|
|
516
512
|
if (sourceMap && map) {
|
|
@@ -585,14 +581,14 @@ class ScopeHoistingPackager {
|
|
|
585
581
|
[res, lines] = this.getHoistedParcelRequires(asset, dep, resolved);
|
|
586
582
|
}
|
|
587
583
|
if (this.bundle.hasAsset(resolved) &&
|
|
588
|
-
!this.seenAssets.has(resolved
|
|
584
|
+
!this.seenAssets.has(resolved)) {
|
|
589
585
|
// If this asset is wrapped, we need to hoist the code for the dependency
|
|
590
586
|
// outside our parcelRequire.register wrapper. This is safe because all
|
|
591
587
|
// assets referenced by this asset will also be wrapped. Otherwise, inline the
|
|
592
588
|
// asset content where the import statement was.
|
|
593
589
|
if (this.useBothScopeHoistingImprovements) {
|
|
594
590
|
if (!resolved.meta.isConstantModule &&
|
|
595
|
-
!this.wrappedAssets.has(resolved
|
|
591
|
+
!this.wrappedAssets.has(resolved)) {
|
|
596
592
|
let [depCode, depMap, depLines] = this.visitAsset(resolved);
|
|
597
593
|
if (utils_1.debugTools['asset-file-names-in-output']) {
|
|
598
594
|
let resolvedPath = this.getAssetFilePath(resolved);
|
|
@@ -759,7 +755,7 @@ ${code}
|
|
|
759
755
|
}
|
|
760
756
|
// If this asset is wrapped, we need to replace the exports namespace with `module.exports`,
|
|
761
757
|
// which will be provided to us by the wrapper.
|
|
762
|
-
if (this.wrappedAssets.has(asset
|
|
758
|
+
if (this.wrappedAssets.has(asset) ||
|
|
763
759
|
(this.bundle.env.outputFormat === 'commonjs' &&
|
|
764
760
|
asset === this.bundle.getMainEntry())) {
|
|
765
761
|
let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
|
|
@@ -913,7 +909,7 @@ ${code}
|
|
|
913
909
|
return false;
|
|
914
910
|
}
|
|
915
911
|
return ((!this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved)) ||
|
|
916
|
-
(this.wrappedAssets.has(resolved
|
|
912
|
+
(this.wrappedAssets.has(resolved) && resolved !== parentAsset));
|
|
917
913
|
}
|
|
918
914
|
getSymbolResolution(parentAsset, resolved, imported, dep, replacements) {
|
|
919
915
|
let { asset: resolvedAsset, exportSymbol, symbol, } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
|
|
@@ -942,12 +938,12 @@ ${code}
|
|
|
942
938
|
// parcelRequire.register'ed there), or if it is indeed registered in this bundle.
|
|
943
939
|
(!this.bundle.hasAsset(resolvedAsset) ||
|
|
944
940
|
!this.shouldSkipAsset(resolvedAsset))) {
|
|
945
|
-
let hoisted = this.hoistedRequires.get(dep
|
|
941
|
+
let hoisted = this.hoistedRequires.get(dep);
|
|
946
942
|
if (!hoisted) {
|
|
947
943
|
hoisted = new Map();
|
|
948
|
-
this.hoistedRequires.set(dep
|
|
944
|
+
this.hoistedRequires.set(dep, hoisted);
|
|
949
945
|
}
|
|
950
|
-
hoisted.set(resolvedAsset
|
|
946
|
+
hoisted.set(resolvedAsset, `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`);
|
|
951
947
|
}
|
|
952
948
|
if (isWrapped) {
|
|
953
949
|
this.needsPrelude = true;
|
|
@@ -982,7 +978,7 @@ ${code}
|
|
|
982
978
|
if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
|
|
983
979
|
// Resolve to the namespace object if requested or this is a CJS default interop reqiure.
|
|
984
980
|
if (parentAsset === resolvedAsset &&
|
|
985
|
-
this.wrappedAssets.has(resolvedAsset
|
|
981
|
+
this.wrappedAssets.has(resolvedAsset)) {
|
|
986
982
|
// Directly use module.exports for wrapped assets importing themselves.
|
|
987
983
|
return 'module.exports';
|
|
988
984
|
}
|
|
@@ -1019,7 +1015,7 @@ ${code}
|
|
|
1019
1015
|
if (resolved.type !== 'js') {
|
|
1020
1016
|
return ['', 0];
|
|
1021
1017
|
}
|
|
1022
|
-
let hoisted = this.hoistedRequires.get(dep
|
|
1018
|
+
let hoisted = this.hoistedRequires.get(dep);
|
|
1023
1019
|
let res = '';
|
|
1024
1020
|
let lineCount = 0;
|
|
1025
1021
|
let isWrapped = this.isWrapped(resolved, parentAsset);
|
|
@@ -1029,7 +1025,7 @@ ${code}
|
|
|
1029
1025
|
// so it runs first.
|
|
1030
1026
|
if (isWrapped &&
|
|
1031
1027
|
!dep.meta.shouldWrap &&
|
|
1032
|
-
(!hoisted || hoisted.keys().next().value !== resolved
|
|
1028
|
+
(!hoisted || hoisted.keys().next().value !== resolved) &&
|
|
1033
1029
|
!this.bundleGraph.isDependencySkipped(dep) &&
|
|
1034
1030
|
!this.shouldSkipAsset(resolved)) {
|
|
1035
1031
|
this.needsPrelude = true;
|
|
@@ -1056,7 +1052,7 @@ ${code}
|
|
|
1056
1052
|
let prepend = '';
|
|
1057
1053
|
let prependLineCount = 0;
|
|
1058
1054
|
let append = '';
|
|
1059
|
-
let shouldWrap = this.wrappedAssets.has(asset
|
|
1055
|
+
let shouldWrap = this.wrappedAssets.has(asset);
|
|
1060
1056
|
let usedSymbols = (0, nullthrows_1.default)(this.bundleGraph.getUsedSymbols(asset));
|
|
1061
1057
|
let assetId = asset.meta.id;
|
|
1062
1058
|
(0, assert_1.default)(typeof assetId === 'string');
|
package/lib/ESMOutputFormat.js
CHANGED
|
@@ -75,7 +75,7 @@ class ESMOutputFormat {
|
|
|
75
75
|
local,
|
|
76
76
|
exportAs
|
|
77
77
|
} of this.packager.exportedSymbols.values()) {
|
|
78
|
-
if (this.packager.wrappedAssets.has(asset
|
|
78
|
+
if (this.packager.wrappedAssets.has(asset)) {
|
|
79
79
|
let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
|
|
80
80
|
res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
|
|
81
81
|
lines++;
|
|
@@ -104,6 +104,7 @@ class ScopeHoistingPackager {
|
|
|
104
104
|
topLevelNames = new Map();
|
|
105
105
|
seenAssets = new Set();
|
|
106
106
|
wrappedAssets = new Set();
|
|
107
|
+
constantAssets = new Set();
|
|
107
108
|
hoistedRequires = new Map();
|
|
108
109
|
seenHoistedRequires = new Set();
|
|
109
110
|
needsPrelude = false;
|
|
@@ -124,10 +125,7 @@ class ScopeHoistingPackager {
|
|
|
124
125
|
}
|
|
125
126
|
async package() {
|
|
126
127
|
var _sourceMap;
|
|
127
|
-
|
|
128
|
-
wrapped: wrappedAssets,
|
|
129
|
-
constant: constantAssets
|
|
130
|
-
} = await this.loadAssets();
|
|
128
|
+
await this.loadAssets();
|
|
131
129
|
this.buildExportedSymbols();
|
|
132
130
|
|
|
133
131
|
// If building a library, the target is actually another bundler rather
|
|
@@ -161,8 +159,8 @@ class ScopeHoistingPackager {
|
|
|
161
159
|
};
|
|
162
160
|
if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || this.useBothScopeHoistingImprovements) {
|
|
163
161
|
// Write out all constant modules used by this bundle
|
|
164
|
-
for (let asset of constantAssets) {
|
|
165
|
-
if (!this.seenAssets.has(asset
|
|
162
|
+
for (let asset of this.constantAssets) {
|
|
163
|
+
if (!this.seenAssets.has(asset)) {
|
|
166
164
|
processAsset(asset);
|
|
167
165
|
}
|
|
168
166
|
}
|
|
@@ -170,8 +168,8 @@ class ScopeHoistingPackager {
|
|
|
170
168
|
|
|
171
169
|
// Hoist wrapped asset to the top of the bundle to ensure that they are registered
|
|
172
170
|
// before they are used.
|
|
173
|
-
for (let asset of wrappedAssets) {
|
|
174
|
-
if (!this.seenAssets.has(asset
|
|
171
|
+
for (let asset of this.wrappedAssets) {
|
|
172
|
+
if (!this.seenAssets.has(asset)) {
|
|
175
173
|
processAsset(asset);
|
|
176
174
|
}
|
|
177
175
|
}
|
|
@@ -179,7 +177,7 @@ class ScopeHoistingPackager {
|
|
|
179
177
|
// Add each asset that is directly connected to the bundle. Dependencies will be handled
|
|
180
178
|
// by replacing `import` statements in the code.
|
|
181
179
|
this.bundle.traverseAssets((asset, _, actions) => {
|
|
182
|
-
if (this.seenAssets.has(asset
|
|
180
|
+
if (this.seenAssets.has(asset)) {
|
|
183
181
|
actions.skipChildren();
|
|
184
182
|
return;
|
|
185
183
|
}
|
|
@@ -217,7 +215,7 @@ class ScopeHoistingPackager {
|
|
|
217
215
|
|
|
218
216
|
// If any of the entry assets are wrapped, call parcelRequire so they are executed.
|
|
219
217
|
for (let entry of entries) {
|
|
220
|
-
if (this.wrappedAssets.has(entry
|
|
218
|
+
if (this.wrappedAssets.has(entry) && !this.isScriptEntry(entry)) {
|
|
221
219
|
var _entry$symbols$get;
|
|
222
220
|
let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
|
|
223
221
|
let entryExports = (_entry$symbols$get = entry.symbols.get('*')) === null || _entry$symbols$get === void 0 ? void 0 : _entry$symbols$get.local;
|
|
@@ -247,7 +245,7 @@ class ScopeHoistingPackager {
|
|
|
247
245
|
let {
|
|
248
246
|
code,
|
|
249
247
|
map: mapBuffer
|
|
250
|
-
} = (0, _nullthrows().default)(this.assetOutputs.get(mainEntry
|
|
248
|
+
} = (0, _nullthrows().default)(this.assetOutputs.get(mainEntry));
|
|
251
249
|
let map;
|
|
252
250
|
if (mapBuffer) {
|
|
253
251
|
map = new (_sourceMap2().default)(this.options.projectRoot, mapBuffer);
|
|
@@ -301,12 +299,10 @@ class ScopeHoistingPackager {
|
|
|
301
299
|
let queue = new (_utils().PromiseQueue)({
|
|
302
300
|
maxConcurrent: 32
|
|
303
301
|
});
|
|
304
|
-
let wrapped = [];
|
|
305
|
-
let constant = [];
|
|
306
302
|
this.bundle.traverseAssets(asset => {
|
|
307
303
|
queue.add(async () => {
|
|
308
304
|
let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
|
|
309
|
-
return [asset
|
|
305
|
+
return [asset, {
|
|
310
306
|
code,
|
|
311
307
|
map
|
|
312
308
|
}];
|
|
@@ -314,10 +310,9 @@ class ScopeHoistingPackager {
|
|
|
314
310
|
if (asset.meta.shouldWrap || this.bundle.env.sourceType === 'script' || this.bundleGraph.isAssetReferenced(this.bundle, asset) || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')) {
|
|
315
311
|
// Don't wrap constant "entry" modules _except_ if they are referenced by any lazy dependency
|
|
316
312
|
if (!asset.meta.isConstantModule || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.priority === 'lazy')) {
|
|
317
|
-
this.wrappedAssets.add(asset
|
|
318
|
-
wrapped.push(asset);
|
|
313
|
+
this.wrappedAssets.add(asset);
|
|
319
314
|
} else if (((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || this.useBothScopeHoistingImprovements) && asset.meta.isConstantModule) {
|
|
320
|
-
|
|
315
|
+
this.constantAssets.add(asset);
|
|
321
316
|
}
|
|
322
317
|
}
|
|
323
318
|
});
|
|
@@ -330,18 +325,20 @@ class ScopeHoistingPackager {
|
|
|
330
325
|
if (!(0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovementV2')) {
|
|
331
326
|
// Make all entry assets wrapped, to avoid any top level hoisting
|
|
332
327
|
for (let entryAsset of this.bundle.getEntryAssets()) {
|
|
333
|
-
if (!this.wrappedAssets.has(entryAsset
|
|
334
|
-
this.wrappedAssets.add(entryAsset
|
|
335
|
-
wrapped.push(entryAsset);
|
|
328
|
+
if (!this.wrappedAssets.has(entryAsset)) {
|
|
329
|
+
this.wrappedAssets.add(entryAsset);
|
|
336
330
|
}
|
|
337
331
|
}
|
|
338
332
|
}
|
|
339
|
-
|
|
333
|
+
|
|
334
|
+
// We need to make a new copy here so that we can add to the list and
|
|
335
|
+
// iterate the newly added items, without mutating the wrappedAssets set
|
|
336
|
+
let moduleGroupParents = [...this.wrappedAssets.values()];
|
|
340
337
|
if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovementV2')) {
|
|
341
338
|
// The main entry needs to be check to find assets that would have gone in
|
|
342
339
|
// the top level scope
|
|
343
340
|
let mainEntry = this.bundle.getMainEntry();
|
|
344
|
-
if (mainEntry && !this.wrappedAssets.has(mainEntry
|
|
341
|
+
if (mainEntry && !this.wrappedAssets.has(mainEntry)) {
|
|
345
342
|
moduleGroupParents.unshift(mainEntry);
|
|
346
343
|
}
|
|
347
344
|
}
|
|
@@ -350,13 +347,12 @@ class ScopeHoistingPackager {
|
|
|
350
347
|
if (asset === moduleGroupParentAsset) {
|
|
351
348
|
return;
|
|
352
349
|
}
|
|
353
|
-
if (this.wrappedAssets.has(asset
|
|
350
|
+
if (this.wrappedAssets.has(asset)) {
|
|
354
351
|
actions.skipChildren();
|
|
355
352
|
return;
|
|
356
353
|
}
|
|
357
354
|
if (!asset.meta.isConstantModule && (assignedAssets.has(asset) || this.isReExported(asset))) {
|
|
358
|
-
|
|
359
|
-
this.wrappedAssets.add(asset.id);
|
|
355
|
+
this.wrappedAssets.add(asset);
|
|
360
356
|
|
|
361
357
|
// This also needs to be added to the traversal so that we iterate
|
|
362
358
|
// it during this check.
|
|
@@ -368,29 +364,22 @@ class ScopeHoistingPackager {
|
|
|
368
364
|
}, moduleGroupParentAsset);
|
|
369
365
|
}
|
|
370
366
|
} else {
|
|
371
|
-
for (let wrappedAssetRoot of
|
|
367
|
+
for (let wrappedAssetRoot of this.wrappedAssets) {
|
|
372
368
|
this.bundle.traverseAssets((asset, _, actions) => {
|
|
373
369
|
if (asset === wrappedAssetRoot) {
|
|
374
370
|
return;
|
|
375
371
|
}
|
|
376
|
-
if (this.wrappedAssets.has(asset
|
|
372
|
+
if (this.wrappedAssets.has(asset)) {
|
|
377
373
|
actions.skipChildren();
|
|
378
374
|
return;
|
|
379
375
|
}
|
|
380
376
|
if (!asset.meta.isConstantModule) {
|
|
381
|
-
this.wrappedAssets.add(asset
|
|
382
|
-
wrapped.push(asset);
|
|
377
|
+
this.wrappedAssets.add(asset);
|
|
383
378
|
}
|
|
384
379
|
}, wrappedAssetRoot);
|
|
385
380
|
}
|
|
386
381
|
}
|
|
387
|
-
|
|
388
|
-
// @ts-expect-error TS2769
|
|
389
382
|
this.assetOutputs = new Map(await queue.run());
|
|
390
|
-
return {
|
|
391
|
-
wrapped,
|
|
392
|
-
constant
|
|
393
|
-
};
|
|
394
383
|
}
|
|
395
384
|
isReExported(asset) {
|
|
396
385
|
let parentSymbols = this.bundleGraph.getIncomingDependencies(asset).map(dep => this.bundleGraph.getAssetWithDependency(dep)).flatMap(parent => {
|
|
@@ -409,7 +398,7 @@ class ScopeHoistingPackager {
|
|
|
409
398
|
|
|
410
399
|
// TODO: handle ESM exports of wrapped entry assets...
|
|
411
400
|
let entry = this.bundle.getMainEntry();
|
|
412
|
-
if (entry && !this.wrappedAssets.has(entry
|
|
401
|
+
if (entry && !this.wrappedAssets.has(entry)) {
|
|
413
402
|
let hasNamespace = entry.symbols.hasExportSymbol('*');
|
|
414
403
|
for (let {
|
|
415
404
|
asset,
|
|
@@ -478,19 +467,19 @@ class ScopeHoistingPackager {
|
|
|
478
467
|
return `${obj}[${JSON.stringify(property)}]`;
|
|
479
468
|
}
|
|
480
469
|
visitAsset(asset) {
|
|
481
|
-
(0, _assert().default)(!this.seenAssets.has(asset
|
|
482
|
-
this.seenAssets.add(asset
|
|
470
|
+
(0, _assert().default)(!this.seenAssets.has(asset), 'Already visited asset');
|
|
471
|
+
this.seenAssets.add(asset);
|
|
483
472
|
let {
|
|
484
473
|
code,
|
|
485
474
|
map
|
|
486
|
-
} = (0, _nullthrows().default)(this.assetOutputs.get(asset
|
|
475
|
+
} = (0, _nullthrows().default)(this.assetOutputs.get(asset));
|
|
487
476
|
return this.buildAsset(asset, code, map);
|
|
488
477
|
}
|
|
489
478
|
getAssetFilePath(asset) {
|
|
490
479
|
return _path().default.relative(this.options.projectRoot, asset.filePath);
|
|
491
480
|
}
|
|
492
481
|
buildAsset(asset, code, map) {
|
|
493
|
-
let shouldWrap = this.wrappedAssets.has(asset
|
|
482
|
+
let shouldWrap = this.wrappedAssets.has(asset);
|
|
494
483
|
let deps = this.bundleGraph.getDependencies(asset);
|
|
495
484
|
let sourceMap = this.bundle.env.sourceMap && map ? new (_sourceMap2().default)(this.options.projectRoot, map) : null;
|
|
496
485
|
|
|
@@ -510,7 +499,7 @@ class ScopeHoistingPackager {
|
|
|
510
499
|
}
|
|
511
500
|
continue;
|
|
512
501
|
}
|
|
513
|
-
if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved
|
|
502
|
+
if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved)) {
|
|
514
503
|
let [code, map, lines] = this.visitAsset(resolved);
|
|
515
504
|
depCode += code + '\n';
|
|
516
505
|
if (sourceMap && map) {
|
|
@@ -584,13 +573,13 @@ class ScopeHoistingPackager {
|
|
|
584
573
|
if (!(0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovementV2')) {
|
|
585
574
|
[res, lines] = this.getHoistedParcelRequires(asset, dep, resolved);
|
|
586
575
|
}
|
|
587
|
-
if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved
|
|
576
|
+
if (this.bundle.hasAsset(resolved) && !this.seenAssets.has(resolved)) {
|
|
588
577
|
// If this asset is wrapped, we need to hoist the code for the dependency
|
|
589
578
|
// outside our parcelRequire.register wrapper. This is safe because all
|
|
590
579
|
// assets referenced by this asset will also be wrapped. Otherwise, inline the
|
|
591
580
|
// asset content where the import statement was.
|
|
592
581
|
if (this.useBothScopeHoistingImprovements) {
|
|
593
|
-
if (!resolved.meta.isConstantModule && !this.wrappedAssets.has(resolved
|
|
582
|
+
if (!resolved.meta.isConstantModule && !this.wrappedAssets.has(resolved)) {
|
|
594
583
|
let [depCode, depMap, depLines] = this.visitAsset(resolved);
|
|
595
584
|
if (_utils().debugTools['asset-file-names-in-output']) {
|
|
596
585
|
let resolvedPath = this.getAssetFilePath(resolved);
|
|
@@ -747,7 +736,7 @@ ${code}
|
|
|
747
736
|
|
|
748
737
|
// If this asset is wrapped, we need to replace the exports namespace with `module.exports`,
|
|
749
738
|
// which will be provided to us by the wrapper.
|
|
750
|
-
if (this.wrappedAssets.has(asset
|
|
739
|
+
if (this.wrappedAssets.has(asset) || this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry()) {
|
|
751
740
|
var _asset$symbols$get;
|
|
752
741
|
let exportsName = ((_asset$symbols$get = asset.symbols.get('*')) === null || _asset$symbols$get === void 0 ? void 0 : _asset$symbols$get.local) || `$${assetId}$exports`;
|
|
753
742
|
replacements.set(exportsName, 'module.exports');
|
|
@@ -888,7 +877,7 @@ ${code}
|
|
|
888
877
|
}
|
|
889
878
|
return false;
|
|
890
879
|
}
|
|
891
|
-
return !this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved) || this.wrappedAssets.has(resolved
|
|
880
|
+
return !this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved) || this.wrappedAssets.has(resolved) && resolved !== parentAsset;
|
|
892
881
|
}
|
|
893
882
|
getSymbolResolution(parentAsset, resolved, imported, dep, replacements) {
|
|
894
883
|
let {
|
|
@@ -915,12 +904,12 @@ ${code}
|
|
|
915
904
|
// Only do this if the asset is part of a different bundle (so it was definitely
|
|
916
905
|
// parcelRequire.register'ed there), or if it is indeed registered in this bundle.
|
|
917
906
|
!this.bundle.hasAsset(resolvedAsset) || !this.shouldSkipAsset(resolvedAsset))) {
|
|
918
|
-
let hoisted = this.hoistedRequires.get(dep
|
|
907
|
+
let hoisted = this.hoistedRequires.get(dep);
|
|
919
908
|
if (!hoisted) {
|
|
920
909
|
hoisted = new Map();
|
|
921
|
-
this.hoistedRequires.set(dep
|
|
910
|
+
this.hoistedRequires.set(dep, hoisted);
|
|
922
911
|
}
|
|
923
|
-
hoisted.set(resolvedAsset
|
|
912
|
+
hoisted.set(resolvedAsset, `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`);
|
|
924
913
|
}
|
|
925
914
|
if (isWrapped) {
|
|
926
915
|
this.needsPrelude = true;
|
|
@@ -949,7 +938,7 @@ ${code}
|
|
|
949
938
|
}
|
|
950
939
|
if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
|
|
951
940
|
// Resolve to the namespace object if requested or this is a CJS default interop reqiure.
|
|
952
|
-
if (parentAsset === resolvedAsset && this.wrappedAssets.has(resolvedAsset
|
|
941
|
+
if (parentAsset === resolvedAsset && this.wrappedAssets.has(resolvedAsset)) {
|
|
953
942
|
// Directly use module.exports for wrapped assets importing themselves.
|
|
954
943
|
return 'module.exports';
|
|
955
944
|
} else {
|
|
@@ -977,7 +966,7 @@ ${code}
|
|
|
977
966
|
if (resolved.type !== 'js') {
|
|
978
967
|
return ['', 0];
|
|
979
968
|
}
|
|
980
|
-
let hoisted = this.hoistedRequires.get(dep
|
|
969
|
+
let hoisted = this.hoistedRequires.get(dep);
|
|
981
970
|
let res = '';
|
|
982
971
|
let lineCount = 0;
|
|
983
972
|
let isWrapped = this.isWrapped(resolved, parentAsset);
|
|
@@ -986,7 +975,7 @@ ${code}
|
|
|
986
975
|
// we need to run side effects when this asset runs. If the resolved asset is not
|
|
987
976
|
// the first one in the hoisted requires, we need to insert a parcelRequire here
|
|
988
977
|
// so it runs first.
|
|
989
|
-
if (isWrapped && !dep.meta.shouldWrap && (!hoisted || hoisted.keys().next().value !== resolved
|
|
978
|
+
if (isWrapped && !dep.meta.shouldWrap && (!hoisted || hoisted.keys().next().value !== resolved) && !this.bundleGraph.isDependencySkipped(dep) && !this.shouldSkipAsset(resolved)) {
|
|
990
979
|
this.needsPrelude = true;
|
|
991
980
|
res += `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(resolved))});`;
|
|
992
981
|
}
|
|
@@ -1010,7 +999,7 @@ ${code}
|
|
|
1010
999
|
let prepend = '';
|
|
1011
1000
|
let prependLineCount = 0;
|
|
1012
1001
|
let append = '';
|
|
1013
|
-
let shouldWrap = this.wrappedAssets.has(asset
|
|
1002
|
+
let shouldWrap = this.wrappedAssets.has(asset);
|
|
1014
1003
|
let usedSymbols = (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(asset));
|
|
1015
1004
|
let assetId = asset.meta.id;
|
|
1016
1005
|
(0, _assert().default)(typeof assetId === 'string');
|
|
@@ -13,7 +13,7 @@ export declare class ScopeHoistingPackager {
|
|
|
13
13
|
outputFormat: OutputFormat;
|
|
14
14
|
isAsyncBundle: boolean;
|
|
15
15
|
globalNames: ReadonlySet<string>;
|
|
16
|
-
assetOutputs: Map<
|
|
16
|
+
assetOutputs: Map<Asset, {
|
|
17
17
|
code: string;
|
|
18
18
|
map: Buffer | null | undefined;
|
|
19
19
|
}>;
|
|
@@ -25,9 +25,10 @@ export declare class ScopeHoistingPackager {
|
|
|
25
25
|
}>;
|
|
26
26
|
externals: Map<string, Map<string, string>>;
|
|
27
27
|
topLevelNames: Map<string, number>;
|
|
28
|
-
seenAssets: Set<
|
|
29
|
-
wrappedAssets: Set<
|
|
30
|
-
|
|
28
|
+
seenAssets: Set<Asset>;
|
|
29
|
+
wrappedAssets: Set<Asset>;
|
|
30
|
+
constantAssets: Set<Asset>;
|
|
31
|
+
hoistedRequires: Map<Dependency, Map<Asset, string>>;
|
|
31
32
|
seenHoistedRequires: Set<string>;
|
|
32
33
|
needsPrelude: boolean;
|
|
33
34
|
usedHelpers: Set<string>;
|
|
@@ -41,10 +42,7 @@ export declare class ScopeHoistingPackager {
|
|
|
41
42
|
}>;
|
|
42
43
|
shouldBundleQueue(bundle: NamedBundle): boolean;
|
|
43
44
|
runWhenReady(bundle: NamedBundle, codeToRun: string): string;
|
|
44
|
-
loadAssets(): Promise<
|
|
45
|
-
wrapped: Array<Asset>;
|
|
46
|
-
constant: Array<Asset>;
|
|
47
|
-
}>;
|
|
45
|
+
loadAssets(): Promise<void>;
|
|
48
46
|
isReExported(asset: Asset): boolean;
|
|
49
47
|
buildExportedSymbols(): void;
|
|
50
48
|
getTopLevelName(name: string): string;
|