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