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