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