-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathESMOutputFormat.js
More file actions
119 lines (103 loc) · 3.08 KB
/
Copy pathESMOutputFormat.js
File metadata and controls
119 lines (103 loc) · 3.08 KB
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
// @flow
import type {
ScopeHoistingPackager,
OutputFormat,
} from './ScopeHoistingPackager';
export class ESMOutputFormat implements OutputFormat {
packager: ScopeHoistingPackager;
constructor(packager: ScopeHoistingPackager) {
this.packager = packager;
}
buildBundlePrelude(): [string, number] {
let res = '';
let lines = 0;
for (let [source, specifiers] of this.packager.externals) {
let defaultSpecifier = null;
let namespaceSpecifier = null;
let namedSpecifiers = [];
for (let [imported, symbol] of specifiers) {
if (imported === 'default' /* || isCommonJS*/) {
defaultSpecifier = symbol;
} else if (imported === '*') {
namespaceSpecifier = `* as ${symbol}`;
} else {
let specifier = imported;
if (symbol !== imported) {
specifier += ` as ${symbol}`;
}
namedSpecifiers.push(specifier);
}
}
// ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
let imported = '';
if (namespaceSpecifier) {
let s = namespaceSpecifier;
if (defaultSpecifier) {
s = `${defaultSpecifier}, ${namespaceSpecifier}`;
}
res += `import ${s} from ${JSON.stringify(source)};\n`;
lines++;
} else if (defaultSpecifier) {
imported = defaultSpecifier;
if (namedSpecifiers.length > 0) {
imported += `, {${namedSpecifiers.join(', ')}}`;
}
} else if (namedSpecifiers.length > 0) {
imported = `{${namedSpecifiers.join(', ')}}`;
}
if (imported.length > 0) {
res += `import ${imported} from ${JSON.stringify(source)};\n`;
lines++;
} else if (!namespaceSpecifier) {
res += `import ${JSON.stringify(source)};\n`;
lines++;
}
}
if (res.length > 0) {
res += '\n';
lines++;
}
return [res, lines];
}
buildBundlePostlude(): [string, number] {
let res = '';
let lines = 0;
let exportSpecifiers = [];
for (let {
asset,
exportSymbol,
local,
exportAs,
} of this.packager.exportedSymbols.values()) {
if (this.packager.wrappedAssets.has(asset.id)) {
let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(
asset,
)}")`;
res += `\nvar ${local} = ${this.packager.getPropertyAccess(
obj,
exportSymbol,
)};`;
lines++;
}
for (let as of exportAs) {
let specifier = local;
if (exportAs !== local) {
specifier += ` as ${as}`;
}
exportSpecifiers.push(specifier);
}
}
if (exportSpecifiers.length > 0) {
res += `\nexport {${exportSpecifiers.join(', ')}};`;
lines++;
}
if (this.packager.shouldBundleQueue(this.packager.bundle)) {
// Should be last thing the bundle executes on intial eval
res += `\n$parcel$global.rlb(${JSON.stringify(
this.packager.bundle.publicId,
)})`;
lines++;
}
return [res, lines];
}
}