-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathScopeHoistingPackager.js
1244 lines (1109 loc) Β· 41.6 KB
/
ScopeHoistingPackager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @flow
import type {
Asset,
BundleGraph,
Dependency,
PluginOptions,
NamedBundle,
} from '@parcel/types';
import {
DefaultMap,
PromiseQueue,
relativeBundlePath,
countLines,
normalizeSeparators,
} from '@parcel/utils';
import SourceMap from '@parcel/source-map';
import nullthrows from 'nullthrows';
import invariant from 'assert';
import ThrowableDiagnostic from '@parcel/diagnostic';
import globals from 'globals';
import path from 'path';
import {ESMOutputFormat} from './ESMOutputFormat';
import {CJSOutputFormat} from './CJSOutputFormat';
import {GlobalOutputFormat} from './GlobalOutputFormat';
import {prelude, helpers} from './helpers';
import {replaceScriptDependencies, getSpecifier} from './utils';
// https://262.ecma-international.org/6.0/#sec-names-and-keywords
const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
const ID_START_RE = /^[$_\p{ID_Start}]/u;
const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
// General regex used to replace imports with the resolved code, references with resolutions,
// and count the number of newlines in the file for source maps.
const REPLACEMENT_RE =
/\n|import\s+"([0-9a-f]{16}:.+?)";|(?:\$[0-9a-f]{16}\$exports)|(?:\$[0-9a-f]{16}\$(?:import|importAsync|require)\$[0-9a-f]+(?:\$[0-9a-f]+)?)/g;
const BUILTINS = Object.keys(globals.builtin);
const GLOBALS_BY_CONTEXT = {
browser: new Set([...BUILTINS, ...Object.keys(globals.browser)]),
'web-worker': new Set([...BUILTINS, ...Object.keys(globals.worker)]),
'service-worker': new Set([
...BUILTINS,
...Object.keys(globals.serviceworker),
]),
worklet: new Set([...BUILTINS]),
node: new Set([...BUILTINS, ...Object.keys(globals.node)]),
'electron-main': new Set([...BUILTINS, ...Object.keys(globals.node)]),
'electron-renderer': new Set([
...BUILTINS,
...Object.keys(globals.node),
...Object.keys(globals.browser),
]),
};
const OUTPUT_FORMATS = {
esmodule: ESMOutputFormat,
commonjs: CJSOutputFormat,
global: GlobalOutputFormat,
};
export interface OutputFormat {
buildBundlePrelude(): [string, number];
buildBundlePostlude(): [string, number];
}
export class ScopeHoistingPackager {
options: PluginOptions;
bundleGraph: BundleGraph<NamedBundle>;
bundle: NamedBundle;
parcelRequireName: string;
outputFormat: OutputFormat;
isAsyncBundle: boolean;
globalNames: $ReadOnlySet<string>;
assetOutputs: Map<string, {|code: string, map: ?Buffer|}>;
exportedSymbols: Map<
string,
{|
asset: Asset,
exportSymbol: string,
local: string,
exportAs: Array<string>,
|},
> = new Map();
externals: Map<string, Map<string, string>> = new Map();
topLevelNames: Map<string, number> = new Map();
seenAssets: Set<string> = new Set();
wrappedAssets: Set<string> = new Set();
hoistedRequires: Map<string, Map<string, string>> = new Map();
needsPrelude: boolean = false;
usedHelpers: Set<string> = new Set();
constructor(
options: PluginOptions,
bundleGraph: BundleGraph<NamedBundle>,
bundle: NamedBundle,
parcelRequireName: string,
) {
this.options = options;
this.bundleGraph = bundleGraph;
this.bundle = bundle;
this.parcelRequireName = parcelRequireName;
let OutputFormat = OUTPUT_FORMATS[this.bundle.env.outputFormat];
this.outputFormat = new OutputFormat(this);
this.isAsyncBundle =
this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') &&
!this.bundle.env.isIsolated() &&
this.bundle.bundleBehavior !== 'isolated';
this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
}
async package(): Promise<{|contents: string, map: ?SourceMap|}> {
let wrappedAssets = await this.loadAssets();
this.buildExportedSymbols();
// If building a library, the target is actually another bundler rather
// than the final output that could be loaded in a browser. So, loader
// runtimes are excluded, and instead we add imports into the entry bundle
// of each bundle group pointing at the sibling bundles. These can be
// picked up by another bundler later at which point runtimes will be added.
if (
this.bundle.env.isLibrary ||
this.bundle.env.outputFormat === 'commonjs'
) {
let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
for (let b of bundles) {
this.externals.set(relativeBundlePath(this.bundle, b), new Map());
}
}
let res = '';
let lineCount = 0;
let sourceMap = null;
let processAsset = asset => {
let [content, map, lines] = this.visitAsset(asset);
if (sourceMap && map) {
sourceMap.addSourceMap(map, lineCount);
} else if (this.bundle.env.sourceMap) {
sourceMap = map;
}
res += content + '\n';
lineCount += lines + 1;
};
// Hoist wrapped asset to the top of the bundle to ensure that they are registered
// before they are used.
for (let asset of wrappedAssets) {
if (!this.seenAssets.has(asset.id)) {
processAsset(asset);
}
}
// Add each asset that is directly connected to the bundle. Dependencies will be handled
// by replacing `import` statements in the code.
this.bundle.traverseAssets((asset, _, actions) => {
if (this.seenAssets.has(asset.id)) {
actions.skipChildren();
return;
}
processAsset(asset);
actions.skipChildren();
});
let [prelude, preludeLines] = this.buildBundlePrelude();
res = prelude + res;
lineCount += preludeLines;
sourceMap?.offsetLines(1, preludeLines);
let entries = this.bundle.getEntryAssets();
let mainEntry = this.bundle.getMainEntry();
if (this.isAsyncBundle) {
// In async bundles we don't want the main entry to execute until we require it
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
entries = entries.filter(a => a.id !== mainEntry?.id);
mainEntry = null;
}
// If any of the entry assets are wrapped, call parcelRequire so they are executed.
for (let entry of entries) {
if (this.wrappedAssets.has(entry.id) && !this.isScriptEntry(entry)) {
let parcelRequire = `parcelRequire(${JSON.stringify(
this.bundleGraph.getAssetPublicId(entry),
)});\n`;
let entryExports = entry.symbols.get('*')?.local;
if (
entryExports &&
entry === mainEntry &&
this.exportedSymbols.has(entryExports)
) {
res += `\nvar ${entryExports} = ${parcelRequire}`;
} else {
res += `\n${parcelRequire}`;
}
lineCount += 2;
}
}
let [postlude, postludeLines] = this.outputFormat.buildBundlePostlude();
res += postlude;
lineCount += postludeLines;
// The entry asset of a script bundle gets hoisted outside the bundle wrapper so that
// its top-level variables become globals like a real browser script. We need to replace
// all dependency references for runtimes with a parcelRequire call.
if (
this.bundle.env.outputFormat === 'global' &&
this.bundle.env.sourceType === 'script'
) {
res += '\n';
lineCount++;
let mainEntry = nullthrows(this.bundle.getMainEntry());
let {code, map: mapBuffer} = nullthrows(
this.assetOutputs.get(mainEntry.id),
);
let map;
if (mapBuffer) {
map = new SourceMap(this.options.projectRoot, mapBuffer);
}
res += replaceScriptDependencies(
this.bundleGraph,
this.bundle,
code,
map,
this.parcelRequireName,
);
if (sourceMap && map) {
sourceMap.addSourceMap(map, lineCount);
}
}
return {
contents: res,
map: sourceMap,
};
}
async loadAssets(): Promise<Array<Asset>> {
let queue = new PromiseQueue({maxConcurrent: 32});
let wrapped = [];
this.bundle.traverseAssets(asset => {
queue.add(async () => {
let [code, map] = await Promise.all([
asset.getCode(),
this.bundle.env.sourceMap ? asset.getMapBuffer() : null,
]);
return [asset.id, {code, map}];
});
if (
asset.meta.shouldWrap ||
this.isAsyncBundle ||
this.bundle.env.sourceType === 'script' ||
this.bundleGraph.isAssetReferenced(this.bundle, asset) ||
this.bundleGraph
.getIncomingDependencies(asset)
.some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')
) {
this.wrappedAssets.add(asset.id);
wrapped.push(asset);
}
});
for (let wrappedAssetRoot of [...wrapped]) {
this.bundle.traverseAssets((asset, _, actions) => {
if (asset === wrappedAssetRoot) {
return;
}
if (this.wrappedAssets.has(asset.id)) {
actions.skipChildren();
return;
}
this.wrappedAssets.add(asset.id);
wrapped.push(asset);
}, wrappedAssetRoot);
}
this.assetOutputs = new Map(await queue.run());
return wrapped;
}
buildExportedSymbols() {
if (
this.isAsyncBundle ||
!this.bundle.env.isLibrary ||
this.bundle.env.outputFormat !== 'esmodule'
) {
return;
}
// TODO: handle ESM exports of wrapped entry assets...
let entry = this.bundle.getMainEntry();
if (entry && !this.wrappedAssets.has(entry.id)) {
for (let {
asset,
exportAs,
symbol,
exportSymbol,
} of this.bundleGraph.getExportedSymbols(entry)) {
if (typeof symbol === 'string') {
let symbols = this.exportedSymbols.get(
symbol === '*' ? nullthrows(entry.symbols.get('*')?.local) : symbol,
)?.exportAs;
if (!symbols) {
symbols = [];
this.exportedSymbols.set(symbol, {
asset,
exportSymbol,
local: symbol,
exportAs: symbols,
});
}
if (exportAs === '*') {
exportAs = 'default';
}
symbols.push(exportAs);
} else if (symbol === null) {
// TODO `meta.exportsIdentifier[exportSymbol]` should be exported
// let relativePath = relative(options.projectRoot, asset.filePath);
// throw getThrowableDiagnosticForNode(
// md`${relativePath} couldn't be statically analyzed when importing '${exportSymbol}'`,
// entry.filePath,
// loc,
// );
} else if (symbol !== false) {
// let relativePath = relative(options.projectRoot, asset.filePath);
// throw getThrowableDiagnosticForNode(
// md`${relativePath} does not export '${exportSymbol}'`,
// entry.filePath,
// loc,
// );
}
}
}
}
getTopLevelName(name: string): string {
name = name.replace(NON_ID_CONTINUE_RE, '');
if (!ID_START_RE.test(name) || this.globalNames.has(name)) {
name = '_' + name;
}
let count = this.topLevelNames.get(name);
if (count == null) {
this.topLevelNames.set(name, 1);
return name;
}
this.topLevelNames.set(name, count + 1);
return name + count;
}
getPropertyAccess(obj: string, property: string): string {
if (IDENTIFIER_RE.test(property)) {
return `${obj}.${property}`;
}
return `${obj}[${JSON.stringify(property)}]`;
}
visitAsset(asset: Asset): [string, ?SourceMap, number] {
invariant(!this.seenAssets.has(asset.id), 'Already visited asset');
this.seenAssets.add(asset.id);
let {code, map} = nullthrows(this.assetOutputs.get(asset.id));
return this.buildAsset(asset, code, map);
}
buildAsset(
asset: Asset,
code: string,
map: ?Buffer,
): [string, ?SourceMap, number] {
let shouldWrap = this.wrappedAssets.has(asset.id);
let deps = this.bundleGraph.getDependencies(asset);
let sourceMap =
this.bundle.env.sourceMap && map
? new SourceMap(this.options.projectRoot, map)
: null;
// If this asset is skipped, just add dependencies and not the asset's content.
if (this.shouldSkipAsset(asset)) {
let depCode = '';
let lineCount = 0;
for (let dep of deps) {
let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
let skipped = this.bundleGraph.isDependencySkipped(dep);
if (skipped) {
continue;
}
if (!resolved) {
if (!dep.isOptional) {
this.addExternal(dep);
}
continue;
}
if (
this.bundle.hasAsset(resolved) &&
!this.seenAssets.has(resolved.id)
) {
let [code, map, lines] = this.visitAsset(resolved);
depCode += code + '\n';
if (sourceMap && map) {
sourceMap.addSourceMap(map, lineCount);
}
lineCount += lines + 1;
}
}
return [depCode, sourceMap, lineCount];
}
// TODO: maybe a meta prop?
if (code.includes('$parcel$global')) {
this.usedHelpers.add('$parcel$global');
}
if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
const relPath = normalizeSeparators(
path.relative(this.bundle.target.distDir, path.dirname(asset.filePath)),
);
code = code.replace('$parcel$dirnameReplace', relPath);
code = code.replace('$parcel$filenameReplace', relPath);
}
let [depMap, replacements] = this.buildReplacements(asset, deps);
let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
if (prependLines > 0) {
sourceMap?.offsetLines(1, prependLines);
code = prepend + code;
}
code += append;
let lineCount = 0;
let depContent = [];
if (depMap.size === 0 && replacements.size === 0) {
// If there are no dependencies or replacements, use a simple function to count the number of lines.
lineCount = countLines(code) - 1;
} else {
// Otherwise, use a regular expression to perform replacements.
// We need to track how many newlines there are for source maps, replace
// all import statements with dependency code, and perform inline replacements
// of all imported symbols with their resolved export symbols. This is all done
// in a single regex so that we only do one pass over the whole code.
let offset = 0;
let columnStartIndex = 0;
code = code.replace(REPLACEMENT_RE, (m, d, i) => {
if (m === '\n') {
columnStartIndex = i + offset + 1;
lineCount++;
return '\n';
}
// If we matched an import, replace with the source code for the dependency.
if (d != null) {
let deps = depMap.get(d);
if (!deps) {
return m;
}
let replacement = '';
// A single `${id}:${specifier}:esm` might have been resolved to multiple assets due to
// reexports.
for (let dep of deps) {
let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
let skipped = this.bundleGraph.isDependencySkipped(dep);
if (resolved && !skipped) {
// Hoist variable declarations for the referenced parcelRequire dependencies
// after the dependency is declared. This handles the case where the resulting asset
// is wrapped, but the dependency in this asset is not marked as wrapped. This means
// that it was imported/required at the top-level, so its side effects should run immediately.
let [res, lines] = this.getHoistedParcelRequires(
asset,
dep,
resolved,
);
let map;
if (
this.bundle.hasAsset(resolved) &&
!this.seenAssets.has(resolved.id)
) {
// If this asset is wrapped, we need to hoist the code for the dependency
// outside our parcelRequire.register wrapper. This is safe because all
// assets referenced by this asset will also be wrapped. Otherwise, inline the
// asset content where the import statement was.
if (shouldWrap) {
depContent.push(this.visitAsset(resolved));
} else {
let [depCode, depMap, depLines] = this.visitAsset(resolved);
res = depCode + '\n' + res;
lines += 1 + depLines;
map = depMap;
}
}
// Push this asset's source mappings down by the number of lines in the dependency
// plus the number of hoisted parcelRequires. Then insert the source map for the dependency.
if (sourceMap) {
if (lines > 0) {
sourceMap.offsetLines(lineCount + 1, lines);
}
if (map) {
sourceMap.addSourceMap(map, lineCount);
}
}
replacement += res;
lineCount += lines;
}
}
return replacement;
}
// If it wasn't a dependency, then it was an inline replacement (e.g. $id$import$foo -> $id$export$foo).
let replacement = replacements.get(m) ?? m;
if (sourceMap) {
// Offset the source map columns for this line if the replacement was a different length.
// This assumes that the match and replacement both do not contain any newlines.
let lengthDifference = replacement.length - m.length;
if (lengthDifference !== 0) {
sourceMap.offsetColumns(
lineCount + 1,
i + offset - columnStartIndex + m.length,
lengthDifference,
);
offset += lengthDifference;
}
}
return replacement;
});
}
// If the asset is wrapped, we need to insert the dependency code outside the parcelRequire.register
// wrapper. Dependencies must be inserted AFTER the asset is registered so that circular dependencies work.
if (shouldWrap) {
// Offset by one line for the parcelRequire.register wrapper.
sourceMap?.offsetLines(1, 1);
lineCount++;
code = `parcelRequire.register(${JSON.stringify(
this.bundleGraph.getAssetPublicId(asset),
)}, function(module, exports) {
${code}
});
`;
lineCount += 2;
for (let [depCode, map, lines] of depContent) {
if (!depCode) continue;
code += depCode + '\n';
if (sourceMap && map) {
sourceMap.addSourceMap(map, lineCount);
}
lineCount += lines + 1;
}
this.needsPrelude = true;
}
return [code, sourceMap, lineCount];
}
buildReplacements(
asset: Asset,
deps: Array<Dependency>,
): [Map<string, Array<Dependency>>, Map<string, string>] {
let assetId = asset.meta.id;
invariant(typeof assetId === 'string');
// Build two maps: one of import specifiers, and one of imported symbols to replace.
// These will be used to build a regex below.
let depMap = new DefaultMap<string, Array<Dependency>>(() => []);
let replacements = new Map();
for (let dep of deps) {
let specifierType =
dep.specifierType === 'esm' ? `:${dep.specifierType}` : '';
depMap
.get(
`${assetId}:${getSpecifier(dep)}${
!dep.meta.placeholder ? specifierType : ''
}`,
)
.push(dep);
let asyncResolution = this.bundleGraph.resolveAsyncDependency(
dep,
this.bundle,
);
let resolved =
asyncResolution?.type === 'asset'
? // Prefer the underlying asset over a runtime to load it. It will
// be wrapped in Promise.resolve() later.
asyncResolution.value
: this.bundleGraph.getResolvedAsset(dep, this.bundle);
if (
!resolved &&
!dep.isOptional &&
!this.bundleGraph.isDependencySkipped(dep)
) {
this.addExternal(dep, replacements);
}
if (!resolved) {
continue;
}
for (let [imported, {local}] of dep.symbols) {
if (local === '*') {
continue;
}
let symbol = this.getSymbolResolution(asset, resolved, imported, dep);
replacements.set(
local,
// If this was an internalized async asset, wrap in a Promise.resolve.
asyncResolution?.type === 'asset'
? `Promise.resolve(${symbol})`
: symbol,
);
}
// Async dependencies need a namespace object even if all used symbols were statically analyzed.
// This is recorded in the promiseSymbol meta property set by the transformer rather than in
// symbols so that we don't mark all symbols as used.
if (dep.priority === 'lazy' && dep.meta.promiseSymbol) {
let promiseSymbol = dep.meta.promiseSymbol;
invariant(typeof promiseSymbol === 'string');
let symbol = this.getSymbolResolution(asset, resolved, '*', dep);
replacements.set(
promiseSymbol,
asyncResolution?.type === 'asset'
? `Promise.resolve(${symbol})`
: symbol,
);
}
}
// If this asset is wrapped, we need to replace the exports namespace with `module.exports`,
// which will be provided to us by the wrapper.
if (
this.wrappedAssets.has(asset.id) ||
(this.bundle.env.outputFormat === 'commonjs' &&
asset === this.bundle.getMainEntry())
) {
let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;
replacements.set(exportsName, 'module.exports');
}
return [depMap, replacements];
}
addExternal(dep: Dependency, replacements?: Map<string, string>) {
if (this.bundle.env.outputFormat === 'global') {
throw new ThrowableDiagnostic({
diagnostic: {
message:
'External modules are not supported when building for browser',
codeFrames: [
{
filePath: nullthrows(dep.sourcePath),
codeHighlights: dep.loc
? [
{
start: dep.loc.start,
end: dep.loc.end,
},
]
: [],
},
],
},
});
}
// Map of DependencySpecifier -> Map<ExportedSymbol, Identifier>>
let external = this.externals.get(dep.specifier);
if (!external) {
external = new Map();
this.externals.set(dep.specifier, external);
}
for (let [imported, {local}] of dep.symbols) {
// If already imported, just add the already renamed variable to the mapping.
let renamed = external.get(imported);
if (renamed && local !== '*' && replacements) {
replacements.set(local, renamed);
continue;
}
// For CJS output, always use a property lookup so that exports remain live.
// For ESM output, use named imports which are always live.
if (this.bundle.env.outputFormat === 'commonjs') {
renamed = external.get('*');
if (!renamed) {
renamed = this.getTopLevelName(
`$${this.bundle.publicId}$${dep.specifier}`,
);
external.set('*', renamed);
}
if (local !== '*' && replacements) {
let replacement;
if (imported === '*') {
replacement = renamed;
} else if (imported === 'default') {
replacement = `($parcel$interopDefault(${renamed}))`;
this.usedHelpers.add('$parcel$interopDefault');
} else {
replacement = this.getPropertyAccess(renamed, imported);
}
replacements.set(local, replacement);
}
} else {
// Rename the specifier so that multiple local imports of the same imported specifier
// are deduplicated. We have to prefix the imported name with the bundle id so that
// local variables do not shadow it.
if (this.exportedSymbols.has(local)) {
renamed = local;
} else if (imported === 'default' || imported === '*') {
renamed = this.getTopLevelName(
`$${this.bundle.publicId}$${dep.specifier}`,
);
} else {
renamed = this.getTopLevelName(
`$${this.bundle.publicId}$${imported}`,
);
}
external.set(imported, renamed);
if (local !== '*' && replacements) {
replacements.set(local, renamed);
}
}
}
}
getSymbolResolution(
parentAsset: Asset,
resolved: Asset,
imported: string,
dep?: Dependency,
): string {
let {
asset: resolvedAsset,
exportSymbol,
symbol,
} = this.bundleGraph.getSymbolResolution(resolved, imported, this.bundle);
if (
resolvedAsset.type !== 'js' ||
(dep && this.bundleGraph.isDependencySkipped(dep))
) {
// Graceful fallback for non-js imports or when trying to resolve a symbol
// that is actually unused but we still need a placeholder value.
return '{}';
}
let isWrapped =
!this.bundle.hasAsset(resolvedAsset) ||
(this.wrappedAssets.has(resolvedAsset.id) &&
resolvedAsset !== parentAsset);
let staticExports = resolvedAsset.meta.staticExports !== false;
let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset);
// If the resolved asset is wrapped, but imported at the top-level by this asset,
// then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
if (
isWrapped &&
dep &&
!dep?.meta.shouldWrap &&
symbol !== false &&
// Only do this if the asset is part of a different bundle (so it was definitely
// parcelRequire.register'ed there), or if it is indeed registered in this bundle.
(!this.bundle.hasAsset(resolvedAsset) ||
!this.shouldSkipAsset(resolvedAsset))
) {
let hoisted = this.hoistedRequires.get(dep.id);
if (!hoisted) {
hoisted = new Map();
this.hoistedRequires.set(dep.id, hoisted);
}
hoisted.set(
resolvedAsset.id,
`var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`,
);
}
if (isWrapped) {
this.needsPrelude = true;
}
// If this is an ESM default import of a CJS module with a `default` symbol,
// and no __esModule flag, we need to resolve to the namespace instead.
let isDefaultInterop =
exportSymbol === 'default' &&
staticExports &&
!isWrapped &&
(dep?.meta.kind === 'Import' || dep?.meta.kind === 'Export') &&
resolvedAsset.symbols.hasExportSymbol('*') &&
resolvedAsset.symbols.hasExportSymbol('default') &&
!resolvedAsset.symbols.hasExportSymbol('__esModule');
// Find the namespace object for the resolved module. If wrapped and this
// is an inline require (not top-level), use a parcelRequire call, otherwise
// the hoisted variable declared above. Otherwise, if not wrapped, use the
// namespace export symbol.
let assetId = resolvedAsset.meta.id;
invariant(typeof assetId === 'string');
let obj =
isWrapped && (!dep || dep?.meta.shouldWrap)
? // Wrap in extra parenthesis to not change semantics, e.g.`new (parcelRequire("..."))()`.
`(parcelRequire(${JSON.stringify(publicId)}))`
: isWrapped && dep
? `$${publicId}`
: resolvedAsset.symbols.get('*')?.local || `$${assetId}$exports`;
if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
// Resolve to the namespace object if requested or this is a CJS default interop reqiure.
if (
parentAsset === resolvedAsset &&
this.wrappedAssets.has(resolvedAsset.id)
) {
// Directly use module.exports for wrapped assets importing themselves.
return 'module.exports';
} else {
return obj;
}
} else if (
(!staticExports || isWrapped || !symbol) &&
resolvedAsset !== parentAsset
) {
// If the resolved asset is wrapped or has non-static exports,
// we need to use a member access off the namespace object rather
// than a direct reference. If importing default from a CJS module,
// use a helper to check the __esModule flag at runtime.
let kind = dep?.meta.kind;
if (
(!dep || kind === 'Import' || kind === 'Export') &&
exportSymbol === 'default' &&
resolvedAsset.symbols.hasExportSymbol('*') &&
this.needsDefaultInterop(resolvedAsset)
) {
this.usedHelpers.add('$parcel$interopDefault');
return `(/*@__PURE__*/$parcel$interopDefault(${obj}))`;
} else {
return this.getPropertyAccess(obj, exportSymbol);
}
} else if (!symbol) {
invariant(false, 'Asset was skipped or not found.');
} else {
return symbol;
}
}
getHoistedParcelRequires(
parentAsset: Asset,
dep: Dependency,
resolved: Asset,
): [string, number] {
if (resolved.type !== 'js') {
return ['', 0];
}
let hoisted = this.hoistedRequires.get(dep.id);
let res = '';
let lineCount = 0;
let isWrapped =
!this.bundle.hasAsset(resolved) ||
(this.wrappedAssets.has(resolved.id) && resolved !== parentAsset);
// If the resolved asset is wrapped and is imported in the top-level by this asset,
// we need to run side effects when this asset runs. If the resolved asset is not
// the first one in the hoisted requires, we need to insert a parcelRequire here
// so it runs first.
if (
isWrapped &&
!dep.meta.shouldWrap &&
(!hoisted || hoisted.keys().next().value !== resolved.id) &&
!this.bundleGraph.isDependencySkipped(dep) &&
!this.shouldSkipAsset(resolved)
) {
this.needsPrelude = true;
res += `parcelRequire(${JSON.stringify(
this.bundleGraph.getAssetPublicId(resolved),
)});`;
}
if (hoisted) {
this.needsPrelude = true;
res += '\n' + [...hoisted.values()].join('\n');
lineCount += hoisted.size;
}
return [res, lineCount];
}
buildAssetPrelude(
asset: Asset,
deps: Array<Dependency>,
): [string, number, string] {
let prepend = '';
let prependLineCount = 0;
let append = '';
let shouldWrap = this.wrappedAssets.has(asset.id);
let usedSymbols = nullthrows(this.bundleGraph.getUsedSymbols(asset));
let assetId = asset.meta.id;
invariant(typeof assetId === 'string');
// If the asset has a namespace export symbol, it is CommonJS.
// If there's no __esModule flag, and default is a used symbol, we need
// to insert an interop helper.
let defaultInterop =
asset.symbols.hasExportSymbol('*') &&
usedSymbols.has('default') &&
!asset.symbols.hasExportSymbol('__esModule');
let usedNamespace =
// If the asset has * in its used symbols, we might need the exports namespace.
// The one case where this isn't true is in ESM library entries, where the only
// dependency on * is the entry dependency. In this case, we will use ESM exports
// instead of the namespace object.
(usedSymbols.has('*') &&
(this.bundle.env.outputFormat !== 'esmodule' ||
!this.bundle.env.isLibrary ||
asset !== this.bundle.getMainEntry() ||
this.bundleGraph
.getIncomingDependencies(asset)
.some(
dep =>
!dep.isEntry &&
nullthrows(this.bundleGraph.getUsedSymbols(dep)).has('*'),
))) ||
// If a symbol is imported (used) from a CJS asset but isn't listed in the symbols,
// we fallback on the namespace object.
(asset.symbols.hasExportSymbol('*') &&
[...usedSymbols].some(s => !asset.symbols.hasExportSymbol(s))) ||
// If the exports has this asset's namespace (e.g. ESM output from CJS input),
// include the namespace object for the default export.
this.exportedSymbols.has(`$${assetId}$exports`);
// If the asset doesn't have static exports, should wrap, the namespace is used,
// or we need default interop, then we need to synthesize a namespace object for
// this asset.
if (
asset.meta.staticExports === false ||
shouldWrap ||
usedNamespace ||
defaultInterop
) {
// Insert a declaration for the exports namespace object. If the asset is wrapped
// we don't need to do this, because we'll use the `module.exports` object provided
// by the wrapper instead. This is also true of CommonJS entry assets, which will use
// the `module.exports` object provided by CJS.
if (
!shouldWrap &&
(this.bundle.env.outputFormat !== 'commonjs' ||
asset !== this.bundle.getMainEntry())
) {
prepend += `var $${assetId}$exports = {};\n`;
prependLineCount++;
}
// Insert the __esModule interop flag for this module if it has a `default` export
// and the namespace symbol is used.
// TODO: only if required by CJS?
if (asset.symbols.hasExportSymbol('default') && usedSymbols.has('*')) {
prepend += `\n$parcel$defineInteropFlag($${assetId}$exports);\n`;
prependLineCount += 2;
this.usedHelpers.add('$parcel$defineInteropFlag');
}
// Find the used exports of this module. This is based on the used symbols of
// incoming dependencies rather than the asset's own used exports so that we include