-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
link.js
1091 lines (973 loc) Β· 32.8 KB
/
link.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,
NamedBundle,
PluginOptions,
Symbol,
SourceLocation,
} from '@parcel/types';
import type {ExternalModule, ExternalBundle} from './types';
import type {
ArrayExpression,
Expression,
ExpressionStatement,
File,
Node,
FunctionDeclaration,
Identifier,
Statement,
StringLiteral,
VariableDeclaration,
} from '@babel/types';
import nullthrows from 'nullthrows';
import path from 'path';
import fs from 'fs';
import invariant from 'assert';
import {relative} from 'path';
import template from '@babel/template';
import * as t from '@babel/types';
import {md} from '@parcel/diagnostic';
import {
isAssignmentExpression,
isExpressionStatement,
isIdentifier,
isMemberExpression,
isSequenceExpression,
isStringLiteral,
} from '@babel/types';
import {traverse2, REMOVE, Scope} from '@parcel/babylon-walk';
import {convertBabelLoc} from '@parcel/babel-ast-utils';
import globals from 'globals';
import {
assertString,
getName,
getHelpers,
getIdentifier,
getThrowableDiagnosticForNode,
isEntry,
isReferenced,
needsPrelude,
needsDefaultInterop,
parse,
} from './utils';
import OutputFormats from './formats/index.js';
const THROW_TEMPLATE = template.statement<
{|MODULE: StringLiteral|},
ExpressionStatement,
>('$parcel$missingModule(MODULE);');
const REQUIRE_RESOLVE_CALL_TEMPLATE = template.expression<
{|ID: StringLiteral|},
Expression,
>('require.resolve(ID)');
const FAKE_INIT_TEMPLATE = template.statement<
{|INIT: Identifier, EXPORTS: Identifier|},
FunctionDeclaration,
>(`function INIT(){
return EXPORTS;
}`);
const PARCEL_REQUIRE_TEMPLATE = template.statement<
{|PARCEL_REQUIRE_NAME: Identifier|},
VariableDeclaration,
>(`var parcelRequire = $parcel$global.PARCEL_REQUIRE_NAME`);
const PARCEL_REQUIRE_NAME_TEMPLATE = template.statement<
{|PARCEL_REQUIRE_NAME: StringLiteral|},
VariableDeclaration,
>(`var parcelRequireName = PARCEL_REQUIRE_NAME;`);
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),
]),
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 PRELUDE_PATH = path.join(__dirname, 'prelude.js');
const PRELUDE = parse(
fs.readFileSync(path.join(__dirname, 'prelude.js'), 'utf8'),
PRELUDE_PATH,
);
const REGISTER_TEMPLATE = template.statements<
{|
REFERENCED_IDS: ArrayExpression,
STATEMENTS: Array<Statement>,
PARCEL_REQUIRE: Identifier,
|},
Array<Statement>,
>(`function $parcel$bundleWrapper() {
if ($parcel$bundleWrapper._executed) return;
$parcel$bundleWrapper._executed = true;
STATEMENTS;
}
var $parcel$referencedAssets = REFERENCED_IDS;
for (var $parcel$i = 0; $parcel$i < $parcel$referencedAssets.length; $parcel$i++) {
PARCEL_REQUIRE.registerBundle($parcel$referencedAssets[$parcel$i], $parcel$bundleWrapper);
}
`);
const WRAPPER_TEMPLATE = template.statement<
{|STATEMENTS: Array<Statement>|},
Statement,
>('(function () { STATEMENTS; })()');
export function link({
bundle,
bundleGraph,
ast,
options,
wrappedAssets,
parcelRequireName,
}: {|
bundle: NamedBundle,
bundleGraph: BundleGraph<NamedBundle>,
ast: File,
options: PluginOptions,
wrappedAssets: Set<string>,
parcelRequireName: string,
|}): File {
let format = OutputFormats[bundle.env.outputFormat];
let replacements: Map<Symbol, Symbol> = new Map();
let imports: Map<Symbol, null | [Asset, Symbol, ?SourceLocation]> = new Map();
let exports: Map<Symbol, [Asset, Symbol]> = new Map();
let assets: Map<string, Asset> = new Map();
let exportsMap: Map<Symbol, Asset> = new Map();
let scope = new Scope('program');
let globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
let helpers = getHelpers();
let importedFiles = new Map<string, ExternalModule | ExternalBundle>();
let referencedAssets = new Set();
let reexports = new Set();
// 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 (bundle.env.isLibrary) {
let bundles = bundleGraph.getReferencedBundles(bundle);
for (let b of bundles) {
importedFiles.set(b.id, {
bundle: b,
assets: new Set(),
});
}
}
// Build a mapping of all imported identifiers to replace.
bundle.traverseAssets(asset => {
assets.set(assertString(asset.meta.id), asset);
exportsMap.set(assertString(asset.meta.exportsIdentifier), asset);
for (let dep of bundleGraph.getDependencies(asset)) {
let resolved = bundleGraph.getDependencyResolution(dep, bundle);
let skipped = bundleGraph.isDependencySkipped(dep);
// If the dependency was skipped, the `...$import$..` identifier needs to be removed.
// If the dependency was excluded, it will be replaced by the output format at the very end.
if (resolved || skipped) {
for (let [imported, {local, loc}] of dep.symbols) {
imports.set(
local,
resolved && !skipped ? [resolved, imported, loc] : null,
);
}
}
}
for (let [symbol, {local}] of asset.symbols) {
exports.set(local, [asset, symbol]);
}
if (bundleGraph.isAssetReferencedByDependant(bundle, asset)) {
referencedAssets.add(asset);
}
});
let entry = bundle.getMainEntry();
let exportedSymbols: Map<
string,
Array<{|exportAs: string, local: string|}>,
> = new Map();
if (entry) {
if (entry.meta.isCommonJS) {
if (bundle.env.outputFormat === 'commonjs') {
exportedSymbols.set(assertString(entry.meta.exportsIdentifier), [
{exportAs: '*', local: 'exports'},
]);
}
} else {
for (let {
exportAs,
exportSymbol,
symbol,
asset,
loc,
} of bundleGraph.getExportedSymbols(entry)) {
if (typeof symbol === 'string') {
let symbols = exportedSymbols.get(
symbol === '*'
? assertString(entry.meta.exportsIdentifier)
: symbol,
);
let local = exportAs;
if (symbols) {
local = symbols[0].local;
} else {
symbols = [];
exportedSymbols.set(symbol, symbols);
if (local === '*') {
local = 'exports';
} else if (!t.isValidIdentifier(local) || globalNames.has(local)) {
local = scope.generateUid(local);
} else {
scope.add(local);
}
}
symbols.push({exportAs, local});
} 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,
);
}
}
}
}
let resolveSymbolCache = new Map();
function resolveSymbol(inputAsset, inputSymbol: Symbol, bundle) {
let k = inputAsset.id + ':' + inputSymbol + ':' + bundle.id;
let cached = resolveSymbolCache.get(k);
if (cached) {
return cached;
}
let {asset, exportSymbol, symbol, loc} = bundleGraph.resolveSymbol(
inputAsset,
inputSymbol,
bundle,
);
if (asset.meta.staticExports === false) {
let res = {
asset: asset,
symbol: exportSymbol,
identifier: null,
loc,
};
resolveSymbolCache.set(k, res);
return res;
}
let identifier = symbol;
if (identifier && imports.get(identifier) === null) {
// a deferred import
let res = {
asset: asset,
symbol: exportSymbol,
identifier: null,
loc,
};
resolveSymbolCache.set(k, res);
return res;
}
// If this is a wildcard import, resolve to the exports object.
if (asset && exportSymbol === '*') {
identifier = assertString(asset.meta.exportsIdentifier);
}
if (identifier && replacements.has(identifier)) {
identifier = replacements.get(identifier);
}
let res = {asset: asset, symbol: exportSymbol, identifier, loc};
resolveSymbolCache.set(k, res);
return res;
}
let needsExportsIdentifierCache = new Map();
let bundleNeedsMainExportsIdentifier =
(bundle.env.outputFormat === 'global' &&
(!isEntry(bundle, bundleGraph) || isReferenced(bundle, bundleGraph))) ||
(bundle.env.outputFormat === 'esmodule' && entry?.meta.isCommonJS);
function needsExportsIdentifier(name: string) {
let asset = exportsMap.get(name);
if (asset) {
return needsExportsIdentifierForAsset(asset);
}
return true;
}
function needsExportsIdentifierForAsset(asset: Asset) {
if (needsExportsIdentifierCache.has(asset)) {
return needsExportsIdentifierCache.get(asset);
}
if (
asset.meta.staticExports === false ||
wrappedAssets.has(asset.id) ||
referencedAssets.has(asset)
) {
needsExportsIdentifierCache.set(asset, true);
return true;
}
let isEntry = asset === bundle.getMainEntry();
if (isEntry && bundleNeedsMainExportsIdentifier) {
needsExportsIdentifierCache.set(asset, true);
return true;
}
let deps = bundleGraph.getIncomingDependencies(asset);
let usedSymbols = bundleGraph.getUsedSymbols(asset);
if (usedSymbols.has('*') && (!isEntry || asset.meta.isCommonJS)) {
needsExportsIdentifierCache.set(asset, true);
return true;
}
let res = deps.some(
dep =>
// Internalized async dependencies need the exports object for Promise.resolve($id$exports)
(dep.isAsync && bundle.hasDependency(dep)) ||
// If there's a dependency on the namespace, and the parent asset's exports object is used,
// we need to keep the exports object for $parcel$exportWildcard.
(!isEntry &&
dep.symbols.hasExportSymbol('*') &&
needsExportsIdentifierForAsset(
nullthrows(bundleGraph.getAssetWithDependency(dep)),
)) ||
// If the asset is CommonJS and there's an ES6 dependency on `default`, we need the
// exports identifier to call $parcel$interopDefault.
(asset.meta.isCommonJS &&
dep.meta.isES6Module &&
dep.symbols.hasExportSymbol('default')) ||
// If the asset is an ES6 module with a default export, and there's a CommonJS dependency
// on it, we need the exports identifier to call $parcel$defineInteropFlag.
(asset.meta.isES6Module &&
asset.symbols.hasExportSymbol('default') &&
dep.meta.isCommonJS &&
!dep.isAsync &&
dep.symbols.hasExportSymbol('*')) ||
// If one of the symbols imported by the dependency doesn't resolve, then we need the
// exports identifier to fall back to.
[...dep.symbols].some(
([symbol]) => !resolveSymbol(asset, symbol, bundle).identifier,
),
);
needsExportsIdentifierCache.set(asset, res);
return res;
}
function needsDeclaration(name: string) {
let exp = exports.get(name);
if (exp) {
let [asset, local] = exp;
if (asset === bundle.getMainEntry() && bundle.env.isLibrary) {
return true;
}
if (asset.meta.staticExports === false) {
return true;
}
let usedSymbols = bundleGraph.getUsedSymbols(asset);
// If the asset is CommonJS, and "default" was used but not defined, this
// will resolve to the $id$exports object, so we need to retain all symbols.
if (
asset.meta.isCommonJS &&
usedSymbols.has('default') &&
!asset.symbols.hasExportSymbol('default')
) {
return true;
}
// Otherwise, if the symbol is pure and unused, it is safe to remove.
if (asset.symbols.get(local)?.meta?.isPure) {
return usedSymbols.has(local) || usedSymbols.has('*');
}
}
return true;
}
function maybeReplaceIdentifier(node: Identifier) {
let {name} = node;
if (typeof name !== 'string') {
return;
}
let replacement = replacements.get(name);
if (replacement) {
node.name = replacement;
}
if (imports.has(name)) {
let res: ?Node;
let imported = imports.get(name);
if (imported == null) {
// import was deferred
res = t.objectExpression([]);
} else {
let [asset, symbol] = imported;
res = replaceImportNode(asset, symbol, node);
// If the export does not exist, replace with an empty object.
if (!res) {
res = t.objectExpression([]);
}
}
return res;
}
}
// node is an Identifier like $id$import$foo that directly imports originalName from originalModule
function replaceImportNode(originalModule, originalName, node) {
let {asset: mod, symbol, identifier} = resolveSymbol(
originalModule,
originalName,
bundle,
);
// If the symbol resolves to the original module where the export is defined,
// do not perform any replacements.
let exp = exports.get(node.name);
if (exp && exp[0] === mod) {
return node;
}
let res = identifier != null ? findSymbol(node, identifier) : identifier;
if (mod.meta.staticExports === false || wrappedAssets.has(mod.id)) {
res = null;
}
// If the module is not in this bundle, create a `require` call for it.
if (!mod.meta.id || !assets.has(assertString(mod.meta.id))) {
if (res === false) {
// Asset was skipped
return null;
}
res = addBundleImport(mod, node);
return res ? interop(mod, symbol, node, res) : null;
}
// The ESM 'does not export' case was already handled by core's symbol proapgation.
// Look for an exports object if we bailed out.
// TODO remove the first part of the condition once bundleGraph.resolveSymbol().identifier === null covers this
if ((res === undefined && mod.meta.isCommonJS) || res === null) {
if (wrappedAssets.has(mod.id)) {
res = t.callExpression(getIdentifier(mod, 'init'), []);
} else {
res = findSymbol(node, assertString(mod.meta.exportsIdentifier));
if (!node) {
return null;
}
}
res = interop(mod, symbol, res, res);
return res;
}
return res;
}
function findSymbol(node, symbol) {
if (symbol && replacements.has(symbol)) {
symbol = replacements.get(symbol);
}
let exp = symbol && exportedSymbols.get(symbol);
if (exp) {
symbol = exp[0].local;
}
// if the symbol exists there is no need to remap it
if (symbol) {
return t.identifier(symbol);
}
return null;
}
function interop(mod, originalName, originalNode, node) {
// Handle interop for default imports of CommonJS modules.
if (
mod.meta.isCommonJS &&
originalName === 'default' &&
needsDefaultInterop(bundleGraph, bundle, mod)
) {
let name = getName(mod, '$interop$default');
return t.identifier(name);
}
// if there is a CommonJS export return $id$exports.name
if (originalName !== '*' && node != null) {
if (t.isValidIdentifier(originalName, false)) {
return t.memberExpression(node, t.identifier(originalName));
} else {
return t.memberExpression(node, t.stringLiteral(originalName), true);
}
}
return node;
}
function addExternalModule(node, ancestors, dep) {
// Find an existing import for this specifier, or create a new one.
let importedFile = importedFiles.get(dep.moduleSpecifier);
if (!importedFile) {
importedFile = {
source: dep.moduleSpecifier,
specifiers: new Map(),
isCommonJS: !!dep.meta?.isCommonJS,
loc: convertBabelLoc(node.loc),
};
importedFiles.set(dep.moduleSpecifier, importedFile);
}
invariant(importedFile.specifiers != null);
let specifiers = importedFile.specifiers;
// For each of the imported symbols, add to the list of imported specifiers.
for (let [imported, {local}] of dep.symbols) {
// If already imported, just add the already renamed variable to the mapping.
let renamed = specifiers.get(imported);
if (renamed) {
replacements.set(local, renamed);
continue;
}
renamed = replacements.get(local);
// If this symbol is re-exported, add it to the reexport list.
let exp = exportedSymbols.get(local);
if (exp) {
renamed = exp[0].local;
for (let e of exp) {
reexports.add(e);
}
}
if (!renamed) {
// Rename the specifier to something nicer. Try to use the imported
// name, except for default and namespace imports, and if the name is
// already in scope.
renamed = imported;
if (imported === 'default' || imported === '*') {
renamed = scope.generateUid(dep.moduleSpecifier);
} else if (scope.has(imported)) {
renamed = scope.generateUid(imported);
} else {
scope.add(imported);
}
replacements.set(local, renamed);
}
specifiers.set(imported, renamed);
}
return specifiers.get('*');
}
function addBundleImport(mod, node) {
// Find a bundle that's reachable from the current bundle (sibling or ancestor)
// containing this asset, and create an import for it if needed.
let importedBundle = bundleGraph.findReachableBundleWithAsset(bundle, mod);
if (!importedBundle) {
throw new Error(
`No reachable bundle found containing ${relative(
options.inputFS.cwd(),
mod.filePath,
)}`,
);
}
let imported = importedFiles.get(importedBundle.id);
if (!imported) {
imported = {
bundle: importedBundle,
assets: new Set(),
loc: convertBabelLoc(node.loc),
};
importedFiles.set(importedBundle.id, imported);
}
invariant(imported.assets != null);
imported.assets.add(mod);
let initIdentifier = getIdentifier(mod, 'init');
return t.callExpression(initIdentifier, []);
}
traverse2(ast, {
CallExpression(node, state, ancestors) {
let {arguments: args, callee} = node;
if (!isIdentifier(callee)) {
return;
}
// each require('module') call gets replaced with $parcel$require(id, 'module')
if (callee.name === '$parcel$require') {
let [id, source] = args;
if (
args.length < 2 ||
!isStringLiteral(id) ||
!isStringLiteral(source)
) {
throw new Error(
'invariant: invalid signature, expected : $parcel$require(number, string)',
);
}
let asset = nullthrows(assets.get(id.value));
let dep = nullthrows(
bundleGraph
.getDependencies(asset)
.find(dep => dep.moduleSpecifier === source.value),
);
let asyncResolution = bundleGraph.resolveAsyncDependency(dep, bundle);
let mod =
asyncResolution?.type === 'asset'
? // Prefer the underlying asset over a runtime to load it. It will
// be wrapped in Promise.resolve() later.
asyncResolution.value
: bundleGraph.getDependencyResolution(dep, bundle);
let newNode;
if (!bundleGraph.isDependencySkipped(dep)) {
if (!mod) {
if (dep.isOptional) {
newNode = THROW_TEMPLATE({MODULE: t.stringLiteral(source.value)});
scope.add('$parcel$missingModule');
} else {
let name = addExternalModule(node, ancestors, dep);
if (!isUnusedValue(ancestors) && name) {
newNode = t.identifier(name);
}
}
} else {
// If there is a third arg, it is an identifier to replace the require with.
// This happens when `require('foo').bar` is detected in the hoister.
if (args.length > 2 && isIdentifier(args[2])) {
newNode = maybeReplaceIdentifier(args[2]);
} else {
if (mod.meta.id && assets.has(assertString(mod.meta.id))) {
let isValueUsed = !isUnusedValue(ancestors);
// We need to wrap the module in a function when a require
// call happens inside a non top-level scope, e.g. in a
// function, if statement, or conditional expression.
if (wrappedAssets.has(mod.id)) {
newNode = t.callExpression(getIdentifier(mod, 'init'), []);
}
// Replace with nothing if the require call's result is not used.
else if (isValueUsed) {
newNode = t.identifier(
assertString(mod.meta.exportsIdentifier),
);
}
} else if (mod.type === 'js') {
newNode = addBundleImport(mod, node);
}
}
// async dependency that was internalized
if (
newNode &&
asyncResolution?.type === 'asset' &&
!isExpressionStatement(newNode)
) {
let _newNode = newNode; // For Flow
newNode = t.callExpression(
t.memberExpression(
t.identifier('Promise'),
t.identifier('resolve'),
),
[_newNode],
);
}
}
}
if (newNode) {
return newNode;
} else {
if (isUnusedValue(ancestors)) {
return REMOVE;
} else {
// e.g. $parcel$exportWildcard;
return t.objectExpression([]);
}
}
} else if (callee.name === '$parcel$require$resolve') {
let [id, source] = args;
if (
args.length !== 2 ||
!isStringLiteral(id) ||
!isStringLiteral(source)
) {
throw new Error(
'invariant: invalid signature, expected : $parcel$require$resolve(number, string)',
);
}
let mapped = nullthrows(assets.get(id.value));
let dep = nullthrows(
bundleGraph
.getDependencies(mapped)
.find(dep => dep.moduleSpecifier === source.value),
);
if (!bundleGraph.getDependencyResolution(dep, bundle)) {
// was excluded from bundling (e.g. includeNodeModules = false)
if (bundle.env.outputFormat !== 'commonjs') {
throw getThrowableDiagnosticForNode(
"'require.resolve' calls for excluded assets are only supported with outputFormat: 'commonjs'",
mapped.filePath,
convertBabelLoc(node.loc),
);
}
return REQUIRE_RESOLVE_CALL_TEMPLATE({
ID: t.stringLiteral(source.value),
});
} else {
throw getThrowableDiagnosticForNode(
"'require.resolve' calls for bundled modules or bundled assets aren't supported with scope hoisting",
mapped.filePath,
convertBabelLoc(node.loc),
);
}
} else if (callee.name === '$parcel$exportWildcard') {
if (args.length !== 2 || !isIdentifier(args[0])) {
throw new Error('Invalid call to $parcel$exportWildcard');
}
if (!needsExportsIdentifier(args[0].name)) {
return REMOVE;
}
} else if (callee.name === '$parcel$export') {
let [obj, symbol] = args;
invariant(isIdentifier(obj));
invariant(isStringLiteral(symbol));
let objName = obj.name;
let symbolName = symbol.value;
// Remove if the $id$exports object is unused.
if (!needsExportsIdentifier(objName)) {
return REMOVE;
}
if (objName === 'exports') {
// Assignment inside a wrapped asset
return;
}
let asset = nullthrows(exportsMap.get(objName));
let incomingDeps = bundleGraph.getIncomingDependencies(asset);
let unused = incomingDeps.every(d => {
let symbols = bundleGraph.getUsedSymbols(d);
return !symbols.has(symbolName) && !symbols.has('*');
});
if (unused) {
return REMOVE;
}
}
},
VariableDeclarator: {
exit(node) {
let {id} = node;
if (isIdentifier(id)) {
if (!needsExportsIdentifier(id.name)) {
return REMOVE;
}
if (!needsDeclaration(id.name)) {
return REMOVE;
}
}
},
},
VariableDeclaration: {
exit(node) {
if (node.declarations.length === 0) {
return REMOVE;
}
// Handle exported declarations using output format specific logic.
let exported = [];
for (let decl of node.declarations) {
let bindingIdentifiers = t.getBindingIdentifiers(decl.id);
for (let name in bindingIdentifiers) {
let exp = exportedSymbols.get(name);
if (exp) {
bindingIdentifiers[name].name = exp[0].local;
exported.push(...exp);
}
}
}
if (exported.length > 0) {
return format.generateMainExport(node, exported);
}
},
},
Declaration: {
exit(node) {
if (t.isVariableDeclaration(node)) {
return;
}
if (node.id != null && isIdentifier(node.id)) {
let id = node.id;
if (!needsDeclaration(id.name)) {
return REMOVE;
}
// Handle exported declarations using output format specific logic.
let exp = exportedSymbols.get(id.name);
if (exp) {
id.name = exp[0].local;
return format.generateMainExport(node, exp);
}
}
},
},
AssignmentExpression(node) {
let {left, right} = node;
if (isMemberExpression(left)) {
let {object, property, computed} = left;
if (
!(
isIdentifier(object) &&
((isIdentifier(property) && !computed) || isStringLiteral(property))
)
) {
return;
}
// Rename references to exported symbols to the exported name.
let exp = exportedSymbols.get(object.name);
if (exp) {
object.name = exp[0].local;
}
let asset = exportsMap.get(object.name);
if (!asset) {
return;
}
if (!needsExportsIdentifier(object.name)) {
return REMOVE;
}
if (isIdentifier(right) && !needsDeclaration(right.name)) {
return REMOVE;
}
}
if (isIdentifier(node.left)) {
let res = maybeReplaceIdentifier(node.left);
if (isIdentifier(res) || isMemberExpression(res)) {
node.left = res;
}
// remove unused CommonJS `$id$export$foo = $id$var$foo;`
if (isIdentifier(left) && !needsDeclaration(left.name)) {
return REMOVE;
}
}
},
Identifier(node, state, ancestors) {
if (
t.isReferenced(
node,
ancestors[ancestors.length - 2],
ancestors[ancestors.length - 3],
)
) {
// If referencing a helper, add it to the scope.
if (helpers.has(node.name)) {
scope.add(node.name);
return;
}
// Rename references to exported symbols to the exported name.
let exp = exportedSymbols.get(node.name);
if (exp) {
node.name = exp[0].local;
}
return maybeReplaceIdentifier(node);
}
},
ExpressionStatement: {
exit(node) {
// Handle exported declarations using output format specific logic.
if (
isAssignmentExpression(node.expression) &&
isIdentifier(node.expression.left)
) {
let left = node.expression.left;
let exp = exportedSymbols.get(left.name);
if (exp) {
left.name = exp[0].local;
return format.generateMainExport(node, exp);
}
}
},
},
SequenceExpression: {
exit(node) {
// This can happen if a $parcel$require result is unused.
if (node.expressions.length === 1) {
return node.expressions[0];
}
},
},
Program: {
exit(node) {
let statements: Array<Statement> = node.body;
let hoistedImports = [];
for (let file of importedFiles.values()) {
if (file.bundle) {
let {hoisted, imports} = format.generateBundleImports(
bundleGraph,
bundle,
file,
scope,
);
statements = imports.concat(statements);
hoistedImports = hoistedImports.concat(hoisted);
} else {
let res = format.generateExternalImport(bundle, file, scope);
statements = res.concat(statements);
}
}
if (referencedAssets.size > 0) {
// Insert fake init functions that will be imported in other bundles,
// because `asset.meta.shouldWrap` isn't set in a packager if `asset` is
// not in the current bundle.
statements = statements.concat(
([...referencedAssets]: Array<Asset>)
.filter(a => !wrappedAssets.has(a.id))
.map(a => {
return FAKE_INIT_TEMPLATE({
INIT: getIdentifier(a, 'init'),
EXPORTS: t.identifier(assertString(a.meta.exportsIdentifier)),
});
}),