@atlaspack/packager-js 2.14.5-canary.16 → 2.14.5-canary.160

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,21 @@ 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
193
  // In async bundles we don't want the main entry to execute until we require it
173
194
  // 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
- });
195
+ if ((0, _featureFlags().getFeatureFlag)('supportWebpackChunkName')) {
196
+ entries = [];
197
+ } else {
198
+ entries = entries.filter(a => {
199
+ var _mainEntry;
200
+ return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
201
+ });
202
+ }
178
203
  mainEntry = null;
179
204
  }
180
205
  let needsBundleQueue = this.shouldBundleQueue(this.bundle);
@@ -184,9 +209,14 @@ class ScopeHoistingPackager {
184
209
  if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
185
210
  var _entry$symbols$get;
186
211
  let parcelRequire = `parcelRequire(${JSON.stringify(this.bundleGraph.getAssetPublicId(entry))});\n`;
212
+
213
+ // @ts-expect-error TS2345
187
214
  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)) {
215
+ if (entryExports && entry === mainEntry &&
216
+ // @ts-expect-error TS2345
217
+ this.exportedSymbols.has(entryExports)) {
189
218
  (0, _assert().default)(!needsBundleQueue, 'Entry exports are not yet compaitble with async bundles');
219
+ // @ts-expect-error TS2731
190
220
  res += `\nvar ${entryExports} = ${parcelRequire}`;
191
221
  } else {
192
222
  if (needsBundleQueue) {
@@ -218,6 +248,7 @@ class ScopeHoistingPackager {
218
248
  }
219
249
  res += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, code, map, this.parcelRequireName);
220
250
  if (sourceMap && map) {
251
+ // @ts-expect-error TS2339
221
252
  sourceMap.addSourceMap(map, lineCount);
222
253
  }
223
254
  }
@@ -231,18 +262,18 @@ class ScopeHoistingPackager {
231
262
  let hasHtmlReference = referencingBundles.some(b => b.type === 'html');
232
263
  let hasConditionalReference = false;
233
264
  let isConditionalBundle = false;
234
- if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && (0, _featureFlags().getFeatureFlag)('conditionalBundlingAsyncRuntime')) {
265
+ if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
235
266
  // If the bundle has a conditional bundle reference (has an importCond)
236
267
  hasConditionalReference = this.bundleGraph.getReferencedConditionalBundles(bundle).length > 0;
237
268
  // If the bundle is a conditional bundle
238
269
  isConditionalBundle = this.hasConditionalDependency();
239
270
  }
240
- return this.useAsyncBundleRuntime && bundle.type === 'js' && bundle.bundleBehavior !== 'inline' && bundle.env.outputFormat === 'esmodule' && !bundle.env.isIsolated() && bundle.bundleBehavior !== 'isolated' && (hasHtmlReference || hasConditionalReference || isConditionalBundle);
271
+ 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
272
  }
242
273
  runWhenReady(bundle, codeToRun) {
243
274
  let deps = this.bundleGraph.getReferencedBundles(bundle).filter(b => this.shouldBundleQueue(b)).map(b => b.publicId);
244
275
  const conditions = [];
245
- if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi') && (0, _featureFlags().getFeatureFlag)('conditionalBundlingAsyncRuntime')) {
276
+ if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingApi')) {
246
277
  const conditionSet = this.bundleGraph.getConditionalBundleMapping().get(bundle.id);
247
278
  for (const [key, {
248
279
  ifTrueBundles,
@@ -265,6 +296,7 @@ class ScopeHoistingPackager {
265
296
  maxConcurrent: 32
266
297
  });
267
298
  let wrapped = [];
299
+ let constant = [];
268
300
  this.bundle.traverseAssets(asset => {
269
301
  queue.add(async () => {
270
302
  let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
@@ -278,38 +310,74 @@ class ScopeHoistingPackager {
278
310
  if (!asset.meta.isConstantModule || this.bundleGraph.getIncomingDependencies(asset).some(dep => dep.priority === 'lazy')) {
279
311
  this.wrappedAssets.add(asset.id);
280
312
  wrapped.push(asset);
313
+ } else if (((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') || (0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) && asset.meta.isConstantModule) {
314
+ constant.push(asset);
281
315
  }
282
316
  }
283
317
  });
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);
318
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
319
+ // Make all entry assets wrapped, to avoid any top level hoisting
320
+ for (let entryAsset of this.bundle.getEntryAssets()) {
321
+ if (!this.wrappedAssets.has(entryAsset.id)) {
322
+ this.wrappedAssets.add(entryAsset.id);
323
+ wrapped.push(entryAsset);
308
324
  }
309
- }, wrappedAssetRoot);
325
+ }
326
+
327
+ // Tracks which assets have been assigned to a wrap group
328
+ let assignedAssets = new Set();
329
+ for (let wrappedAsset of wrapped) {
330
+ this.bundle.traverseAssets((asset, _, actions) => {
331
+ if (asset === wrappedAsset) {
332
+ return;
333
+ }
334
+ if (this.wrappedAssets.has(asset.id)) {
335
+ actions.skipChildren();
336
+ return;
337
+ }
338
+ if (!asset.meta.isConstantModule && (assignedAssets.has(asset) || this.isReExported(asset))) {
339
+ wrapped.push(asset);
340
+ this.wrappedAssets.add(asset.id);
341
+ actions.skipChildren();
342
+ return;
343
+ }
344
+ assignedAssets.add(asset);
345
+ }, wrappedAsset);
346
+ }
347
+ } else {
348
+ for (let wrappedAssetRoot of [...wrapped]) {
349
+ this.bundle.traverseAssets((asset, _, actions) => {
350
+ if (asset === wrappedAssetRoot) {
351
+ return;
352
+ }
353
+ if (this.wrappedAssets.has(asset.id)) {
354
+ actions.skipChildren();
355
+ return;
356
+ }
357
+ if (!asset.meta.isConstantModule) {
358
+ this.wrappedAssets.add(asset.id);
359
+ wrapped.push(asset);
360
+ }
361
+ }, wrappedAssetRoot);
362
+ }
310
363
  }
364
+
365
+ // @ts-expect-error TS2769
311
366
  this.assetOutputs = new Map(await queue.run());
312
- return wrapped;
367
+ return {
368
+ wrapped,
369
+ constant
370
+ };
371
+ }
372
+ isReExported(asset) {
373
+ let parentSymbols = this.bundleGraph.getIncomingDependencies(asset).map(dep => this.bundleGraph.getAssetWithDependency(dep)).flatMap(parent => {
374
+ if (parent == null) {
375
+ return [];
376
+ }
377
+ return this.bundleGraph.getExportedSymbols(parent, this.bundle);
378
+ });
379
+ let assetSymbols = this.bundleGraph.getExportedSymbols(asset, this.bundle);
380
+ return assetSymbols.some(assetSymbol => parentSymbols.some(parentSymbol => parentSymbol.symbol === assetSymbol.symbol));
313
381
  }
314
382
  buildExportedSymbols() {
315
383
  if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
@@ -319,6 +387,7 @@ class ScopeHoistingPackager {
319
387
  // TODO: handle ESM exports of wrapped entry assets...
320
388
  let entry = this.bundle.getMainEntry();
321
389
  if (entry && !this.wrappedAssets.has(entry.id)) {
390
+ // @ts-expect-error TS2345
322
391
  let hasNamespace = entry.symbols.hasExportSymbol('*');
323
392
  for (let {
324
393
  asset,
@@ -334,11 +403,14 @@ class ScopeHoistingPackager {
334
403
  if (hasNamespace && this.isAsyncBundle && exportAs !== '*') {
335
404
  continue;
336
405
  }
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;
406
+ let symbols = (_this$exportedSymbols = this.exportedSymbols.get(
407
+ // @ts-expect-error TS2345
408
+ 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
409
  if (!symbols) {
339
410
  symbols = [];
340
411
  this.exportedSymbols.set(symbol, {
341
412
  asset,
413
+ // @ts-expect-error TS2322
342
414
  exportSymbol,
343
415
  local: symbol,
344
416
  exportAs: symbols
@@ -347,6 +419,8 @@ class ScopeHoistingPackager {
347
419
  if (exportAs === '*') {
348
420
  exportAs = 'default';
349
421
  }
422
+
423
+ // @ts-expect-error TS2345
350
424
  symbols.push(exportAs);
351
425
  } else if (symbol === null) {
352
426
  // TODO `meta.exportsIdentifier[exportSymbol]` should be exported
@@ -395,6 +469,9 @@ class ScopeHoistingPackager {
395
469
  } = (0, _nullthrows().default)(this.assetOutputs.get(asset.id));
396
470
  return this.buildAsset(asset, code, map);
397
471
  }
472
+ getAssetFilePath(asset) {
473
+ return _path().default.relative(this.options.projectRoot, asset.filePath);
474
+ }
398
475
  buildAsset(asset, code, map) {
399
476
  let shouldWrap = this.wrappedAssets.has(asset.id);
400
477
  let deps = this.bundleGraph.getDependencies(asset);
@@ -420,6 +497,7 @@ class ScopeHoistingPackager {
420
497
  let [code, map, lines] = this.visitAsset(resolved);
421
498
  depCode += code + '\n';
422
499
  if (sourceMap && map) {
500
+ // @ts-expect-error TS2551
423
501
  sourceMap.addSourceMap(map, lineCount);
424
502
  }
425
503
  lineCount += lines + 1;
@@ -445,6 +523,7 @@ class ScopeHoistingPackager {
445
523
  }
446
524
  code += append;
447
525
  let lineCount = 0;
526
+ // @ts-expect-error TS2552
448
527
  let depContent = [];
449
528
  if (depMap.size === 0 && replacements.size === 0) {
450
529
  // If there are no dependencies or replacements, use a simple function to count the number of lines.
@@ -489,13 +568,33 @@ class ScopeHoistingPackager {
489
568
  // outside our parcelRequire.register wrapper. This is safe because all
490
569
  // assets referenced by this asset will also be wrapped. Otherwise, inline the
491
570
  // asset content where the import statement was.
492
- if (shouldWrap) {
493
- depContent.push(this.visitAsset(resolved));
571
+ if ((0, _featureFlags().getFeatureFlag)('applyScopeHoistingImprovement')) {
572
+ if (!resolved.meta.isConstantModule && !this.wrappedAssets.has(resolved.id)) {
573
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
574
+ if (_utils().debugTools['asset-file-names-in-output']) {
575
+ let resolvedPath = this.getAssetFilePath(resolved);
576
+ res = (0, _outdent().outdent)`
577
+ /* Scope hoisted asset: ${resolvedPath} */
578
+ ${depCode}
579
+ /* End: ${resolvedPath} */
580
+ ${res}
581
+ `;
582
+ lines += 3 + depLines;
583
+ } else {
584
+ res = depCode + '\n' + res;
585
+ lines += 1 + depLines;
586
+ }
587
+ map = depMap;
588
+ }
494
589
  } else {
495
- let [depCode, depMap, depLines] = this.visitAsset(resolved);
496
- res = depCode + '\n' + res;
497
- lines += 1 + depLines;
498
- map = depMap;
590
+ if (shouldWrap) {
591
+ depContent.push(this.visitAsset(resolved));
592
+ } else {
593
+ let [depCode, depMap, depLines] = this.visitAsset(resolved);
594
+ res = depCode + '\n' + res;
595
+ lines += 1 + depLines;
596
+ map = depMap;
597
+ }
499
598
  }
500
599
  }
501
600
 
@@ -506,6 +605,7 @@ class ScopeHoistingPackager {
506
605
  sourceMap.offsetLines(lineCount + 1, lines);
507
606
  }
508
607
  if (map) {
608
+ // @ts-expect-error TS2551
509
609
  sourceMap.addSourceMap(map, lineCount);
510
610
  }
511
611
  }
@@ -542,10 +642,15 @@ ${code}
542
642
  });
543
643
  `;
544
644
  lineCount += 2;
645
+ if (_utils().debugTools['asset-file-names-in-output']) {
646
+ code = `/* ${this.getAssetFilePath(asset)} */\n` + code;
647
+ lineCount += 1;
648
+ }
545
649
  for (let [depCode, map, lines] of depContent) {
546
650
  if (!depCode) continue;
547
651
  code += depCode + '\n';
548
652
  if (sourceMap && map) {
653
+ // @ts-expect-error TS2551
549
654
  sourceMap.addSourceMap(map, lineCount);
550
655
  }
551
656
  lineCount += lines + 1;
@@ -592,9 +697,12 @@ ${code}
592
697
  for (let [imported, {
593
698
  local
594
699
  }] of dep.symbols) {
700
+ // @ts-expect-error TS2367
595
701
  if (local === '*') {
596
702
  continue;
597
703
  }
704
+
705
+ // @ts-expect-error TS2345
598
706
  let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
599
707
  replacements.set(local,
600
708
  // If this was an internalized async asset, wrap in a Promise.resolve.
@@ -616,6 +724,7 @@ ${code}
616
724
  // which will be provided to us by the wrapper.
617
725
  if (this.wrappedAssets.has(asset.id) || this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry()) {
618
726
  var _asset$symbols$get;
727
+ // @ts-expect-error TS2345
619
728
  let exportsName = ((_asset$symbols$get = asset.symbols.get('*')) === null || _asset$symbols$get === void 0 ? void 0 : _asset$symbols$get.local) || `$${assetId}$exports`;
620
729
  replacements.set(exportsName, 'module.exports');
621
730
  }
@@ -648,8 +757,11 @@ ${code}
648
757
  local
649
758
  }] of dep.symbols) {
650
759
  // If already imported, just add the already renamed variable to the mapping.
760
+ // @ts-expect-error TS2345
651
761
  let renamed = external.get(imported);
762
+ // @ts-expect-error TS2367
652
763
  if (renamed && local !== '*' && replacements) {
764
+ // @ts-expect-error TS2345
653
765
  replacements.set(local, renamed);
654
766
  continue;
655
767
  }
@@ -662,16 +774,25 @@ ${code}
662
774
  if (referencedBundle) {
663
775
  var _entry$symbols$get3;
664
776
  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`;
777
+ // @ts-expect-error TS2322
778
+ renamed =
779
+ // @ts-expect-error TS2345
780
+ ((_entry$symbols$get3 = entry.symbols.get('*')) === null || _entry$symbols$get3 === void 0 ? void 0 : _entry$symbols$get3.local) ?? `$${String(entry.meta.id)}$exports`;
666
781
  } else {
667
782
  renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
668
783
  }
784
+
785
+ // @ts-expect-error TS2345
669
786
  external.set('*', renamed);
670
787
  }
788
+
789
+ // @ts-expect-error TS2367
671
790
  if (local !== '*' && replacements) {
672
791
  let replacement;
792
+ // @ts-expect-error TS2367
673
793
  if (imported === '*') {
674
794
  replacement = renamed;
795
+ // @ts-expect-error TS2367
675
796
  } else if (imported === 'default') {
676
797
  let needsDefaultInterop = true;
677
798
  if (referencedBundle) {
@@ -685,32 +806,41 @@ ${code}
685
806
  replacement = `${renamed}.default`;
686
807
  }
687
808
  } else {
809
+ // @ts-expect-error TS2345
688
810
  replacement = this.getPropertyAccess(renamed, imported);
689
811
  }
812
+
813
+ // @ts-expect-error TS2345
690
814
  replacements.set(local, replacement);
691
815
  }
692
816
  } else {
693
817
  let property;
694
818
  if (referencedBundle) {
695
819
  let entry = (0, _nullthrows().default)(referencedBundle.getMainEntry());
820
+ // @ts-expect-error TS2345
696
821
  if (entry.symbols.hasExportSymbol('*')) {
697
822
  // If importing * and the referenced module has a * export (e.g. CJS), use default instead.
698
823
  // This mirrors the logic in buildExportedSymbols.
699
824
  property = imported;
825
+ // @ts-expect-error TS2322
700
826
  imported = (referencedBundle === null || referencedBundle === void 0 ? void 0 : referencedBundle.env.outputFormat) === 'esmodule' ? 'default' : '*';
701
827
  } else {
828
+ // @ts-expect-error TS2367
702
829
  if (imported === '*') {
703
830
  let exportedSymbols = this.bundleGraph.getExportedSymbols(entry);
831
+ // @ts-expect-error TS2367
704
832
  if (local === '*') {
705
833
  // Re-export all symbols.
706
834
  for (let exported of exportedSymbols) {
707
835
  if (exported.symbol) {
836
+ // @ts-expect-error TS2345
708
837
  external.set(exported.exportSymbol, exported.symbol);
709
838
  }
710
839
  }
711
840
  continue;
712
841
  }
713
842
  }
843
+ // @ts-expect-error TS2322
714
844
  renamed = this.bundleGraph.getSymbolResolution(entry, imported, this.bundle).symbol;
715
845
  }
716
846
  }
@@ -719,25 +849,37 @@ ${code}
719
849
  // are deduplicated. We have to prefix the imported name with the bundle id so that
720
850
  // local variables do not shadow it.
721
851
  if (!renamed) {
852
+ // @ts-expect-error TS2345
722
853
  if (this.exportedSymbols.has(local)) {
854
+ // @ts-expect-error TS2322
723
855
  renamed = local;
856
+ // @ts-expect-error TS2367
724
857
  } else if (imported === 'default' || imported === '*') {
725
858
  renamed = this.getTopLevelName(`$${this.bundle.publicId}$${specifier}`);
726
859
  } else {
727
- renamed = this.getTopLevelName(`$${this.bundle.publicId}$${imported}`);
860
+ renamed = this.getTopLevelName(
861
+ // @ts-expect-error TS2731
862
+ `$${this.bundle.publicId}$${imported}`);
728
863
  }
729
864
  }
865
+
866
+ // @ts-expect-error TS2345
730
867
  external.set(imported, renamed);
868
+ // @ts-expect-error TS2367
731
869
  if (local !== '*' && replacements) {
732
870
  let replacement = renamed;
871
+ // @ts-expect-error TS2367
733
872
  if (property === '*') {
734
873
  replacement = renamed;
874
+ // @ts-expect-error TS2367
735
875
  } else if (property === 'default') {
736
876
  replacement = `($parcel$interopDefault(${renamed}))`;
737
877
  this.usedHelpers.add('$parcel$interopDefault');
738
878
  } else if (property) {
879
+ // @ts-expect-error TS2345
739
880
  replacement = this.getPropertyAccess(renamed, property);
740
881
  }
882
+ // @ts-expect-error TS2345
741
883
  replacements.set(local, replacement);
742
884
  }
743
885
  }
@@ -759,6 +901,7 @@ ${code}
759
901
  asset: resolvedAsset,
760
902
  exportSymbol,
761
903
  symbol
904
+ // @ts-expect-error TS2345
762
905
  } = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
763
906
  if (resolvedAsset.type !== 'js' || dep && this.bundleGraph.isDependencySkipped(dep)) {
764
907
  // Graceful fallback for non-js imports or when trying to resolve a symbol
@@ -792,7 +935,13 @@ ${code}
792
935
 
793
936
  // If this is an ESM default import of a CJS module with a `default` symbol,
794
937
  // 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');
938
+ 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') &&
939
+ // @ts-expect-error TS2345
940
+ resolvedAsset.symbols.hasExportSymbol('*') &&
941
+ // @ts-expect-error TS2345
942
+ resolvedAsset.symbols.hasExportSymbol('default') &&
943
+ // @ts-expect-error TS2345
944
+ !resolvedAsset.symbols.hasExportSymbol('__esModule');
796
945
 
797
946
  // Find the namespace object for the resolved module. If wrapped and this
798
947
  // is an inline require (not top-level), use a parcelRequire call, otherwise
@@ -808,7 +957,9 @@ ${code}
808
957
  obj = `$${publicId}`;
809
958
  } else {
810
959
  var _resolvedAsset$symbol;
960
+ // @ts-expect-error TS2345
811
961
  obj = ((_resolvedAsset$symbol = resolvedAsset.symbols.get('*')) === null || _resolvedAsset$symbol === void 0 ? void 0 : _resolvedAsset$symbol.local) || `$${assetId}$exports`;
962
+ // @ts-expect-error TS2345
812
963
  obj = (replacements === null || replacements === void 0 ? void 0 : replacements.get(obj)) || obj;
813
964
  }
814
965
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
@@ -817,6 +968,7 @@ ${code}
817
968
  // Directly use module.exports for wrapped assets importing themselves.
818
969
  return 'module.exports';
819
970
  } else {
971
+ // @ts-expect-error TS2322
820
972
  return obj;
821
973
  }
822
974
  } else if ((!staticExports || isWrapped || !symbol || isExternalCommonJS) && resolvedAsset !== parentAsset) {
@@ -825,15 +977,20 @@ ${code}
825
977
  // than a direct reference. If importing default from a CJS module,
826
978
  // use a helper to check the __esModule flag at runtime.
827
979
  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)) {
980
+ if ((!dep || kind === 'Import' || kind === 'Export') && exportSymbol === 'default' &&
981
+ // @ts-expect-error TS2345
982
+ resolvedAsset.symbols.hasExportSymbol('*') && this.needsDefaultInterop(resolvedAsset)) {
829
983
  this.usedHelpers.add('$parcel$interopDefault');
984
+ // @ts-expect-error TS2731
830
985
  return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
831
986
  } else {
987
+ // @ts-expect-error TS2345
832
988
  return this.getPropertyAccess(obj, exportSymbol);
833
989
  }
834
990
  } else if (!symbol) {
835
991
  (0, _assert().default)(false, 'Asset was skipped or not found.');
836
992
  } else {
993
+ // @ts-expect-error TS2322
837
994
  return (replacements === null || replacements === void 0 ? void 0 : replacements.get(symbol)) || symbol;
838
995
  }
839
996
  }
@@ -873,21 +1030,39 @@ ${code}
873
1030
  // If the asset has a namespace export symbol, it is CommonJS.
874
1031
  // If there's no __esModule flag, and default is a used symbol, we need
875
1032
  // 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();
1033
+ let defaultInterop =
1034
+ // @ts-expect-error TS2345
1035
+ asset.symbols.hasExportSymbol('*') &&
1036
+ // @ts-expect-error TS2345
1037
+ usedSymbols.has('default') &&
1038
+ // @ts-expect-error TS2345
1039
+ !asset.symbols.hasExportSymbol('__esModule');
1040
+ let usedNamespace;
1041
+ if ((0, _featureFlags().getFeatureFlag)('inlineConstOptimisationFix') && asset.meta.isConstantModule) {
1042
+ // Only set usedNamespace if there is an incoming dependency in the current bundle that uses '*'
1043
+ usedNamespace = this.bundleGraph.getIncomingDependencies(asset).some(dep => this.bundle.hasDependency(dep) &&
1044
+ // @ts-expect-error TS2345
1045
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'));
1046
+ } else {
1047
+ usedNamespace =
1048
+ // If the asset has * in its used symbols, we might need the exports namespace.
1049
+ // The one case where this isn't true is in ESM library entries, where the only
1050
+ // dependency on * is the entry dependency. In this case, we will use ESM exports
1051
+ // instead of the namespace object.
1052
+ // @ts-expect-error TS2345
1053
+ 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) &&
1054
+ // @ts-expect-error TS2345
1055
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep)).has('*'))) ||
1056
+ // If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
1057
+ // we fallback on the namespace object.
1058
+ // @ts-expect-error TS2345
1059
+ asset.symbols.hasExportSymbol('*') && [...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s)) ||
1060
+ // If the exports has this asset's namespace (e.g. ESM output from CJS input),
1061
+ // include the namespace object for the default export.
1062
+ this.exportedSymbols.has(`$${assetId}$exports`) ||
1063
+ // CommonJS library bundle entries always need a namespace.
1064
+ this.bundle.env.isLibrary && this.bundle.env.outputFormat === 'commonjs' && asset === this.bundle.getMainEntry();
1065
+ }
891
1066
 
892
1067
  // If the asset doesn't have static exports, should wrap, the namespace is used,
893
1068
  // or we need default interop, then we need to synthesize a namespace object for
@@ -905,6 +1080,7 @@ ${code}
905
1080
  // Insert the __esModule interop flag for this module if it has a `default` export
906
1081
  // and the namespace symbol is used.
907
1082
  // TODO: only if required by CJS?
1083
+ // @ts-expect-error TS2345
908
1084
  if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
909
1085
  prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
910
1086
  prependLineCount += 2;
@@ -923,6 +1099,7 @@ ${code}
923
1099
  for (let [imported, {
924
1100
  local
925
1101
  }] of dep.symbols) {
1102
+ // @ts-expect-error TS2367
926
1103
  if (imported === '*' && local === '*') {
927
1104
  if (!resolved) {
928
1105
  // Re-exporting an external module. This should have already been handled in buildReplacements.
@@ -935,20 +1112,29 @@ ${code}
935
1112
  // If the resolved asset has an exports object, use the $parcel$exportWildcard helper
936
1113
  // to re-export all symbols. Otherwise, if there's no namespace object available, add
937
1114
  // $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('*') ||
1115
+ if (isWrapped || resolved.meta.staticExports === false ||
1116
+ // @ts-expect-error TS2345
1117
+ (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(resolved)).has('*') ||
939
1118
  // an empty asset
940
- !resolved.meta.hasCJSExports && resolved.symbols.hasExportSymbol('*')) {
1119
+ !resolved.meta.hasCJSExports &&
1120
+ // @ts-expect-error TS2345
1121
+ resolved.symbols.hasExportSymbol('*')) {
941
1122
  let obj = this.getSymbolResolution(asset, resolved, '*', dep, replacements);
942
1123
  append += `$parcel$exportWildcard($${assetId}$exports, ${obj});\n`;
943
1124
  this.usedHelpers.add('$parcel$exportWildcard');
944
1125
  } else {
945
1126
  for (let symbol of (0, _nullthrows().default)(this.bundleGraph.getUsedSymbols(dep))) {
946
- if (symbol === 'default' ||
1127
+ if (
1128
+ // @ts-expect-error TS2367
1129
+ symbol === 'default' ||
947
1130
  // `export * as ...` does not include the default export
1131
+ // @ts-expect-error TS2367
948
1132
  symbol === '__esModule') {
949
1133
  continue;
950
1134
  }
951
- let resolvedSymbol = this.getSymbolResolution(asset, resolved, symbol, undefined, replacements);
1135
+ let resolvedSymbol = this.getSymbolResolution(asset, resolved,
1136
+ // @ts-expect-error TS2345
1137
+ symbol, undefined, replacements);
952
1138
  let get = this.buildFunctionExpression([], resolvedSymbol);
953
1139
  let set = asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolvedSymbol} = v`) : '';
954
1140
  prepend += `$parcel$export($${assetId}$exports, ${JSON.stringify(symbol)}, ${get}${set});\n`;
@@ -965,6 +1151,7 @@ ${code}
965
1151
  // re-exported symbols rather than only symbols declared in this asset.
966
1152
  let incomingDeps = this.bundleGraph.getIncomingDependencies(asset);
967
1153
  let usedExports = [...asset.symbols.exportSymbols()].filter(symbol => {
1154
+ // @ts-expect-error TS2367
968
1155
  if (symbol === '*') {
969
1156
  return false;
970
1157
  }
@@ -979,6 +1166,8 @@ ${code}
979
1166
 
980
1167
  // No used symbols available for the asset, make sure we keep all of them
981
1168
  if (!symbols) return false;
1169
+
1170
+ // @ts-expect-error TS2345
982
1171
  return !symbols.has(symbol) && !symbols.has('*');
983
1172
  });
984
1173
  return !unused;
@@ -990,7 +1179,9 @@ ${code}
990
1179
  // additional assignments after each mutation of the original binding.
991
1180
  prepend += `\n${usedExports.map(exp => {
992
1181
  var _asset$symbols$get2;
993
- let resolved = this.getSymbolResolution(asset, asset, exp, undefined, replacements);
1182
+ let resolved = this.getSymbolResolution(asset, asset,
1183
+ // @ts-expect-error TS2345
1184
+ exp, undefined, replacements);
994
1185
  let get = this.buildFunctionExpression([], resolved);
995
1186
  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
1187
  let set = !isEsmExport && asset.meta.hasCJSExports ? ', ' + this.buildFunctionExpression(['v'], `${resolved} = v`) : '';
@@ -1028,8 +1219,10 @@ ${code}
1028
1219
  this.usedHelpers.add('$parcel$global');
1029
1220
  }
1030
1221
  for (let helper of this.usedHelpers) {
1222
+ // @ts-expect-error TS7053
1031
1223
  let currentHelper = _helpers.helpers[helper];
1032
1224
  if (typeof currentHelper === 'function') {
1225
+ // @ts-expect-error TS7053
1033
1226
  currentHelper = _helpers.helpers[helper](this.bundle.env);
1034
1227
  }
1035
1228
  res += currentHelper;
@@ -1041,7 +1234,7 @@ ${code}
1041
1234
  // Add the prelude if this is potentially the first JS bundle to load in a
1042
1235
  // particular context (e.g. entry scripts in HTML, workers, etc.).
1043
1236
  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' ||
1237
+ 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
1238
  // Conditional deps may be loaded before entrypoints on the server
1046
1239
  this.hasConditionalDependency();
1047
1240
  if (mightBeFirstJS) {
@@ -1068,7 +1261,7 @@ ${code}
1068
1261
  }
1069
1262
 
1070
1263
  // Add importScripts for sibling bundles in workers.
1071
- if (this.bundle.env.isWorker() || this.bundle.env.isWorklet()) {
1264
+ if (this.bundle.env.isWorker() || this.bundle.env.isTesseract() || this.bundle.env.isWorklet()) {
1072
1265
  let importScripts = '';
1073
1266
  let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
1074
1267
  for (let b of bundles) {
@@ -1085,7 +1278,11 @@ ${code}
1085
1278
  return [res, lines];
1086
1279
  }
1087
1280
  needsDefaultInterop(asset) {
1088
- if (asset.symbols.hasExportSymbol('*') && !asset.symbols.hasExportSymbol('default')) {
1281
+ if (
1282
+ // @ts-expect-error TS2345
1283
+ asset.symbols.hasExportSymbol('*') &&
1284
+ // @ts-expect-error TS2345
1285
+ !asset.symbols.hasExportSymbol('default')) {
1089
1286
  return true;
1090
1287
  }
1091
1288
  return false;