@atlaspack/packager-js 2.14.5-canary.14 → 2.14.5-canary.140
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/CHANGELOG.md +208 -0
- package/lib/CJSOutputFormat.d.ts +7 -0
- package/lib/DevPackager.d.ts +15 -0
- package/lib/DevPackager.js +26 -1
- package/lib/ESMOutputFormat.d.ts +7 -0
- package/lib/GlobalOutputFormat.d.ts +7 -0
- package/lib/ScopeHoistingPackager.d.ts +65 -0
- package/lib/ScopeHoistingPackager.js +259 -71
- package/lib/dev-prelude.js +6 -6
- package/lib/helpers.d.ts +11 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +5 -10
- package/lib/utils.d.ts +6 -0
- package/package.json +17 -12
- package/src/{CJSOutputFormat.js → CJSOutputFormat.ts} +0 -1
- package/src/{DevPackager.js → DevPackager.ts} +31 -5
- package/src/{ESMOutputFormat.js → ESMOutputFormat.ts} +2 -3
- package/src/{GlobalOutputFormat.js → GlobalOutputFormat.ts} +0 -1
- package/src/{ScopeHoistingPackager.js → ScopeHoistingPackager.ts} +283 -100
- package/src/dev-prelude.js +6 -6
- package/src/{helpers.js → helpers.ts} +1 -2
- package/src/{index.js → index.ts} +13 -20
- package/src/{utils.js → utils.ts} +1 -2
- package/tsconfig.json +4 -0
|
@@ -60,6 +60,13 @@ function _featureFlags() {
|
|
|
60
60
|
};
|
|
61
61
|
return data;
|
|
62
62
|
}
|
|
63
|
+
function _outdent() {
|
|
64
|
+
const data = require("outdent");
|
|
65
|
+
_outdent = function () {
|
|
66
|
+
return data;
|
|
67
|
+
};
|
|
68
|
+
return data;
|
|
69
|
+
}
|
|
63
70
|
var _ESMOutputFormat = require("./ESMOutputFormat");
|
|
64
71
|
var _CJSOutputFormat = require("./CJSOutputFormat");
|
|
65
72
|
var _GlobalOutputFormat = require("./GlobalOutputFormat");
|
|
@@ -70,7 +77,6 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
|
|
|
70
77
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
71
78
|
// General regex used to replace imports with the resolved code, references with resolutions,
|
|
72
79
|
// and count the number of newlines in the file for source maps.
|
|
73
|
-
//
|
|
74
80
|
// For conditional bundling the only difference in this regex is adding `importCond` where we have `importAsync` etc..
|
|
75
81
|
const REPLACEMENT_RE_CONDITIONAL = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require|importCond)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
|
|
76
82
|
const REPLACEMENT_RE = /\n|import\s+"([0-9a-f]{16,20}:.+?)";|(?:\$[0-9a-f]{16,20}\$exports)|(?:\$[0-9a-f]{16,20}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
|
|
@@ -90,6 +96,8 @@ const OUTPUT_FORMATS = {
|
|
|
90
96
|
global: _GlobalOutputFormat.GlobalOutputFormat
|
|
91
97
|
};
|
|
92
98
|
class ScopeHoistingPackager {
|
|
99
|
+
// @ts-expect-error TS2564
|
|
100
|
+
|
|
93
101
|
exportedSymbols = new Map();
|
|
94
102
|
externals = new Map();
|
|
95
103
|
topLevelNames = new Map();
|
|
@@ -99,14 +107,12 @@ class ScopeHoistingPackager {
|
|
|
99
107
|
needsPrelude = false;
|
|
100
108
|
usedHelpers = new Set();
|
|
101
109
|
externalAssets = new Set();
|
|
102
|
-
|
|
103
|
-
constructor(options, bundleGraph, bundle, parcelRequireName, useAsyncBundleRuntime, forceSkipWrapAssets, logger) {
|
|
110
|
+
constructor(options, bundleGraph, bundle, parcelRequireName, useAsyncBundleRuntime, logger) {
|
|
104
111
|
this.options = options;
|
|
105
112
|
this.bundleGraph = bundleGraph;
|
|
106
113
|
this.bundle = bundle;
|
|
107
114
|
this.parcelRequireName = parcelRequireName;
|
|
108
115
|
this.useAsyncBundleRuntime = useAsyncBundleRuntime;
|
|
109
|
-
this.forceSkipWrapAssets = forceSkipWrapAssets ?? [];
|
|
110
116
|
this.logger = logger;
|
|
111
117
|
let OutputFormat = OUTPUT_FORMATS[this.bundle.env.outputFormat];
|
|
112
118
|
this.outputFormat = new OutputFormat(this);
|
|
@@ -115,7 +121,10 @@ class ScopeHoistingPackager {
|
|
|
115
121
|
}
|
|
116
122
|
async package() {
|
|
117
123
|
var _sourceMap;
|
|
118
|
-
let
|
|
124
|
+
let {
|
|
125
|
+
wrapped: wrappedAssets,
|
|
126
|
+
constant: constantAssets
|
|
127
|
+
} = await this.loadAssets();
|
|
119
128
|
this.buildExportedSymbols();
|
|
120
129
|
|
|
121
130
|
// If building a library, the target is actually another bundler rather
|
|
@@ -132,9 +141,11 @@ class ScopeHoistingPackager {
|
|
|
132
141
|
}
|
|
133
142
|
let res = '';
|
|
134
143
|
let lineCount = 0;
|
|
144
|
+
// @ts-expect-error TS7034
|
|
135
145
|
let sourceMap = null;
|
|
136
146
|
let processAsset = asset => {
|
|
137
147
|
let [content, map, lines] = this.visitAsset(asset);
|
|
148
|
+
// @ts-expect-error TS7005
|
|
138
149
|
if (sourceMap && map) {
|
|
139
150
|
sourceMap.addSourceMap(map, lineCount);
|
|
140
151
|
} else if (this.bundle.env.sourceMap) {
|
|
@@ -143,6 +154,14 @@ class ScopeHoistingPackager {
|
|
|
143
154
|
res += content + '\n';
|
|
144
155
|
lineCount += lines + 1;
|
|
145
156
|
};
|
|
157
|
+
if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || (0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
|
|
158
|
+
// Write out all constant modules used by this bundle
|
|
159
|
+
for (let asset of constantAssets) {
|
|
160
|
+
if (!this.seenAssets.has(asset.id)) {
|
|
161
|
+
processAsset(asset);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
146
165
|
|
|
147
166
|
// Hoist wrapped asset to the top of the bundle to ensure that they are registered
|
|
148
167
|
// before they are used.
|
|
@@ -165,16 +184,21 @@ class ScopeHoistingPackager {
|
|
|
165
184
|
let [prelude, preludeLines] = this.buildBundlePrelude();
|
|
166
185
|
res = prelude + res;
|
|
167
186
|
lineCount += preludeLines;
|
|
187
|
+
// @ts-expect-error TS2339
|
|
168
188
|
(_sourceMap = sourceMap) === null || _sourceMap === void 0 || _sourceMap.offsetLines(1, preludeLines);
|
|
169
189
|
let entries = this.bundle.getEntryAssets();
|
|
170
190
|
let mainEntry = this.bundle.getMainEntry();
|
|
171
191
|
if (this.isAsyncBundle) {
|
|
172
192
|
// In async bundles we don't want the main entry to execute until we require it
|
|
173
193
|
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
194
|
+
if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
|
|
195
|
+
entries = [];
|
|
196
|
+
} else {
|
|
197
|
+
entries = entries.filter(a => {
|
|
198
|
+
var _mainEntry;
|
|
199
|
+
return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
178
202
|
mainEntry = null;
|
|
179
203
|
}
|
|
180
204
|
let needsBundleQueue = this.shouldBundleQueue(this.bundle);
|
|
@@ -184,9 +208,14 @@ class ScopeHoistingPackager {
|
|
|
184
208
|
if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
|
|
185
209
|
var _entry$symbols$get;
|
|
186
210
|
let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
|
|
211
|
+
|
|
212
|
+
// @ts-expect-error TS2345
|
|
187
213
|
let entryExports = (_entry$symbols$get = entry.symbols.get('*')) === null || _entry$symbols$get === void 0 ? void 0 : _entry$symbols$get.local;
|
|
188
|
-
if (entryExports && entry === mainEntry &&
|
|
214
|
+
if (entryExports && entry === mainEntry &&
|
|
215
|
+
// @ts-expect-error TS2345
|
|
216
|
+
this.exportedSymbols.has(entryExports)) {
|
|
189
217
|
(0, _assert().default)(!needsBundleQueue, 'Entry exports are not yet compaitble with async bundles');
|
|
218
|
+
// @ts-expect-error TS2731
|
|
190
219
|
res += `\nvar ${entryExports} = ${parcelRequire}`;
|
|
191
220
|
} else {
|
|
192
221
|
if (needsBundleQueue) {
|
|
@@ -218,6 +247,7 @@ class ScopeHoistingPackager {
|
|
|
218
247
|
}
|
|
219
248
|
res += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
|
|
220
249
|
if (sourceMap && map) {
|
|
250
|
+
// @ts-expect-error TS2339
|
|
221
251
|
sourceMap.addSourceMap(map, lineCount);
|
|
222
252
|
}
|
|
223
253
|
}
|
|
@@ -231,7 +261,7 @@ class ScopeHoistingPackager {
|
|
|
231
261
|
let hasHtmlReference = referencingBundles.some(b => b.type === 'html');
|
|
232
262
|
let hasConditionalReference = false;
|
|
233
263
|
let isConditionalBundle = false;
|
|
234
|
-
if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')
|
|
264
|
+
if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
|
|
235
265
|
// If the bundle has a conditional bundle reference (has an importCond)
|
|
236
266
|
hasConditionalReference = this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
|
|
237
267
|
// If the bundle is a conditional bundle
|
|
@@ -242,7 +272,7 @@ class ScopeHoistingPackager {
|
|
|
242
272
|
runWhenReady(bundle, codeToRun) {
|
|
243
273
|
let deps = this.bundleGraph.getReferencedBundles(bundle).filter(b => this.shouldBundleQueue(b)).map(b => b.publicId);
|
|
244
274
|
const conditions = [];
|
|
245
|
-
if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')
|
|
275
|
+
if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
|
|
246
276
|
const conditionSet = this.bundleGraph.getConditionalBundleMapping().get(bundle.id);
|
|
247
277
|
for (const [key, {
|
|
248
278
|
ifTrueBundles,
|
|
@@ -265,6 +295,7 @@ class ScopeHoistingPackager {
|
|
|
265
295
|
maxConcurrent: 32
|
|
266
296
|
});
|
|
267
297
|
let wrapped = [];
|
|
298
|
+
let constant = [];
|
|
268
299
|
this.bundle.traverseAssets(asset => {
|
|
269
300
|
queue.add(async () => {
|
|
270
301
|
let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
|
|
@@ -278,38 +309,66 @@ class ScopeHoistingPackager {
|
|
|
278
309
|
if (!asset.meta.isConstantModule || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.priority === 'lazy')) {
|
|
279
310
|
this.wrappedAssets.add(asset.id);
|
|
280
311
|
wrapped.push(asset);
|
|
312
|
+
} else if (((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || (0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) && asset.meta.isConstantModule) {
|
|
313
|
+
constant.push(asset);
|
|
281
314
|
}
|
|
282
315
|
}
|
|
283
316
|
});
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
317
|
+
if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
|
|
318
|
+
// Tracks which assets have been assigned to a wrap group
|
|
319
|
+
let assignedAssets = new Set();
|
|
320
|
+
for (let wrappedAsset of wrapped) {
|
|
321
|
+
this.bundle.traverseAssets((asset, _, actions) => {
|
|
322
|
+
if (asset === wrappedAsset) {
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
if (this.wrappedAssets.has(asset.id)) {
|
|
326
|
+
actions.skipChildren();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
if (!asset.meta.isConstantModule && (assignedAssets.has(asset) || this.isReExported(asset))) {
|
|
330
|
+
wrapped.push(asset);
|
|
331
|
+
this.wrappedAssets.add(asset.id);
|
|
332
|
+
actions.skipChildren();
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
assignedAssets.add(asset);
|
|
336
|
+
}, wrappedAsset);
|
|
337
|
+
}
|
|
338
|
+
} else {
|
|
339
|
+
for (let wrappedAssetRoot of [...wrapped]) {
|
|
340
|
+
this.bundle.traverseAssets((asset, _, actions) => {
|
|
341
|
+
if (asset === wrappedAssetRoot) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (this.wrappedAssets.has(asset.id)) {
|
|
345
|
+
actions.skipChildren();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
if (!asset.meta.isConstantModule) {
|
|
349
|
+
this.wrappedAssets.add(asset.id);
|
|
350
|
+
wrapped.push(asset);
|
|
351
|
+
}
|
|
352
|
+
}, wrappedAssetRoot);
|
|
353
|
+
}
|
|
310
354
|
}
|
|
355
|
+
|
|
356
|
+
// @ts-expect-error TS2769
|
|
311
357
|
this.assetOutputs = new Map(await queue.run());
|
|
312
|
-
return
|
|
358
|
+
return {
|
|
359
|
+
wrapped,
|
|
360
|
+
constant
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
isReExported(asset) {
|
|
364
|
+
let parentSymbols = this.bundleGraph.getIncomingDependencies(asset).map(dep => this.bundleGraph.getAssetWithDependency(dep)).flatMap(parent => {
|
|
365
|
+
if (parent == null) {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
return this.bundleGraph.getExportedSymbols(parent, this.bundle);
|
|
369
|
+
});
|
|
370
|
+
let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
|
|
371
|
+
return assetSymbols.some(assetSymbol => parentSymbols.some(parentSymbol => parentSymbol.symbol === assetSymbol.symbol));
|
|
313
372
|
}
|
|
314
373
|
buildExportedSymbols() {
|
|
315
374
|
if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
|
|
@@ -319,6 +378,7 @@ class ScopeHoistingPackager {
|
|
|
319
378
|
// TODO: handle ESM exports of wrapped entry assets...
|
|
320
379
|
let entry = this.bundle.getMainEntry();
|
|
321
380
|
if (entry && !this.wrappedAssets.has(entry.id)) {
|
|
381
|
+
// @ts-expect-error TS2345
|
|
322
382
|
let hasNamespace = entry.symbols.hasExportSymbol('*');
|
|
323
383
|
for (let {
|
|
324
384
|
asset,
|
|
@@ -334,11 +394,14 @@ class ScopeHoistingPackager {
|
|
|
334
394
|
if (hasNamespace && this.isAsyncBundle && exportAs !== '*') {
|
|
335
395
|
continue;
|
|
336
396
|
}
|
|
337
|
-
let symbols = (_this$exportedSymbols = this.exportedSymbols.get(
|
|
397
|
+
let symbols = (_this$exportedSymbols = this.exportedSymbols.get(
|
|
398
|
+
// @ts-expect-error TS2345
|
|
399
|
+
symbol === '*' ? (0, _nullthrows().default)((_entry$symbols$get2 = entry.symbols.get('*')) === null || _entry$symbols$get2 === void 0 ? void 0 : _entry$symbols$get2.local) : symbol)) === null || _this$exportedSymbols === void 0 ? void 0 : _this$exportedSymbols.exportAs;
|
|
338
400
|
if (!symbols) {
|
|
339
401
|
symbols = [];
|
|
340
402
|
this.exportedSymbols.set(symbol, {
|
|
341
403
|
asset,
|
|
404
|
+
// @ts-expect-error TS2322
|
|
342
405
|
exportSymbol,
|
|
343
406
|
local: symbol,
|
|
344
407
|
exportAs: symbols
|
|
@@ -347,6 +410,8 @@ class ScopeHoistingPackager {
|
|
|
347
410
|
if (exportAs === '*') {
|
|
348
411
|
exportAs = 'default';
|
|
349
412
|
}
|
|
413
|
+
|
|
414
|
+
// @ts-expect-error TS2345
|
|
350
415
|
symbols.push(exportAs);
|
|
351
416
|
} else if (symbol === null) {
|
|
352
417
|
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
|
|
@@ -395,6 +460,9 @@ class ScopeHoistingPackager {
|
|
|
395
460
|
} = (0, _nullthrows().default)(this.assetOutputs.get(asset.id));
|
|
396
461
|
return this.buildAsset(asset, code, map);
|
|
397
462
|
}
|
|
463
|
+
getAssetFilePath(asset) {
|
|
464
|
+
return _path().default.relative(this.options.projectRoot, asset.filePath);
|
|
465
|
+
}
|
|
398
466
|
buildAsset(asset, code, map) {
|
|
399
467
|
let shouldWrap = this.wrappedAssets.has(asset.id);
|
|
400
468
|
let deps = this.bundleGraph.getDependencies(asset);
|
|
@@ -420,6 +488,7 @@ class ScopeHoistingPackager {
|
|
|
420
488
|
let [code, map, lines] = this.visitAsset(resolved);
|
|
421
489
|
depCode += code + '\n';
|
|
422
490
|
if (sourceMap && map) {
|
|
491
|
+
// @ts-expect-error TS2551
|
|
423
492
|
sourceMap.addSourceMap(map, lineCount);
|
|
424
493
|
}
|
|
425
494
|
lineCount += lines + 1;
|
|
@@ -445,6 +514,7 @@ class ScopeHoistingPackager {
|
|
|
445
514
|
}
|
|
446
515
|
code += append;
|
|
447
516
|
let lineCount = 0;
|
|
517
|
+
// @ts-expect-error TS2552
|
|
448
518
|
let depContent = [];
|
|
449
519
|
if (depMap.size === 0 && replacements.size === 0) {
|
|
450
520
|
// If there are no dependencies or replacements, use a simple function to count the number of lines.
|
|
@@ -489,13 +559,33 @@ class ScopeHoistingPackager {
|
|
|
489
559
|
// outside our parcelRequire.register wrapper. This is safe because all
|
|
490
560
|
// assets referenced by this asset will also be wrapped. Otherwise, inline the
|
|
491
561
|
// asset content where the import statement was.
|
|
492
|
-
if (
|
|
493
|
-
|
|
562
|
+
if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
|
|
563
|
+
if (!resolved.meta.isConstantModule && !this.wrappedAssets.has(resolved.id)) {
|
|
564
|
+
let [depCode, depMap, depLines] = this.visitAsset(resolved);
|
|
565
|
+
if (_utils().debugTools['asset-file-names-in-output']) {
|
|
566
|
+
let resolvedPath = this.getAssetFilePath(resolved);
|
|
567
|
+
res = (0, _outdent().outdent)`
|
|
568
|
+
/* Scope hoisted asset: ${resolvedPath} */
|
|
569
|
+
${depCode}
|
|
570
|
+
/* End: ${resolvedPath} */
|
|
571
|
+
${res}
|
|
572
|
+
`;
|
|
573
|
+
lines += 3 + depLines;
|
|
574
|
+
} else {
|
|
575
|
+
res = depCode + '\n' + res;
|
|
576
|
+
lines += 1 + depLines;
|
|
577
|
+
}
|
|
578
|
+
map = depMap;
|
|
579
|
+
}
|
|
494
580
|
} else {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
581
|
+
if (shouldWrap) {
|
|
582
|
+
depContent.push(this.visitAsset(resolved));
|
|
583
|
+
} else {
|
|
584
|
+
let [depCode, depMap, depLines] = this.visitAsset(resolved);
|
|
585
|
+
res = depCode + '\n' + res;
|
|
586
|
+
lines += 1 + depLines;
|
|
587
|
+
map = depMap;
|
|
588
|
+
}
|
|
499
589
|
}
|
|
500
590
|
}
|
|
501
591
|
|
|
@@ -506,6 +596,7 @@ class ScopeHoistingPackager {
|
|
|
506
596
|
sourceMap.offsetLines(lineCount + 1, lines);
|
|
507
597
|
}
|
|
508
598
|
if (map) {
|
|
599
|
+
// @ts-expect-error TS2551
|
|
509
600
|
sourceMap.addSourceMap(map, lineCount);
|
|
510
601
|
}
|
|
511
602
|
}
|
|
@@ -542,10 +633,15 @@ ${code}
|
|
|
542
633
|
});
|
|
543
634
|
`;
|
|
544
635
|
lineCount += 2;
|
|
636
|
+
if (_utils().debugTools['asset-file-names-in-output']) {
|
|
637
|
+
code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
|
|
638
|
+
lineCount += 1;
|
|
639
|
+
}
|
|
545
640
|
for (let [depCode, map, lines] of depContent) {
|
|
546
641
|
if (!depCode) continue;
|
|
547
642
|
code += depCode + '\n';
|
|
548
643
|
if (sourceMap && map) {
|
|
644
|
+
// @ts-expect-error TS2551
|
|
549
645
|
sourceMap.addSourceMap(map, lineCount);
|
|
550
646
|
}
|
|
551
647
|
lineCount += lines + 1;
|
|
@@ -592,9 +688,12 @@ ${code}
|
|
|
592
688
|
for (let [imported, {
|
|
593
689
|
local
|
|
594
690
|
}] of dep.symbols) {
|
|
691
|
+
// @ts-expect-error TS2367
|
|
595
692
|
if (local === '*') {
|
|
596
693
|
continue;
|
|
597
694
|
}
|
|
695
|
+
|
|
696
|
+
// @ts-expect-error TS2345
|
|
598
697
|
let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
|
|
599
698
|
replacements.set(local,
|
|
600
699
|
// If this was an internalized async asset, wrap in a Promise.resolve.
|
|
@@ -616,6 +715,7 @@ ${code}
|
|
|
616
715
|
// which will be provided to us by the wrapper.
|
|
617
716
|
if (this.wrappedAssets.has(asset.id) || this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry()) {
|
|
618
717
|
var _asset$symbols$get;
|
|
718
|
+
// @ts-expect-error TS2345
|
|
619
719
|
let exportsName = ((_asset$symbols$get = asset.symbols.get('*')) === null || _asset$symbols$get === void 0 ? void 0 : _asset$symbols$get.local) || `$${assetId}$exports`;
|
|
620
720
|
replacements.set(exportsName, 'module.exports');
|
|
621
721
|
}
|
|
@@ -648,8 +748,11 @@ ${code}
|
|
|
648
748
|
local
|
|
649
749
|
}] of dep.symbols) {
|
|
650
750
|
// If already imported, just add the already renamed variable to the mapping.
|
|
751
|
+
// @ts-expect-error TS2345
|
|
651
752
|
let renamed = external.get(imported);
|
|
753
|
+
// @ts-expect-error TS2367
|
|
652
754
|
if (renamed && local !== '*' && replacements) {
|
|
755
|
+
// @ts-expect-error TS2345
|
|
653
756
|
replacements.set(local, renamed);
|
|
654
757
|
continue;
|
|
655
758
|
}
|
|
@@ -662,16 +765,25 @@ ${code}
|
|
|
662
765
|
if (referencedBundle) {
|
|
663
766
|
var _entry$symbols$get3;
|
|
664
767
|
let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
|
|
665
|
-
|
|
768
|
+
// @ts-expect-error TS2322
|
|
769
|
+
renamed =
|
|
770
|
+
// @ts-expect-error TS2345
|
|
771
|
+
((_entry$symbols$get3 = entry.symbols.get('*')) === null || _entry$symbols$get3 === void 0 ? void 0 : _entry$symbols$get3.local) ?? `$${String(entry.meta.id)}$exports`;
|
|
666
772
|
} else {
|
|
667
773
|
renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
|
|
668
774
|
}
|
|
775
|
+
|
|
776
|
+
// @ts-expect-error TS2345
|
|
669
777
|
external.set('*', renamed);
|
|
670
778
|
}
|
|
779
|
+
|
|
780
|
+
// @ts-expect-error TS2367
|
|
671
781
|
if (local !== '*' && replacements) {
|
|
672
782
|
let replacement;
|
|
783
|
+
// @ts-expect-error TS2367
|
|
673
784
|
if (imported === '*') {
|
|
674
785
|
replacement = renamed;
|
|
786
|
+
// @ts-expect-error TS2367
|
|
675
787
|
} else if (imported === 'default') {
|
|
676
788
|
let needsDefaultInterop = true;
|
|
677
789
|
if (referencedBundle) {
|
|
@@ -685,32 +797,41 @@ ${code}
|
|
|
685
797
|
replacement = `${renamed}.default`;
|
|
686
798
|
}
|
|
687
799
|
} else {
|
|
800
|
+
// @ts-expect-error TS2345
|
|
688
801
|
replacement = this.getPropertyAccess(renamed, imported);
|
|
689
802
|
}
|
|
803
|
+
|
|
804
|
+
// @ts-expect-error TS2345
|
|
690
805
|
replacements.set(local, replacement);
|
|
691
806
|
}
|
|
692
807
|
} else {
|
|
693
808
|
let property;
|
|
694
809
|
if (referencedBundle) {
|
|
695
810
|
let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
|
|
811
|
+
// @ts-expect-error TS2345
|
|
696
812
|
if (entry.symbols.hasExportSymbol('*')) {
|
|
697
813
|
// If importing * and the referenced module has a * export (e.g. CJS), use default instead.
|
|
698
814
|
// This mirrors the logic in buildExportedSymbols.
|
|
699
815
|
property = imported;
|
|
816
|
+
// @ts-expect-error TS2322
|
|
700
817
|
imported = (referencedBundle === null || referencedBundle === void 0 ? void 0 : referencedBundle.env.outputFormat) === 'esmodule' ? 'default' : '*';
|
|
701
818
|
} else {
|
|
819
|
+
// @ts-expect-error TS2367
|
|
702
820
|
if (imported === '*') {
|
|
703
821
|
let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
|
|
822
|
+
// @ts-expect-error TS2367
|
|
704
823
|
if (local === '*') {
|
|
705
824
|
// Re-export all symbols.
|
|
706
825
|
for (let exported of exportedSymbols) {
|
|
707
826
|
if (exported.symbol) {
|
|
827
|
+
// @ts-expect-error TS2345
|
|
708
828
|
external.set(exported.exportSymbol, exported.symbol);
|
|
709
829
|
}
|
|
710
830
|
}
|
|
711
831
|
continue;
|
|
712
832
|
}
|
|
713
833
|
}
|
|
834
|
+
// @ts-expect-error TS2322
|
|
714
835
|
renamed = this.bundleGraph.getSymbolResolution(entry, imported, this.bundle).symbol;
|
|
715
836
|
}
|
|
716
837
|
}
|
|
@@ -719,25 +840,37 @@ ${code}
|
|
|
719
840
|
// are deduplicated. We have to prefix the imported name with the bundle id so that
|
|
720
841
|
// local variables do not shadow it.
|
|
721
842
|
if (!renamed) {
|
|
843
|
+
// @ts-expect-error TS2345
|
|
722
844
|
if (this.exportedSymbols.has(local)) {
|
|
845
|
+
// @ts-expect-error TS2322
|
|
723
846
|
renamed = local;
|
|
847
|
+
// @ts-expect-error TS2367
|
|
724
848
|
} else if (imported === 'default' || imported === '*') {
|
|
725
849
|
renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
|
|
726
850
|
} else {
|
|
727
|
-
renamed = this.getTopLevelName(
|
|
851
|
+
renamed = this.getTopLevelName(
|
|
852
|
+
// @ts-expect-error TS2731
|
|
853
|
+
`$${this.bundle.publicId}$${imported}`);
|
|
728
854
|
}
|
|
729
855
|
}
|
|
856
|
+
|
|
857
|
+
// @ts-expect-error TS2345
|
|
730
858
|
external.set(imported, renamed);
|
|
859
|
+
// @ts-expect-error TS2367
|
|
731
860
|
if (local !== '*' && replacements) {
|
|
732
861
|
let replacement = renamed;
|
|
862
|
+
// @ts-expect-error TS2367
|
|
733
863
|
if (property === '*') {
|
|
734
864
|
replacement = renamed;
|
|
865
|
+
// @ts-expect-error TS2367
|
|
735
866
|
} else if (property === 'default') {
|
|
736
867
|
replacement = `($parcel$interopDefault(${renamed}))`;
|
|
737
868
|
this.usedHelpers.add('$parcel$interopDefault');
|
|
738
869
|
} else if (property) {
|
|
870
|
+
// @ts-expect-error TS2345
|
|
739
871
|
replacement = this.getPropertyAccess(renamed, property);
|
|
740
872
|
}
|
|
873
|
+
// @ts-expect-error TS2345
|
|
741
874
|
replacements.set(local, replacement);
|
|
742
875
|
}
|
|
743
876
|
}
|
|
@@ -759,6 +892,7 @@ ${code}
|
|
|
759
892
|
asset: resolvedAsset,
|
|
760
893
|
exportSymbol,
|
|
761
894
|
symbol
|
|
895
|
+
// @ts-expect-error TS2345
|
|
762
896
|
} = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
|
|
763
897
|
if (resolvedAsset.type !== 'js' || dep && this.bundleGraph.isDependencySkipped(dep)) {
|
|
764
898
|
// Graceful fallback for non-js imports or when trying to resolve a symbol
|
|
@@ -792,7 +926,13 @@ ${code}
|
|
|
792
926
|
|
|
793
927
|
// If this is an ESM default import of a CJS module with a `default` symbol,
|
|
794
928
|
// and no __esModule flag, we need to resolve to the namespace instead.
|
|
795
|
-
let isDefaultInterop = exportSymbol === 'default' && staticExports && !isWrapped && ((dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Import' || (dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Export') &&
|
|
929
|
+
let isDefaultInterop = exportSymbol === 'default' && staticExports && !isWrapped && ((dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Import' || (dep === null || dep === void 0 ? void 0 : dep.meta.kind) === 'Export') &&
|
|
930
|
+
// @ts-expect-error TS2345
|
|
931
|
+
resolvedAsset.symbols.hasExportSymbol('*') &&
|
|
932
|
+
// @ts-expect-error TS2345
|
|
933
|
+
resolvedAsset.symbols.hasExportSymbol('default') &&
|
|
934
|
+
// @ts-expect-error TS2345
|
|
935
|
+
!resolvedAsset.symbols.hasExportSymbol('__esModule');
|
|
796
936
|
|
|
797
937
|
// Find the namespace object for the resolved module. If wrapped and this
|
|
798
938
|
// is an inline require (not top-level), use a parcelRequire call, otherwise
|
|
@@ -808,7 +948,9 @@ ${code}
|
|
|
808
948
|
obj = `$${publicId}`;
|
|
809
949
|
} else {
|
|
810
950
|
var _resolvedAsset$symbol;
|
|
951
|
+
// @ts-expect-error TS2345
|
|
811
952
|
obj = ((_resolvedAsset$symbol = resolvedAsset.symbols.get('*')) === null || _resolvedAsset$symbol === void 0 ? void 0 : _resolvedAsset$symbol.local) || `$${assetId}$exports`;
|
|
953
|
+
// @ts-expect-error TS2345
|
|
812
954
|
obj = (replacements === null || replacements === void 0 ? void 0 : replacements.get(obj)) || obj;
|
|
813
955
|
}
|
|
814
956
|
if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
|
|
@@ -817,6 +959,7 @@ ${code}
|
|
|
817
959
|
// Directly use module.exports for wrapped assets importing themselves.
|
|
818
960
|
return 'module.exports';
|
|
819
961
|
} else {
|
|
962
|
+
// @ts-expect-error TS2322
|
|
820
963
|
return obj;
|
|
821
964
|
}
|
|
822
965
|
} else if ((!staticExports || isWrapped || !symbol || isExternalCommonJS) && resolvedAsset !== parentAsset) {
|
|
@@ -825,15 +968,20 @@ ${code}
|
|
|
825
968
|
// than a direct reference. If importing default from a CJS module,
|
|
826
969
|
// use a helper to check the __esModule flag at runtime.
|
|
827
970
|
let kind = dep === null || dep === void 0 ? void 0 : dep.meta.kind;
|
|
828
|
-
if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' &&
|
|
971
|
+
if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' &&
|
|
972
|
+
// @ts-expect-error TS2345
|
|
973
|
+
resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
|
|
829
974
|
this.usedHelpers.add('$parcel$interopDefault');
|
|
975
|
+
// @ts-expect-error TS2731
|
|
830
976
|
return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
|
|
831
977
|
} else {
|
|
978
|
+
// @ts-expect-error TS2345
|
|
832
979
|
return this.getPropertyAccess(obj, exportSymbol);
|
|
833
980
|
}
|
|
834
981
|
} else if (!symbol) {
|
|
835
982
|
(0, _assert().default)(false, 'Asset was skipped or not found.');
|
|
836
983
|
} else {
|
|
984
|
+
// @ts-expect-error TS2322
|
|
837
985
|
return (replacements === null || replacements === void 0 ? void 0 : replacements.get(symbol)) || symbol;
|
|
838
986
|
}
|
|
839
987
|
}
|
|
@@ -873,21 +1021,39 @@ ${code}
|
|
|
873
1021
|
// If the asset has a namespace export symbol, it is CommonJS.
|
|
874
1022
|
// If there's no __esModule flag, and default is a used symbol, we need
|
|
875
1023
|
// to insert an interop helper.
|
|
876
|
-
let defaultInterop =
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
//
|
|
880
|
-
|
|
881
|
-
//
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
1024
|
+
let defaultInterop =
|
|
1025
|
+
// @ts-expect-error TS2345
|
|
1026
|
+
asset.symbols.hasExportSymbol('*') &&
|
|
1027
|
+
// @ts-expect-error TS2345
|
|
1028
|
+
usedSymbols.has('default') &&
|
|
1029
|
+
// @ts-expect-error TS2345
|
|
1030
|
+
!asset.symbols.hasExportSymbol('__esModule');
|
|
1031
|
+
let usedNamespace;
|
|
1032
|
+
if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') && asset.meta.isConstantModule) {
|
|
1033
|
+
// Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
|
|
1034
|
+
usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(dep => this.bundle.hasDependency(dep) &&
|
|
1035
|
+
// @ts-expect-error TS2345
|
|
1036
|
+
(0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'));
|
|
1037
|
+
} else {
|
|
1038
|
+
usedNamespace =
|
|
1039
|
+
// If the asset has * in its used symbols, we might need the exports namespace.
|
|
1040
|
+
// The one case where this isn't true is in ESM library entries, where the only
|
|
1041
|
+
// dependency on * is the entry dependency. In this case, we will use ESM exports
|
|
1042
|
+
// instead of the namespace object.
|
|
1043
|
+
// @ts-expect-error TS2345
|
|
1044
|
+
usedSymbols.has('*') && (this.bundle.env.outputFormat !== 'esmodule' || !this.bundle.env.isLibrary || asset !== this.bundle.getMainEntry() || this.bundleGraph.getIncomingDependencies(asset).some(dep => !dep.isEntry && this.bundle.hasDependency(dep) &&
|
|
1045
|
+
// @ts-expect-error TS2345
|
|
1046
|
+
(0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
|
|
1047
|
+
// If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
|
|
1048
|
+
// we fallback on the namespace object.
|
|
1049
|
+
// @ts-expect-error TS2345
|
|
1050
|
+
asset.symbols.hasExportSymbol('*') && [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s)) ||
|
|
1051
|
+
// If the exports has this asset's namespace (e.g. ESM output from CJS input),
|
|
1052
|
+
// include the namespace object for the default export.
|
|
1053
|
+
this.exportedSymbols.has(`$${assetId}$exports`) ||
|
|
1054
|
+
// CommonJS library bundle entries always need a namespace.
|
|
1055
|
+
this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry();
|
|
1056
|
+
}
|
|
891
1057
|
|
|
892
1058
|
// If the asset doesn't have static exports, should wrap, the namespace is used,
|
|
893
1059
|
// or we need default interop, then we need to synthesize a namespace object for
|
|
@@ -905,6 +1071,7 @@ ${code}
|
|
|
905
1071
|
// Insert the __esModule interop flag for this module if it has a `default` export
|
|
906
1072
|
// and the namespace symbol is used.
|
|
907
1073
|
// TODO: only if required by CJS?
|
|
1074
|
+
// @ts-expect-error TS2345
|
|
908
1075
|
if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
|
|
909
1076
|
prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
|
|
910
1077
|
prependLineCount += 2;
|
|
@@ -923,6 +1090,7 @@ ${code}
|
|
|
923
1090
|
for (let [imported, {
|
|
924
1091
|
local
|
|
925
1092
|
}] of dep.symbols) {
|
|
1093
|
+
// @ts-expect-error TS2367
|
|
926
1094
|
if (imported === '*' && local === '*') {
|
|
927
1095
|
if (!resolved) {
|
|
928
1096
|
// Re-exporting an external module. This should have already been handled in buildReplacements.
|
|
@@ -935,20 +1103,29 @@ ${code}
|
|
|
935
1103
|
// If the resolved asset has an exports object, use the $parcel$exportWildcard helper
|
|
936
1104
|
// to re-export all symbols. Otherwise, if there's no namespace object available, add
|
|
937
1105
|
// $parcel$export calls for each used symbol of the dependency.
|
|
938
|
-
if (isWrapped || resolved.meta.staticExports === false ||
|
|
1106
|
+
if (isWrapped || resolved.meta.staticExports === false ||
|
|
1107
|
+
// @ts-expect-error TS2345
|
|
1108
|
+
(0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
|
|
939
1109
|
// an empty asset
|
|
940
|
-
!resolved.meta.hasCJSExports &&
|
|
1110
|
+
!resolved.meta.hasCJSExports &&
|
|
1111
|
+
// @ts-expect-error TS2345
|
|
1112
|
+
resolved.symbols.hasExportSymbol('*')) {
|
|
941
1113
|
let obj = this.getSymbolResolution(asset, resolved, '*', dep, replacements);
|
|
942
1114
|
append += `$parcel$exportWildcard($${assetId}$exports, ${obj});\n`;
|
|
943
1115
|
this.usedHelpers.add('$parcel$exportWildcard');
|
|
944
1116
|
} else {
|
|
945
1117
|
for (let symbol of (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep))) {
|
|
946
|
-
if (
|
|
1118
|
+
if (
|
|
1119
|
+
// @ts-expect-error TS2367
|
|
1120
|
+
symbol === 'default' ||
|
|
947
1121
|
// `export * as ...` does not include the default export
|
|
1122
|
+
// @ts-expect-error TS2367
|
|
948
1123
|
symbol === '__esModule') {
|
|
949
1124
|
continue;
|
|
950
1125
|
}
|
|
951
|
-
let resolvedSymbol = this.getSymbolResolution(asset, resolved,
|
|
1126
|
+
let resolvedSymbol = this.getSymbolResolution(asset, resolved,
|
|
1127
|
+
// @ts-expect-error TS2345
|
|
1128
|
+
symbol, undefined, replacements);
|
|
952
1129
|
let get = this.buildFunctionExpression([], resolvedSymbol);
|
|
953
1130
|
let set = asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolvedSymbol} = v`) : '';
|
|
954
1131
|
prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(symbol)}, ${get}${set});\n`;
|
|
@@ -965,6 +1142,7 @@ ${code}
|
|
|
965
1142
|
// re-exported symbols rather than only symbols declared in this asset.
|
|
966
1143
|
let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
|
|
967
1144
|
let usedExports = [...asset.symbols.exportSymbols()].filter(symbol => {
|
|
1145
|
+
// @ts-expect-error TS2367
|
|
968
1146
|
if (symbol === '*') {
|
|
969
1147
|
return false;
|
|
970
1148
|
}
|
|
@@ -979,6 +1157,8 @@ ${code}
|
|
|
979
1157
|
|
|
980
1158
|
// No used symbols available for the asset, make sure we keep all of them
|
|
981
1159
|
if (!symbols) return false;
|
|
1160
|
+
|
|
1161
|
+
// @ts-expect-error TS2345
|
|
982
1162
|
return !symbols.has(symbol) && !symbols.has('*');
|
|
983
1163
|
});
|
|
984
1164
|
return !unused;
|
|
@@ -990,7 +1170,9 @@ ${code}
|
|
|
990
1170
|
// additional assignments after each mutation of the original binding.
|
|
991
1171
|
prepend += `\n${usedExports.map(exp => {
|
|
992
1172
|
var _asset$symbols$get2;
|
|
993
|
-
let resolved = this.getSymbolResolution(asset, asset,
|
|
1173
|
+
let resolved = this.getSymbolResolution(asset, asset,
|
|
1174
|
+
// @ts-expect-error TS2345
|
|
1175
|
+
exp, undefined, replacements);
|
|
994
1176
|
let get = this.buildFunctionExpression([], resolved);
|
|
995
1177
|
let isEsmExport = !!((_asset$symbols$get2 = asset.symbols.get(exp)) !== null && _asset$symbols$get2 !== void 0 && (_asset$symbols$get2 = _asset$symbols$get2.meta) !== null && _asset$symbols$get2 !== void 0 && _asset$symbols$get2.isEsm);
|
|
996
1178
|
let set = !isEsmExport && asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`) : '';
|
|
@@ -1028,8 +1210,10 @@ ${code}
|
|
|
1028
1210
|
this.usedHelpers.add('$parcel$global');
|
|
1029
1211
|
}
|
|
1030
1212
|
for (let helper of this.usedHelpers) {
|
|
1213
|
+
// @ts-expect-error TS7053
|
|
1031
1214
|
let currentHelper = _helpers.helpers[helper];
|
|
1032
1215
|
if (typeof currentHelper === 'function') {
|
|
1216
|
+
// @ts-expect-error TS7053
|
|
1033
1217
|
currentHelper = _helpers.helpers[helper](this.bundle.env);
|
|
1034
1218
|
}
|
|
1035
1219
|
res += currentHelper;
|
|
@@ -1085,7 +1269,11 @@ ${code}
|
|
|
1085
1269
|
return [res, lines];
|
|
1086
1270
|
}
|
|
1087
1271
|
needsDefaultInterop(asset) {
|
|
1088
|
-
if (
|
|
1272
|
+
if (
|
|
1273
|
+
// @ts-expect-error TS2345
|
|
1274
|
+
asset.symbols.hasExportSymbol('*') &&
|
|
1275
|
+
// @ts-expect-error TS2345
|
|
1276
|
+
!asset.symbols.hasExportSymbol('default')) {
|
|
1089
1277
|
return true;
|
|
1090
1278
|
}
|
|
1091
1279
|
return false;
|