@atlaspack/packager-js 2.14.5-canary.17 → 2.14.5-canary.171

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.
@@ -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;
@@ -80,6 +86,7 @@ const GLOBALS_BY_CONTEXT = {
80
86
  'web-worker': new Set([...BUILTINS, ...Object.keys(_globals().default.worker)]),
81
87
  'service-worker': new Set([...BUILTINS, ...Object.keys(_globals().default.serviceworker)]),
82
88
  worklet: new Set([...BUILTINS]),
89
+ tesseract: new Set([...BUILTINS, ...Object.keys(_globals().default.worker)]),
83
90
  node: new Set([...BUILTINS, ...Object.keys(_globals().default.node)]),
84
91
  'electron-main': new Set([...BUILTINS, ...Object.keys(_globals().default.node)]),
85
92
  'electron-renderer': new Set([...BUILTINS, ...Object.keys(_globals().default.node), ...Object.keys(_globals().default.browser)])
@@ -90,6 +97,8 @@ const OUTPUT_FORMATS = {
90
97
  global: _GlobalOutputFormat.GlobalOutputFormat
91
98
  };
92
99
  class ScopeHoistingPackager {
100
+ // @ts-expect-error TS2564
101
+
93
102
  exportedSymbols = new Map();
94
103
  externals = new Map();
95
104
  topLevelNames = new Map();
@@ -99,23 +108,24 @@ class ScopeHoistingPackager {
99
108
  needsPrelude = false;
100
109
  usedHelpers = new Set();
101
110
  externalAssets = new Set();
102
- forceSkipWrapAssets = [];
103
- constructor(options, bundleGraph, bundle, parcelRequireName, useAsyncBundleRuntime, forceSkipWrapAssets, logger) {
111
+ constructor(options, bundleGraph, bundle, parcelRequireName, useAsyncBundleRuntime, logger) {
104
112
  this.options = options;
105
113
  this.bundleGraph = bundleGraph;
106
114
  this.bundle = bundle;
107
115
  this.parcelRequireName = parcelRequireName;
108
116
  this.useAsyncBundleRuntime = useAsyncBundleRuntime;
109
- this.forceSkipWrapAssets = forceSkipWrapAssets ?? [];
110
117
  this.logger = logger;
111
118
  let OutputFormat = OUTPUT_FORMATS[this.bundle.env.outputFormat];
112
119
  this.outputFormat = new OutputFormat(this);
113
- this.isAsyncBundle = this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') && !this.bundle.env.isIsolated() && this.bundle.bundleBehavior !== 'isolated';
120
+ this.isAsyncBundle = this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') && !this.bundle.env.isIsolated() && this.bundle.bundleBehavior !== 'isolated' && this.bundle.bundleBehavior !== 'inlineIsolated';
114
121
  this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
115
122
  }
116
123
  async package() {
117
124
  var _sourceMap;
118
- let wrappedAssets = await this.loadAssets();
125
+ let {
126
+ wrapped: wrappedAssets,
127
+ constant: constantAssets
128
+ } = await this.loadAssets();
119
129
  this.buildExportedSymbols();
120
130
 
121
131
  // If building a library, the target is actually another bundler rather
@@ -132,9 +142,11 @@ class ScopeHoistingPackager {
132
142
  }
133
143
  let res = '';
134
144
  let lineCount = 0;
145
+ // @ts-expect-error TS7034
135
146
  let sourceMap = null;
136
147
  let processAsset = asset => {
137
148
  let [content, map, lines] = this.visitAsset(asset);
149
+ // @ts-expect-error TS7005
138
150
  if (sourceMap && map) {
139
151
  sourceMap.addSourceMap(map, lineCount);
140
152
  } else if (this.bundle.env.sourceMap) {
@@ -143,6 +155,14 @@ class ScopeHoistingPackager {
143
155
  res += content + '\n';
144
156
  lineCount += lines + 1;
145
157
  };
158
+ if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || (0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
159
+ // Write out all constant modules used by this bundle
160
+ for (let asset of constantAssets) {
161
+ if (!this.seenAssets.has(asset.id)) {
162
+ processAsset(asset);
163
+ }
164
+ }
165
+ }
146
166
 
147
167
  // Hoist wrapped asset to the top of the bundle to ensure that they are registered
148
168
  // before they are used.
@@ -165,16 +185,28 @@ class ScopeHoistingPackager {
165
185
  let [prelude, preludeLines] = this.buildBundlePrelude();
166
186
  res = prelude + res;
167
187
  lineCount += preludeLines;
188
+ // @ts-expect-error TS2339
168
189
  (_sourceMap = sourceMap) === null || _sourceMap === void 0 || _sourceMap.offsetLines(1, preludeLines);
169
190
  let entries = this.bundle.getEntryAssets();
170
191
  let mainEntry = this.bundle.getMainEntry();
171
192
  if (this.isAsyncBundle) {
172
- // In async bundles we don't want the main entry to execute until we require it
173
- // as there might be dependencies in a sibling bundle that hasn't loaded yet.
174
- entries = entries.filter(a => {
175
- var _mainEntry;
176
- return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
177
- });
193
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement') || (0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
194
+ // Generally speaking, async bundles should not be executed on load, as
195
+ // they're just collections of assets that other assets require.
196
+ // However, there are some special cases where a runtime asset needs to be
197
+ // injected, but no other asset will require it (mostly the bundle
198
+ // manifest).
199
+ // In this case, those assets need to be required on load.
200
+ entries = entries.filter(a => {
201
+ var _a$meta;
202
+ return (_a$meta = a.meta) === null || _a$meta === void 0 ? void 0 : _a$meta.runtimeAssetRequiringExecutionOnLoad;
203
+ });
204
+ } else {
205
+ entries = entries.filter(a => {
206
+ var _mainEntry;
207
+ return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
208
+ });
209
+ }
178
210
  mainEntry = null;
179
211
  }
180
212
  let needsBundleQueue = this.shouldBundleQueue(this.bundle);
@@ -184,9 +216,14 @@ class ScopeHoistingPackager {
184
216
  if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
185
217
  var _entry$symbols$get;
186
218
  let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
219
+
220
+ // @ts-expect-error TS2345
187
221
  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 && this.exportedSymbols.has(entryExports)) {
222
+ if (entryExports && entry === mainEntry &&
223
+ // @ts-expect-error TS2345
224
+ this.exportedSymbols.has(entryExports)) {
189
225
  (0, _assert().default)(!needsBundleQueue, 'Entry exports are not yet compaitble with async bundles');
226
+ // @ts-expect-error TS2731
190
227
  res += `\nvar ${entryExports} = ${parcelRequire}`;
191
228
  } else {
192
229
  if (needsBundleQueue) {
@@ -218,6 +255,7 @@ class ScopeHoistingPackager {
218
255
  }
219
256
  res += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
220
257
  if (sourceMap && map) {
258
+ // @ts-expect-error TS2339
221
259
  sourceMap.addSourceMap(map, lineCount);
222
260
  }
223
261
  }
@@ -231,18 +269,18 @@ class ScopeHoistingPackager {
231
269
  let hasHtmlReference = referencingBundles.some(b => b.type === 'html');
232
270
  let hasConditionalReference = false;
233
271
  let isConditionalBundle = false;
234
- if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && (0, _featureFlags().getFeatureFlag)('conditionalBundlingAsyncRuntime')) {
272
+ if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
235
273
  // If the bundle has a conditional bundle reference (has an importCond)
236
274
  hasConditionalReference = this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
237
275
  // If the bundle is a conditional bundle
238
276
  isConditionalBundle = this.hasConditionalDependency();
239
277
  }
240
- return this.useAsyncBundleRuntime && bundle.type === 'js' && bundle.bundleBehavior !== 'inline' && bundle.env.outputFormat === 'esmodule' && !bundle.env.isIsolated() && bundle.bundleBehavior !== 'isolated' && (hasHtmlReference || hasConditionalReference || isConditionalBundle);
278
+ return this.useAsyncBundleRuntime && bundle.type === 'js' && bundle.bundleBehavior !== 'inline' && bundle.bundleBehavior !== 'inlineIsolated' && bundle.env.outputFormat === 'esmodule' && !bundle.env.isIsolated() && bundle.bundleBehavior !== 'isolated' && (hasHtmlReference || hasConditionalReference || isConditionalBundle);
241
279
  }
242
280
  runWhenReady(bundle, codeToRun) {
243
281
  let deps = this.bundleGraph.getReferencedBundles(bundle).filter(b => this.shouldBundleQueue(b)).map(b => b.publicId);
244
282
  const conditions = [];
245
- if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && (0, _featureFlags().getFeatureFlag)('conditionalBundlingAsyncRuntime')) {
283
+ if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
246
284
  const conditionSet = this.bundleGraph.getConditionalBundleMapping().get(bundle.id);
247
285
  for (const [key, {
248
286
  ifTrueBundles,
@@ -265,6 +303,7 @@ class ScopeHoistingPackager {
265
303
  maxConcurrent: 32
266
304
  });
267
305
  let wrapped = [];
306
+ let constant = [];
268
307
  this.bundle.traverseAssets(asset => {
269
308
  queue.add(async () => {
270
309
  let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
@@ -278,38 +317,74 @@ class ScopeHoistingPackager {
278
317
  if (!asset.meta.isConstantModule || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.priority === 'lazy')) {
279
318
  this.wrappedAssets.add(asset.id);
280
319
  wrapped.push(asset);
320
+ } else if (((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || (0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) && asset.meta.isConstantModule) {
321
+ constant.push(asset);
281
322
  }
282
323
  }
283
324
  });
284
- for (let wrappedAssetRoot of [...wrapped]) {
285
- this.bundle.traverseAssets((asset, _, actions) => {
286
- if (asset === wrappedAssetRoot) {
287
- return;
288
- }
289
- if (this.wrappedAssets.has(asset.id)) {
290
- actions.skipChildren();
291
- return;
292
- }
293
- // This prevents children of a wrapped asset also being wrapped - it's an "unsafe" optimisation
294
- // that should only be used when you know (or think you know) what you're doing.
295
- //
296
- // In particular this can force an async bundle to be scope hoisted where it previously would not be
297
- // due to the entry asset being wrapped.
298
- if (this.forceSkipWrapAssets.length > 0 && this.forceSkipWrapAssets.some(p => p === _path().default.relative(this.options.projectRoot, asset.filePath))) {
299
- this.logger.verbose({
300
- message: `Force skipping wrapping of ${_path().default.relative(this.options.projectRoot, asset.filePath)}`
301
- });
302
- actions.skipChildren();
303
- return;
304
- }
305
- if (!asset.meta.isConstantModule) {
306
- this.wrappedAssets.add(asset.id);
307
- wrapped.push(asset);
325
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
326
+ // Make all entry assets wrapped, to avoid any top level hoisting
327
+ for (let entryAsset of this.bundle.getEntryAssets()) {
328
+ if (!this.wrappedAssets.has(entryAsset.id)) {
329
+ this.wrappedAssets.add(entryAsset.id);
330
+ wrapped.push(entryAsset);
308
331
  }
309
- }, wrappedAssetRoot);
332
+ }
333
+
334
+ // Tracks which assets have been assigned to a wrap group
335
+ let assignedAssets = new Set();
336
+ for (let wrappedAsset of wrapped) {
337
+ this.bundle.traverseAssets((asset, _, actions) => {
338
+ if (asset === wrappedAsset) {
339
+ return;
340
+ }
341
+ if (this.wrappedAssets.has(asset.id)) {
342
+ actions.skipChildren();
343
+ return;
344
+ }
345
+ if (!asset.meta.isConstantModule && (assignedAssets.has(asset) || this.isReExported(asset))) {
346
+ wrapped.push(asset);
347
+ this.wrappedAssets.add(asset.id);
348
+ actions.skipChildren();
349
+ return;
350
+ }
351
+ assignedAssets.add(asset);
352
+ }, wrappedAsset);
353
+ }
354
+ } else {
355
+ for (let wrappedAssetRoot of [...wrapped]) {
356
+ this.bundle.traverseAssets((asset, _, actions) => {
357
+ if (asset === wrappedAssetRoot) {
358
+ return;
359
+ }
360
+ if (this.wrappedAssets.has(asset.id)) {
361
+ actions.skipChildren();
362
+ return;
363
+ }
364
+ if (!asset.meta.isConstantModule) {
365
+ this.wrappedAssets.add(asset.id);
366
+ wrapped.push(asset);
367
+ }
368
+ }, wrappedAssetRoot);
369
+ }
310
370
  }
371
+
372
+ // @ts-expect-error TS2769
311
373
  this.assetOutputs = new Map(await queue.run());
312
- return wrapped;
374
+ return {
375
+ wrapped,
376
+ constant
377
+ };
378
+ }
379
+ isReExported(asset) {
380
+ let parentSymbols = this.bundleGraph.getIncomingDependencies(asset).map(dep => this.bundleGraph.getAssetWithDependency(dep)).flatMap(parent => {
381
+ if (parent == null) {
382
+ return [];
383
+ }
384
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
385
+ });
386
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
387
+ return assetSymbols.some(assetSymbol => parentSymbols.some(parentSymbol => parentSymbol.symbol === assetSymbol.symbol));
313
388
  }
314
389
  buildExportedSymbols() {
315
390
  if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
@@ -319,6 +394,7 @@ class ScopeHoistingPackager {
319
394
  // TODO: handle ESM exports of wrapped entry assets...
320
395
  let entry = this.bundle.getMainEntry();
321
396
  if (entry && !this.wrappedAssets.has(entry.id)) {
397
+ // @ts-expect-error TS2345
322
398
  let hasNamespace = entry.symbols.hasExportSymbol('*');
323
399
  for (let {
324
400
  asset,
@@ -334,11 +410,14 @@ class ScopeHoistingPackager {
334
410
  if (hasNamespace && this.isAsyncBundle && exportAs !== '*') {
335
411
  continue;
336
412
  }
337
- let symbols = (_this$exportedSymbols = this.exportedSymbols.get(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;
413
+ let symbols = (_this$exportedSymbols = this.exportedSymbols.get(
414
+ // @ts-expect-error TS2345
415
+ 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
416
  if (!symbols) {
339
417
  symbols = [];
340
418
  this.exportedSymbols.set(symbol, {
341
419
  asset,
420
+ // @ts-expect-error TS2322
342
421
  exportSymbol,
343
422
  local: symbol,
344
423
  exportAs: symbols
@@ -347,6 +426,8 @@ class ScopeHoistingPackager {
347
426
  if (exportAs === '*') {
348
427
  exportAs = 'default';
349
428
  }
429
+
430
+ // @ts-expect-error TS2345
350
431
  symbols.push(exportAs);
351
432
  } else if (symbol === null) {
352
433
  // TODO `meta.exportsIdentifier[exportSymbol]` should be exported
@@ -395,6 +476,9 @@ class ScopeHoistingPackager {
395
476
  } = (0, _nullthrows().default)(this.assetOutputs.get(asset.id));
396
477
  return this.buildAsset(asset, code, map);
397
478
  }
479
+ getAssetFilePath(asset) {
480
+ return _path().default.relative(this.options.projectRoot, asset.filePath);
481
+ }
398
482
  buildAsset(asset, code, map) {
399
483
  let shouldWrap = this.wrappedAssets.has(asset.id);
400
484
  let deps = this.bundleGraph.getDependencies(asset);
@@ -420,6 +504,7 @@ class ScopeHoistingPackager {
420
504
  let [code, map, lines] = this.visitAsset(resolved);
421
505
  depCode += code + '\n';
422
506
  if (sourceMap && map) {
507
+ // @ts-expect-error TS2551
423
508
  sourceMap.addSourceMap(map, lineCount);
424
509
  }
425
510
  lineCount += lines + 1;
@@ -445,6 +530,7 @@ class ScopeHoistingPackager {
445
530
  }
446
531
  code += append;
447
532
  let lineCount = 0;
533
+ // @ts-expect-error TS2552
448
534
  let depContent = [];
449
535
  if (depMap.size === 0 && replacements.size === 0) {
450
536
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
@@ -489,13 +575,33 @@ class ScopeHoistingPackager {
489
575
  // outside our parcelRequire.register wrapper. This is safe because all
490
576
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
491
577
  // asset content where the import statement was.
492
- if (shouldWrap) {
493
- depContent.push(this.visitAsset(resolved));
578
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
579
+ if (!resolved.meta.isConstantModule && !this.wrappedAssets.has(resolved.id)) {
580
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
581
+ if (_utils().debugTools['asset-file-names-in-output']) {
582
+ let resolvedPath = this.getAssetFilePath(resolved);
583
+ res = (0, _outdent().outdent)`
584
+ /* Scope hoisted asset: ${resolvedPath} */
585
+ ${depCode}
586
+ /* End: ${resolvedPath} */
587
+ ${res}
588
+ `;
589
+ lines += 3 + depLines;
590
+ } else {
591
+ res = depCode + '\n' + res;
592
+ lines += 1 + depLines;
593
+ }
594
+ map = depMap;
595
+ }
494
596
  } else {
495
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
496
- res = depCode + '\n' + res;
497
- lines += 1 + depLines;
498
- map = depMap;
597
+ if (shouldWrap) {
598
+ depContent.push(this.visitAsset(resolved));
599
+ } else {
600
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
601
+ res = depCode + '\n' + res;
602
+ lines += 1 + depLines;
603
+ map = depMap;
604
+ }
499
605
  }
500
606
  }
501
607
 
@@ -506,6 +612,7 @@ class ScopeHoistingPackager {
506
612
  sourceMap.offsetLines(lineCount + 1, lines);
507
613
  }
508
614
  if (map) {
615
+ // @ts-expect-error TS2551
509
616
  sourceMap.addSourceMap(map, lineCount);
510
617
  }
511
618
  }
@@ -542,10 +649,15 @@ ${code}
542
649
  });
543
650
  `;
544
651
  lineCount += 2;
652
+ if (_utils().debugTools['asset-file-names-in-output']) {
653
+ code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
654
+ lineCount += 1;
655
+ }
545
656
  for (let [depCode, map, lines] of depContent) {
546
657
  if (!depCode) continue;
547
658
  code += depCode + '\n';
548
659
  if (sourceMap && map) {
660
+ // @ts-expect-error TS2551
549
661
  sourceMap.addSourceMap(map, lineCount);
550
662
  }
551
663
  lineCount += lines + 1;
@@ -592,9 +704,12 @@ ${code}
592
704
  for (let [imported, {
593
705
  local
594
706
  }] of dep.symbols) {
707
+ // @ts-expect-error TS2367
595
708
  if (local === '*') {
596
709
  continue;
597
710
  }
711
+
712
+ // @ts-expect-error TS2345
598
713
  let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
599
714
  replacements.set(local,
600
715
  // If this was an internalized async asset, wrap in a Promise.resolve.
@@ -616,6 +731,7 @@ ${code}
616
731
  // which will be provided to us by the wrapper.
617
732
  if (this.wrappedAssets.has(asset.id) || this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry()) {
618
733
  var _asset$symbols$get;
734
+ // @ts-expect-error TS2345
619
735
  let exportsName = ((_asset$symbols$get = asset.symbols.get('*')) === null || _asset$symbols$get === void 0 ? void 0 : _asset$symbols$get.local) || `$${assetId}$exports`;
620
736
  replacements.set(exportsName, 'module.exports');
621
737
  }
@@ -648,8 +764,11 @@ ${code}
648
764
  local
649
765
  }] of dep.symbols) {
650
766
  // If already imported, just add the already renamed variable to the mapping.
767
+ // @ts-expect-error TS2345
651
768
  let renamed = external.get(imported);
769
+ // @ts-expect-error TS2367
652
770
  if (renamed && local !== '*' && replacements) {
771
+ // @ts-expect-error TS2345
653
772
  replacements.set(local, renamed);
654
773
  continue;
655
774
  }
@@ -662,16 +781,25 @@ ${code}
662
781
  if (referencedBundle) {
663
782
  var _entry$symbols$get3;
664
783
  let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
665
- renamed = ((_entry$symbols$get3 = entry.symbols.get('*')) === null || _entry$symbols$get3 === void 0 ? void 0 : _entry$symbols$get3.local) ?? `$${String(entry.meta.id)}$exports`;
784
+ // @ts-expect-error TS2322
785
+ renamed =
786
+ // @ts-expect-error TS2345
787
+ ((_entry$symbols$get3 = entry.symbols.get('*')) === null || _entry$symbols$get3 === void 0 ? void 0 : _entry$symbols$get3.local) ?? `$${String(entry.meta.id)}$exports`;
666
788
  } else {
667
789
  renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
668
790
  }
791
+
792
+ // @ts-expect-error TS2345
669
793
  external.set('*', renamed);
670
794
  }
795
+
796
+ // @ts-expect-error TS2367
671
797
  if (local !== '*' && replacements) {
672
798
  let replacement;
799
+ // @ts-expect-error TS2367
673
800
  if (imported === '*') {
674
801
  replacement = renamed;
802
+ // @ts-expect-error TS2367
675
803
  } else if (imported === 'default') {
676
804
  let needsDefaultInterop = true;
677
805
  if (referencedBundle) {
@@ -685,32 +813,41 @@ ${code}
685
813
  replacement = `${renamed}.default`;
686
814
  }
687
815
  } else {
816
+ // @ts-expect-error TS2345
688
817
  replacement = this.getPropertyAccess(renamed, imported);
689
818
  }
819
+
820
+ // @ts-expect-error TS2345
690
821
  replacements.set(local, replacement);
691
822
  }
692
823
  } else {
693
824
  let property;
694
825
  if (referencedBundle) {
695
826
  let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
827
+ // @ts-expect-error TS2345
696
828
  if (entry.symbols.hasExportSymbol('*')) {
697
829
  // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
698
830
  // This mirrors the logic in buildExportedSymbols.
699
831
  property = imported;
832
+ // @ts-expect-error TS2322
700
833
  imported = (referencedBundle === null || referencedBundle === void 0 ? void 0 : referencedBundle.env.outputFormat) === 'esmodule' ? 'default' : '*';
701
834
  } else {
835
+ // @ts-expect-error TS2367
702
836
  if (imported === '*') {
703
837
  let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
838
+ // @ts-expect-error TS2367
704
839
  if (local === '*') {
705
840
  // Re-export all symbols.
706
841
  for (let exported of exportedSymbols) {
707
842
  if (exported.symbol) {
843
+ // @ts-expect-error TS2345
708
844
  external.set(exported.exportSymbol, exported.symbol);
709
845
  }
710
846
  }
711
847
  continue;
712
848
  }
713
849
  }
850
+ // @ts-expect-error TS2322
714
851
  renamed = this.bundleGraph.getSymbolResolution(entry, imported, this.bundle).symbol;
715
852
  }
716
853
  }
@@ -719,25 +856,37 @@ ${code}
719
856
  // are deduplicated. We have to prefix the imported name with the bundle id so that
720
857
  // local variables do not shadow it.
721
858
  if (!renamed) {
859
+ // @ts-expect-error TS2345
722
860
  if (this.exportedSymbols.has(local)) {
861
+ // @ts-expect-error TS2322
723
862
  renamed = local;
863
+ // @ts-expect-error TS2367
724
864
  } else if (imported === 'default' || imported === '*') {
725
865
  renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
726
866
  } else {
727
- renamed = this.getTopLevelName(`$${this.bundle.publicId}$${imported}`);
867
+ renamed = this.getTopLevelName(
868
+ // @ts-expect-error TS2731
869
+ `$${this.bundle.publicId}$${imported}`);
728
870
  }
729
871
  }
872
+
873
+ // @ts-expect-error TS2345
730
874
  external.set(imported, renamed);
875
+ // @ts-expect-error TS2367
731
876
  if (local !== '*' && replacements) {
732
877
  let replacement = renamed;
878
+ // @ts-expect-error TS2367
733
879
  if (property === '*') {
734
880
  replacement = renamed;
881
+ // @ts-expect-error TS2367
735
882
  } else if (property === 'default') {
736
883
  replacement = `($parcel$interopDefault(${renamed}))`;
737
884
  this.usedHelpers.add('$parcel$interopDefault');
738
885
  } else if (property) {
886
+ // @ts-expect-error TS2345
739
887
  replacement = this.getPropertyAccess(renamed, property);
740
888
  }
889
+ // @ts-expect-error TS2345
741
890
  replacements.set(local, replacement);
742
891
  }
743
892
  }
@@ -759,6 +908,7 @@ ${code}
759
908
  asset: resolvedAsset,
760
909
  exportSymbol,
761
910
  symbol
911
+ // @ts-expect-error TS2345
762
912
  } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
763
913
  if (resolvedAsset.type !== 'js' || dep && this.bundleGraph.isDependencySkipped(dep)) {
764
914
  // Graceful fallback for non-js imports or when trying to resolve a symbol
@@ -792,7 +942,13 @@ ${code}
792
942
 
793
943
  // If this is an ESM default import of a CJS module with a `default` symbol,
794
944
  // 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') && resolvedAsset.symbols.hasExportSymbol('*') && resolvedAsset.symbols.hasExportSymbol('default') && !resolvedAsset.symbols.hasExportSymbol('__esModule');
945
+ 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') &&
946
+ // @ts-expect-error TS2345
947
+ resolvedAsset.symbols.hasExportSymbol('*') &&
948
+ // @ts-expect-error TS2345
949
+ resolvedAsset.symbols.hasExportSymbol('default') &&
950
+ // @ts-expect-error TS2345
951
+ !resolvedAsset.symbols.hasExportSymbol('__esModule');
796
952
 
797
953
  // Find the namespace object for the resolved module. If wrapped and this
798
954
  // is an inline require (not top-level), use a parcelRequire call, otherwise
@@ -808,7 +964,9 @@ ${code}
808
964
  obj = `$${publicId}`;
809
965
  } else {
810
966
  var _resolvedAsset$symbol;
967
+ // @ts-expect-error TS2345
811
968
  obj = ((_resolvedAsset$symbol = resolvedAsset.symbols.get('*')) === null || _resolvedAsset$symbol === void 0 ? void 0 : _resolvedAsset$symbol.local) || `$${assetId}$exports`;
969
+ // @ts-expect-error TS2345
812
970
  obj = (replacements === null || replacements === void 0 ? void 0 : replacements.get(obj)) || obj;
813
971
  }
814
972
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
@@ -817,6 +975,7 @@ ${code}
817
975
  // Directly use module.exports for wrapped assets importing themselves.
818
976
  return 'module.exports';
819
977
  } else {
978
+ // @ts-expect-error TS2322
820
979
  return obj;
821
980
  }
822
981
  } else if ((!staticExports || isWrapped || !symbol || isExternalCommonJS) && resolvedAsset !== parentAsset) {
@@ -825,15 +984,20 @@ ${code}
825
984
  // than a direct reference. If importing default from a CJS module,
826
985
  // use a helper to check the __esModule flag at runtime.
827
986
  let kind = dep === null || dep === void 0 ? void 0 : dep.meta.kind;
828
- if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' && resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
987
+ if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' &&
988
+ // @ts-expect-error TS2345
989
+ resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
829
990
  this.usedHelpers.add('$parcel$interopDefault');
991
+ // @ts-expect-error TS2731
830
992
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
831
993
  } else {
994
+ // @ts-expect-error TS2345
832
995
  return this.getPropertyAccess(obj, exportSymbol);
833
996
  }
834
997
  } else if (!symbol) {
835
998
  (0, _assert().default)(false, 'Asset was skipped or not found.');
836
999
  } else {
1000
+ // @ts-expect-error TS2322
837
1001
  return (replacements === null || replacements === void 0 ? void 0 : replacements.get(symbol)) || symbol;
838
1002
  }
839
1003
  }
@@ -873,21 +1037,39 @@ ${code}
873
1037
  // If the asset has a namespace export symbol, it is CommonJS.
874
1038
  // If there's no __esModule flag, and default is a used symbol, we need
875
1039
  // to insert an interop helper.
876
- let defaultInterop = asset.symbols.hasExportSymbol('*') && usedSymbols.has('default') && !asset.symbols.hasExportSymbol('__esModule');
877
- let usedNamespace =
878
- // If the asset has * in its used symbols, we might need the exports namespace.
879
- // The one case where this isn't true is in ESM library entries, where the only
880
- // dependency on * is the entry dependency. In this case, we will use ESM exports
881
- // instead of the namespace object.
882
- 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) && (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
883
- // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
884
- // we fallback on the namespace object.
885
- asset.symbols.hasExportSymbol('*') && [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s)) ||
886
- // If the exports has this asset's namespace (e.g. ESM output from CJS input),
887
- // include the namespace object for the default export.
888
- this.exportedSymbols.has(`$${assetId}$exports`) ||
889
- // CommonJS library bundle entries always need a namespace.
890
- this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry();
1040
+ let defaultInterop =
1041
+ // @ts-expect-error TS2345
1042
+ asset.symbols.hasExportSymbol('*') &&
1043
+ // @ts-expect-error TS2345
1044
+ usedSymbols.has('default') &&
1045
+ // @ts-expect-error TS2345
1046
+ !asset.symbols.hasExportSymbol('__esModule');
1047
+ let usedNamespace;
1048
+ if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') && asset.meta.isConstantModule) {
1049
+ // Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
1050
+ usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(dep => this.bundle.hasDependency(dep) &&
1051
+ // @ts-expect-error TS2345
1052
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'));
1053
+ } else {
1054
+ usedNamespace =
1055
+ // If the asset has * in its used symbols, we might need the exports namespace.
1056
+ // The one case where this isn't true is in ESM library entries, where the only
1057
+ // dependency on * is the entry dependency. In this case, we will use ESM exports
1058
+ // instead of the namespace object.
1059
+ // @ts-expect-error TS2345
1060
+ 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) &&
1061
+ // @ts-expect-error TS2345
1062
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
1063
+ // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1064
+ // we fallback on the namespace object.
1065
+ // @ts-expect-error TS2345
1066
+ asset.symbols.hasExportSymbol('*') && [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s)) ||
1067
+ // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1068
+ // include the namespace object for the default export.
1069
+ this.exportedSymbols.has(`$${assetId}$exports`) ||
1070
+ // CommonJS library bundle entries always need a namespace.
1071
+ this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry();
1072
+ }
891
1073
 
892
1074
  // If the asset doesn't have static exports, should wrap, the namespace is used,
893
1075
  // or we need default interop, then we need to synthesize a namespace object for
@@ -905,6 +1087,7 @@ ${code}
905
1087
  // Insert the __esModule interop flag for this module if it has a `default` export
906
1088
  // and the namespace symbol is used.
907
1089
  // TODO: only if required by CJS?
1090
+ // @ts-expect-error TS2345
908
1091
  if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
909
1092
  prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
910
1093
  prependLineCount += 2;
@@ -923,6 +1106,7 @@ ${code}
923
1106
  for (let [imported, {
924
1107
  local
925
1108
  }] of dep.symbols) {
1109
+ // @ts-expect-error TS2367
926
1110
  if (imported === '*' && local === '*') {
927
1111
  if (!resolved) {
928
1112
  // Re-exporting an external module. This should have already been handled in buildReplacements.
@@ -935,20 +1119,29 @@ ${code}
935
1119
  // If the resolved asset has an exports object, use the $parcel$exportWildcard helper
936
1120
  // to re-export all symbols. Otherwise, if there's no namespace object available, add
937
1121
  // $parcel$export calls for each used symbol of the dependency.
938
- if (isWrapped || resolved.meta.staticExports === false || (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
1122
+ if (isWrapped || resolved.meta.staticExports === false ||
1123
+ // @ts-expect-error TS2345
1124
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
939
1125
  // an empty asset
940
- !resolved.meta.hasCJSExports && resolved.symbols.hasExportSymbol('*')) {
1126
+ !resolved.meta.hasCJSExports &&
1127
+ // @ts-expect-error TS2345
1128
+ resolved.symbols.hasExportSymbol('*')) {
941
1129
  let obj = this.getSymbolResolution(asset, resolved, '*', dep, replacements);
942
1130
  append += `$parcel$exportWildcard($${assetId}$exports, ${obj});\n`;
943
1131
  this.usedHelpers.add('$parcel$exportWildcard');
944
1132
  } else {
945
1133
  for (let symbol of (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep))) {
946
- if (symbol === 'default' ||
1134
+ if (
1135
+ // @ts-expect-error TS2367
1136
+ symbol === 'default' ||
947
1137
  // `export * as ...` does not include the default export
1138
+ // @ts-expect-error TS2367
948
1139
  symbol === '__esModule') {
949
1140
  continue;
950
1141
  }
951
- let resolvedSymbol = this.getSymbolResolution(asset, resolved, symbol, undefined, replacements);
1142
+ let resolvedSymbol = this.getSymbolResolution(asset, resolved,
1143
+ // @ts-expect-error TS2345
1144
+ symbol, undefined, replacements);
952
1145
  let get = this.buildFunctionExpression([], resolvedSymbol);
953
1146
  let set = asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolvedSymbol} = v`) : '';
954
1147
  prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(symbol)}, ${get}${set});\n`;
@@ -965,6 +1158,7 @@ ${code}
965
1158
  // re-exported symbols rather than only symbols declared in this asset.
966
1159
  let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
967
1160
  let usedExports = [...asset.symbols.exportSymbols()].filter(symbol => {
1161
+ // @ts-expect-error TS2367
968
1162
  if (symbol === '*') {
969
1163
  return false;
970
1164
  }
@@ -979,6 +1173,8 @@ ${code}
979
1173
 
980
1174
  // No used symbols available for the asset, make sure we keep all of them
981
1175
  if (!symbols) return false;
1176
+
1177
+ // @ts-expect-error TS2345
982
1178
  return !symbols.has(symbol) && !symbols.has('*');
983
1179
  });
984
1180
  return !unused;
@@ -990,7 +1186,9 @@ ${code}
990
1186
  // additional assignments after each mutation of the original binding.
991
1187
  prepend += `\n${usedExports.map(exp => {
992
1188
  var _asset$symbols$get2;
993
- let resolved = this.getSymbolResolution(asset, asset, exp, undefined, replacements);
1189
+ let resolved = this.getSymbolResolution(asset, asset,
1190
+ // @ts-expect-error TS2345
1191
+ exp, undefined, replacements);
994
1192
  let get = this.buildFunctionExpression([], resolved);
995
1193
  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
1194
  let set = !isEsmExport && asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`) : '';
@@ -1028,8 +1226,10 @@ ${code}
1028
1226
  this.usedHelpers.add('$parcel$global');
1029
1227
  }
1030
1228
  for (let helper of this.usedHelpers) {
1229
+ // @ts-expect-error TS7053
1031
1230
  let currentHelper = _helpers.helpers[helper];
1032
1231
  if (typeof currentHelper === 'function') {
1232
+ // @ts-expect-error TS7053
1033
1233
  currentHelper = _helpers.helpers[helper](this.bundle.env);
1034
1234
  }
1035
1235
  res += currentHelper;
@@ -1041,7 +1241,7 @@ ${code}
1041
1241
  // Add the prelude if this is potentially the first JS bundle to load in a
1042
1242
  // particular context (e.g. entry scripts in HTML, workers, etc.).
1043
1243
  let parentBundles = this.bundleGraph.getParentBundles(this.bundle);
1044
- let mightBeFirstJS = parentBundles.length === 0 || parentBundles.some(b => b.type !== 'js') || this.bundleGraph.getBundleGroupsContainingBundle(this.bundle).some(g => this.bundleGraph.isEntryBundleGroup(g)) || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated' ||
1244
+ let mightBeFirstJS = parentBundles.length === 0 || parentBundles.some(b => b.type !== 'js') || this.bundleGraph.getBundleGroupsContainingBundle(this.bundle).some(g => this.bundleGraph.isEntryBundleGroup(g)) || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated' || this.bundle.bundleBehavior === 'inlineIsolated' ||
1045
1245
  // Conditional deps may be loaded before entrypoints on the server
1046
1246
  this.hasConditionalDependency();
1047
1247
  if (mightBeFirstJS) {
@@ -1068,7 +1268,7 @@ ${code}
1068
1268
  }
1069
1269
 
1070
1270
  // Add importScripts for sibling bundles in workers.
1071
- if (this.bundle.env.isWorker() || this.bundle.env.isWorklet()) {
1271
+ if (this.bundle.env.isWorker() || this.bundle.env.isTesseract() || this.bundle.env.isWorklet()) {
1072
1272
  let importScripts = '';
1073
1273
  let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
1074
1274
  for (let b of bundles) {
@@ -1085,7 +1285,11 @@ ${code}
1085
1285
  return [res, lines];
1086
1286
  }
1087
1287
  needsDefaultInterop(asset) {
1088
- if (asset.symbols.hasExportSymbol('*') && !asset.symbols.hasExportSymbol('default')) {
1288
+ if (
1289
+ // @ts-expect-error TS2345
1290
+ asset.symbols.hasExportSymbol('*') &&
1291
+ // @ts-expect-error TS2345
1292
+ !asset.symbols.hasExportSymbol('default')) {
1089
1293
  return true;
1090
1294
  }
1091
1295
  return false;