@parcel/packager-js 2.13.3 → 2.14.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/DevPackager.js +64 -2
- package/lib/ScopeHoistingPackager.js +39 -9
- package/lib/dev-prelude.js +19 -5
- package/lib/helpers.js +133 -1
- package/lib/index.js +2 -2
- package/package.json +8 -8
- package/src/DevPackager.js +82 -4
- package/src/ScopeHoistingPackager.js +54 -8
- package/src/dev-prelude.js +19 -5
- package/src/helpers.js +144 -1
- package/src/index.js +1 -1
package/lib/DevPackager.js
CHANGED
|
@@ -40,6 +40,7 @@ function _fs() {
|
|
|
40
40
|
return data;
|
|
41
41
|
}
|
|
42
42
|
var _utils2 = require("./utils");
|
|
43
|
+
var _helpers = require("./helpers");
|
|
43
44
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
44
45
|
const PRELUDE = _fs().default.readFileSync(_path().default.join(__dirname, 'dev-prelude.js'), 'utf8').trim().replace(/;$/, '');
|
|
45
46
|
class DevPackager {
|
|
@@ -71,6 +72,7 @@ class DevPackager {
|
|
|
71
72
|
let prefix = this.getPrefix();
|
|
72
73
|
let lineOffset = (0, _utils().countLines)(prefix);
|
|
73
74
|
let script = null;
|
|
75
|
+
let usedHelpers = 0;
|
|
74
76
|
this.bundle.traverse(node => {
|
|
75
77
|
let wrapped = first ? '' : ',';
|
|
76
78
|
if (node.type === 'dependency') {
|
|
@@ -87,6 +89,9 @@ class DevPackager {
|
|
|
87
89
|
if (node.type === 'asset') {
|
|
88
90
|
let asset = node.value;
|
|
89
91
|
(0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
|
|
92
|
+
if (typeof asset.meta.usedHelpers === 'number') {
|
|
93
|
+
usedHelpers |= asset.meta.usedHelpers;
|
|
94
|
+
}
|
|
90
95
|
|
|
91
96
|
// If this is the main entry of a script rather than a module, we need to hoist it
|
|
92
97
|
// outside the bundle wrapper function so that its variables are exposed as globals.
|
|
@@ -146,7 +151,7 @@ class DevPackager {
|
|
|
146
151
|
});
|
|
147
152
|
let entries = this.bundle.getEntryAssets();
|
|
148
153
|
let mainEntry = this.bundle.getMainEntry();
|
|
149
|
-
if (!this.isEntry() && this.bundle.env.outputFormat
|
|
154
|
+
if (!this.isEntry() && this.bundle.env.outputFormat !== 'commonjs' || this.bundle.env.sourceType === 'script') {
|
|
150
155
|
// In async bundles we don't want the main entry to execute until we require it
|
|
151
156
|
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
152
157
|
entries = entries.filter(a => {
|
|
@@ -155,7 +160,50 @@ class DevPackager {
|
|
|
155
160
|
});
|
|
156
161
|
mainEntry = null;
|
|
157
162
|
}
|
|
158
|
-
let
|
|
163
|
+
let load = '';
|
|
164
|
+
if (usedHelpers & 4) {
|
|
165
|
+
load += _helpers.helpers.$parcel$import(this.bundle.env, this.bundle, new Set());
|
|
166
|
+
load += 'newRequire.load = $parcel$import;\n';
|
|
167
|
+
}
|
|
168
|
+
if (usedHelpers & 8) {
|
|
169
|
+
load += _helpers.helpers.$parcel$resolve(this.bundle.env, this.bundle, new Set());
|
|
170
|
+
load += 'newRequire.resolve = $parcel$resolve;\n';
|
|
171
|
+
}
|
|
172
|
+
if (usedHelpers & 16) {
|
|
173
|
+
load += _helpers.helpers.$parcel$extendImportMap(this.bundle.env);
|
|
174
|
+
load += `newRequire.extendImportMap = $parcel$extendImportMap;\n`;
|
|
175
|
+
}
|
|
176
|
+
if (load) {
|
|
177
|
+
usedHelpers |= 1 | 2;
|
|
178
|
+
// Remove newlines to avoid messing up source maps
|
|
179
|
+
prefix = prefix.replace('// INSERT_LOAD_HERE', load.replace(/\n/g, ''));
|
|
180
|
+
}
|
|
181
|
+
let contents = prefix + '({' + assets + '},' + JSON.stringify(entries.map(asset => this.bundleGraph.getAssetPublicId(asset))) + ', ' + JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) + ', ' + JSON.stringify(this.parcelRequireName);
|
|
182
|
+
if (usedHelpers & 1) {
|
|
183
|
+
// Generate a relative path from this bundle to the root of the dist dir.
|
|
184
|
+
let distDir = (0, _utils().relativePath)(_path().default.dirname(this.bundle.name), '');
|
|
185
|
+
if (!distDir.endsWith('/')) {
|
|
186
|
+
distDir += '/';
|
|
187
|
+
}
|
|
188
|
+
contents += ', ' + JSON.stringify(distDir);
|
|
189
|
+
} else if (usedHelpers & (2 | 32)) {
|
|
190
|
+
contents += ', null';
|
|
191
|
+
}
|
|
192
|
+
if (usedHelpers & 2) {
|
|
193
|
+
// Ensure the public url always ends with a slash to code can easily join paths to it.
|
|
194
|
+
let publicUrl = this.bundle.target.publicUrl;
|
|
195
|
+
if (!publicUrl.endsWith('/')) {
|
|
196
|
+
publicUrl += '/';
|
|
197
|
+
}
|
|
198
|
+
contents += ', ' + JSON.stringify(publicUrl);
|
|
199
|
+
} else if (usedHelpers & 32) {
|
|
200
|
+
contents += ', null';
|
|
201
|
+
}
|
|
202
|
+
if (usedHelpers & 32) {
|
|
203
|
+
let code = _helpers.helpers.$parcel$devServer(this.bundle.env, this.bundle, new Set(), this.options);
|
|
204
|
+
contents += ', ' + code.slice('var $parcel$devServer = '.length, -2);
|
|
205
|
+
}
|
|
206
|
+
contents += ')\n';
|
|
159
207
|
|
|
160
208
|
// The entry asset of a script bundle gets hoisted outside the bundle wrapper function
|
|
161
209
|
// so that its variables become globals. We need to replace any require calls for
|
|
@@ -190,6 +238,20 @@ class DevPackager {
|
|
|
190
238
|
for (let b of bundles) {
|
|
191
239
|
importScripts += `importScripts("${(0, _utils().relativeBundlePath)(this.bundle, b)}");\n`;
|
|
192
240
|
}
|
|
241
|
+
} else if (this.bundle.env.isNode()) {
|
|
242
|
+
let bundles = this.bundleGraph.getReferencedBundles(this.bundle, {
|
|
243
|
+
includeInline: false
|
|
244
|
+
});
|
|
245
|
+
for (let b of bundles) {
|
|
246
|
+
if (b.type !== 'js') {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (this.bundle.env.outputFormat === 'esmodule') {
|
|
250
|
+
importScripts += `import "${(0, _utils().relativeBundlePath)(this.bundle, b)}";\n`;
|
|
251
|
+
} else {
|
|
252
|
+
importScripts += `require("${(0, _utils().relativeBundlePath)(this.bundle, b)}");\n`;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
193
255
|
}
|
|
194
256
|
return (
|
|
195
257
|
// If the entry asset included a hashbang, repeat it at the top of the bundle
|
|
@@ -65,14 +65,18 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
|
|
|
65
65
|
// and count the number of newlines in the file for source maps.
|
|
66
66
|
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;
|
|
67
67
|
const BUILTINS = Object.keys(_globals().default.builtin);
|
|
68
|
+
const BROWSER_BUILTINS = new Set([...BUILTINS, ...Object.keys(_globals().default.browser)]);
|
|
69
|
+
const NODE_BUILTINS = new Set([...BUILTINS, ...Object.keys(_globals().default.node)]);
|
|
68
70
|
const GLOBALS_BY_CONTEXT = {
|
|
69
|
-
browser:
|
|
71
|
+
browser: BROWSER_BUILTINS,
|
|
72
|
+
'react-client': BROWSER_BUILTINS,
|
|
70
73
|
'web-worker': new Set([...BUILTINS, ...Object.keys(_globals().default.worker)]),
|
|
71
74
|
'service-worker': new Set([...BUILTINS, ...Object.keys(_globals().default.serviceworker)]),
|
|
72
75
|
worklet: new Set([...BUILTINS]),
|
|
73
|
-
node:
|
|
74
|
-
'electron-main':
|
|
75
|
-
'electron-renderer': new Set([...BUILTINS, ...Object.keys(_globals().default.node), ...Object.keys(_globals().default.browser)])
|
|
76
|
+
node: NODE_BUILTINS,
|
|
77
|
+
'electron-main': NODE_BUILTINS,
|
|
78
|
+
'electron-renderer': new Set([...BUILTINS, ...Object.keys(_globals().default.node), ...Object.keys(_globals().default.browser)]),
|
|
79
|
+
'react-server': NODE_BUILTINS
|
|
76
80
|
};
|
|
77
81
|
const OUTPUT_FORMATS = {
|
|
78
82
|
esmodule: _ESMOutputFormat.ESMOutputFormat,
|
|
@@ -110,11 +114,13 @@ class ScopeHoistingPackager {
|
|
|
110
114
|
// runtimes are excluded, and instead we add imports into the entry bundle
|
|
111
115
|
// of each bundle group pointing at the sibling bundles. These can be
|
|
112
116
|
// picked up by another bundler later at which point runtimes will be added.
|
|
113
|
-
if (this.bundle.env.isLibrary || this.bundle.env.outputFormat === 'commonjs') {
|
|
117
|
+
if (this.bundle.env.isLibrary || this.bundle.env.outputFormat === 'commonjs' || this.bundle.env.outputFormat === 'esmodule' && !this.isAsyncBundle) {
|
|
114
118
|
for (let b of this.bundleGraph.getReferencedBundles(this.bundle, {
|
|
115
119
|
recursive: false
|
|
116
120
|
})) {
|
|
117
|
-
|
|
121
|
+
if (this.bundle.env.isLibrary || b.type === 'js') {
|
|
122
|
+
this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), new Map());
|
|
123
|
+
}
|
|
118
124
|
}
|
|
119
125
|
}
|
|
120
126
|
let res = '';
|
|
@@ -286,7 +292,7 @@ class ScopeHoistingPackager {
|
|
|
286
292
|
// If the module has a namespace (e.g. commonjs), and this is not an entry, only export the namespace
|
|
287
293
|
// as default, without individual exports. This mirrors the importing logic in addExternal, avoiding
|
|
288
294
|
// extra unused exports and potential for non-identifier export names.
|
|
289
|
-
if (hasNamespace && this.
|
|
295
|
+
if (hasNamespace && !this.bundle.needsStableName && exportAs !== '*') {
|
|
290
296
|
continue;
|
|
291
297
|
}
|
|
292
298
|
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;
|
|
@@ -387,6 +393,30 @@ class ScopeHoistingPackager {
|
|
|
387
393
|
if (code.includes('$parcel$global')) {
|
|
388
394
|
this.usedHelpers.add('$parcel$global');
|
|
389
395
|
}
|
|
396
|
+
let usedHelpers = asset.meta.usedHelpers;
|
|
397
|
+
if (typeof usedHelpers === 'number') {
|
|
398
|
+
if (usedHelpers & 1) {
|
|
399
|
+
this.usedHelpers.add('$parcel$distDir');
|
|
400
|
+
}
|
|
401
|
+
if (usedHelpers & 2) {
|
|
402
|
+
this.usedHelpers.add('$parcel$publicUrl');
|
|
403
|
+
}
|
|
404
|
+
if (usedHelpers & 4) {
|
|
405
|
+
this.needsPrelude = true;
|
|
406
|
+
this.usedHelpers.add('$parcel$import');
|
|
407
|
+
}
|
|
408
|
+
if (usedHelpers & 8) {
|
|
409
|
+
this.needsPrelude = true;
|
|
410
|
+
this.usedHelpers.add('$parcel$resolve');
|
|
411
|
+
}
|
|
412
|
+
if (usedHelpers & 16) {
|
|
413
|
+
this.needsPrelude = true;
|
|
414
|
+
this.usedHelpers.add('$parcel$extendImportMap');
|
|
415
|
+
}
|
|
416
|
+
if (usedHelpers & 32) {
|
|
417
|
+
this.usedHelpers.add('$parcel$devServer');
|
|
418
|
+
}
|
|
419
|
+
}
|
|
390
420
|
if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
|
|
391
421
|
const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
|
|
392
422
|
code = code.replace('$parcel$dirnameReplace', relPath);
|
|
@@ -982,7 +1012,7 @@ ${code}
|
|
|
982
1012
|
for (let helper of this.usedHelpers) {
|
|
983
1013
|
let currentHelper = _helpers.helpers[helper];
|
|
984
1014
|
if (typeof currentHelper === 'function') {
|
|
985
|
-
currentHelper = _helpers.helpers[helper](this.bundle.env);
|
|
1015
|
+
currentHelper = _helpers.helpers[helper](this.bundle.env, this.bundle, this.usedHelpers, this.options);
|
|
986
1016
|
}
|
|
987
1017
|
res += currentHelper;
|
|
988
1018
|
if (enableSourceMaps) {
|
|
@@ -993,7 +1023,7 @@ ${code}
|
|
|
993
1023
|
// Add the prelude if this is potentially the first JS bundle to load in a
|
|
994
1024
|
// particular context (e.g. entry scripts in HTML, workers, etc.).
|
|
995
1025
|
let parentBundles = this.bundleGraph.getParentBundles(this.bundle);
|
|
996
|
-
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';
|
|
1026
|
+
let mightBeFirstJS = parentBundles.length === 0 || parentBundles.some(b => b.type !== 'js' || b.env.context !== this.bundle.env.context) || this.bundleGraph.getBundleGroupsContainingBundle(this.bundle).some(g => this.bundleGraph.isEntryBundleGroup(g)) || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated';
|
|
997
1027
|
if (mightBeFirstJS) {
|
|
998
1028
|
let preludeCode = (0, _helpers.prelude)(this.parcelRequireName);
|
|
999
1029
|
res += preludeCode;
|
package/lib/dev-prelude.js
CHANGED
|
@@ -6,7 +6,15 @@
|
|
|
6
6
|
// anything defined in a previous bundle is accessed via the
|
|
7
7
|
// orig method which is the require for previous bundles
|
|
8
8
|
|
|
9
|
-
(function (
|
|
9
|
+
(function (
|
|
10
|
+
modules,
|
|
11
|
+
entry,
|
|
12
|
+
mainEntry,
|
|
13
|
+
parcelRequireName,
|
|
14
|
+
distDir,
|
|
15
|
+
publicUrl,
|
|
16
|
+
devServer
|
|
17
|
+
) {
|
|
10
18
|
/* eslint-disable no-undef */
|
|
11
19
|
var globalObject =
|
|
12
20
|
typeof globalThis !== 'undefined'
|
|
@@ -25,6 +33,7 @@
|
|
|
25
33
|
typeof globalObject[parcelRequireName] === 'function' &&
|
|
26
34
|
globalObject[parcelRequireName];
|
|
27
35
|
|
|
36
|
+
var importMap = previousRequire.i || {};
|
|
28
37
|
var cache = previousRequire.cache || {};
|
|
29
38
|
// Do not use `require` to prevent Webpack from trying to bundle this call
|
|
30
39
|
var nodeRequire =
|
|
@@ -93,6 +102,7 @@
|
|
|
93
102
|
function Module(moduleName) {
|
|
94
103
|
this.id = moduleName;
|
|
95
104
|
this.bundle = newRequire;
|
|
105
|
+
this.require = nodeRequire;
|
|
96
106
|
this.exports = {};
|
|
97
107
|
}
|
|
98
108
|
|
|
@@ -101,6 +111,10 @@
|
|
|
101
111
|
newRequire.modules = modules;
|
|
102
112
|
newRequire.cache = cache;
|
|
103
113
|
newRequire.parent = previousRequire;
|
|
114
|
+
newRequire.distDir = distDir;
|
|
115
|
+
newRequire.publicUrl = publicUrl;
|
|
116
|
+
newRequire.devServer = devServer;
|
|
117
|
+
newRequire.i = importMap;
|
|
104
118
|
newRequire.register = function (id, exports) {
|
|
105
119
|
modules[id] = [
|
|
106
120
|
function (require, module) {
|
|
@@ -110,6 +124,10 @@
|
|
|
110
124
|
];
|
|
111
125
|
};
|
|
112
126
|
|
|
127
|
+
// Only insert newRequire.load when it is actually used.
|
|
128
|
+
// The code in this file is linted against ES5, so dynamic import is not allowed.
|
|
129
|
+
// INSERT_LOAD_HERE
|
|
130
|
+
|
|
113
131
|
Object.defineProperty(newRequire, 'root', {
|
|
114
132
|
get: function () {
|
|
115
133
|
return globalObject[parcelRequireName];
|
|
@@ -136,10 +154,6 @@
|
|
|
136
154
|
define(function () {
|
|
137
155
|
return mainExports;
|
|
138
156
|
});
|
|
139
|
-
|
|
140
|
-
// <script>
|
|
141
|
-
} else if (globalName) {
|
|
142
|
-
this[globalName] = mainExports;
|
|
143
157
|
}
|
|
144
158
|
}
|
|
145
159
|
});
|
package/lib/helpers.js
CHANGED
|
@@ -4,6 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.prelude = exports.helpers = exports.fnExpr = exports.bundleQueuePrelude = void 0;
|
|
7
|
+
function _utils() {
|
|
8
|
+
const data = require("@parcel/utils");
|
|
9
|
+
_utils = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _path() {
|
|
15
|
+
const data = _interopRequireDefault(require("path"));
|
|
16
|
+
_path = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
22
|
const prelude = parcelRequireName => `
|
|
8
23
|
var $parcel$modules = {};
|
|
9
24
|
var $parcel$inits = {};
|
|
@@ -120,10 +135,127 @@ function $parcel$defineInteropFlag(a) {
|
|
|
120
135
|
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
121
136
|
}
|
|
122
137
|
`;
|
|
138
|
+
const $parcel$distDir = (env, bundle) => {
|
|
139
|
+
// Generate a relative path from this bundle to the root of the dist dir.
|
|
140
|
+
let distDir = (0, _utils().relativePath)(_path().default.dirname(bundle.name), '');
|
|
141
|
+
if (!distDir.endsWith('/')) {
|
|
142
|
+
distDir += '/';
|
|
143
|
+
}
|
|
144
|
+
return `var $parcel$distDir = ${JSON.stringify(distDir)};\n`;
|
|
145
|
+
};
|
|
146
|
+
const $parcel$publicUrl = (env, bundle) => {
|
|
147
|
+
// Ensure the public url always ends with a slash to code can easily join paths to it.
|
|
148
|
+
let publicUrl = bundle.target.publicUrl;
|
|
149
|
+
if (!publicUrl.endsWith('/')) {
|
|
150
|
+
publicUrl += '/';
|
|
151
|
+
}
|
|
152
|
+
return `var $parcel$publicUrl = ${JSON.stringify(publicUrl)};\n`;
|
|
153
|
+
};
|
|
154
|
+
const $parcel$devServer = (env, bundle, _usedHelpers, options) => {
|
|
155
|
+
if (options.hmrOptions) {
|
|
156
|
+
let {
|
|
157
|
+
host = 'localhost',
|
|
158
|
+
port
|
|
159
|
+
} = options.hmrOptions;
|
|
160
|
+
let https = options.serveOptions ? options.serveOptions.https : false;
|
|
161
|
+
port = port ?? (options.serveOptions ? options.serveOptions.port : null);
|
|
162
|
+
if (port != null) {
|
|
163
|
+
let url = (https ? 'https://' : 'http://') + host + ':' + port;
|
|
164
|
+
return `var $parcel$devServer = ${JSON.stringify(url)};\n`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return `var $parcel$devServer = null;\n`;
|
|
168
|
+
};
|
|
169
|
+
const $parcel$extendImportMap = env => {
|
|
170
|
+
let defineImportMap = env.shouldScopeHoist ? 'parcelRequire.i ??= {}' : 'importMap';
|
|
171
|
+
return `
|
|
172
|
+
function $parcel$extendImportMap(map) {
|
|
173
|
+
Object.assign(${defineImportMap}, map);
|
|
174
|
+
}
|
|
175
|
+
`;
|
|
176
|
+
};
|
|
177
|
+
const $parcel$import = (env, bundle, usedHelpers) => {
|
|
178
|
+
usedHelpers.add('$parcel$distDir');
|
|
179
|
+
let distDir = env.shouldScopeHoist ? '$parcel$distDir' : 'distDir';
|
|
180
|
+
let importMap = env.shouldScopeHoist ? 'parcelRequire.i?.' : 'importMap';
|
|
181
|
+
return `
|
|
182
|
+
function $parcel$import(url) {
|
|
183
|
+
url = ${importMap}[url] || url;
|
|
184
|
+
return import(${distDir} + url);
|
|
185
|
+
}
|
|
186
|
+
`;
|
|
187
|
+
};
|
|
188
|
+
const $parcel$resolve = (env, bundle, usedHelpers) => {
|
|
189
|
+
let distDir = env.shouldScopeHoist ? '$parcel$distDir' : 'distDir';
|
|
190
|
+
let publicUrl = env.shouldScopeHoist ? '$parcel$publicUrl' : 'publicUrl';
|
|
191
|
+
let importMap = env.shouldScopeHoist ? 'parcelRequire.i?.' : 'importMap';
|
|
192
|
+
if (env.context === 'react-server' || env.context === 'react-client') {
|
|
193
|
+
usedHelpers.add('$parcel$publicUrl');
|
|
194
|
+
return `
|
|
195
|
+
function $parcel$resolve(url) {
|
|
196
|
+
url = ${importMap}[url] || url;
|
|
197
|
+
return ${publicUrl} + url;
|
|
198
|
+
}
|
|
199
|
+
`;
|
|
200
|
+
} else if (env.outputFormat === 'esmodule' && env.supports('import-meta-resolve')) {
|
|
201
|
+
usedHelpers.add('$parcel$distDir');
|
|
202
|
+
return `
|
|
203
|
+
function $parcel$resolve(url) {
|
|
204
|
+
url = ${importMap}[url] || url;
|
|
205
|
+
return import.meta.resolve(${distDir} + url);
|
|
206
|
+
}
|
|
207
|
+
`;
|
|
208
|
+
} else if (env.outputFormat === 'esmodule' && env.supports('import-meta-url')) {
|
|
209
|
+
usedHelpers.add('$parcel$distDir');
|
|
210
|
+
return `
|
|
211
|
+
function $parcel$resolve(url) {
|
|
212
|
+
url = ${importMap}[url] || url;
|
|
213
|
+
return new URL(${distDir} + url, import.meta.url).toString();
|
|
214
|
+
}
|
|
215
|
+
`;
|
|
216
|
+
} else if (env.outputFormat === 'commonjs' || env.isNode()) {
|
|
217
|
+
usedHelpers.add('$parcel$distDir');
|
|
218
|
+
return `
|
|
219
|
+
function $parcel$resolve(url) {
|
|
220
|
+
url = ${importMap}[url] || url;
|
|
221
|
+
return new URL(${distDir} + url, 'file:' + __filename).toString();
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
} else {
|
|
225
|
+
usedHelpers.add('$parcel$distDir');
|
|
226
|
+
return `
|
|
227
|
+
var $parcel$bundleURL;
|
|
228
|
+
function $parcel$resolve(url) {
|
|
229
|
+
url = ${importMap}[url] || url;
|
|
230
|
+
if (!$parcel$bundleURL) {
|
|
231
|
+
try {
|
|
232
|
+
throw new Error();
|
|
233
|
+
} catch (err) {
|
|
234
|
+
var matches = ('' + err.stack).match(
|
|
235
|
+
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\\/\\/[^)\\n]+/g,
|
|
236
|
+
);
|
|
237
|
+
if (matches) {
|
|
238
|
+
$parcel$bundleURL = matches[0];
|
|
239
|
+
} else {
|
|
240
|
+
return ${distDir} + url;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return new URL(${distDir} + url, $parcel$bundleURL).toString();
|
|
245
|
+
}
|
|
246
|
+
`;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
123
249
|
const helpers = exports.helpers = {
|
|
124
250
|
$parcel$export,
|
|
125
251
|
$parcel$exportWildcard,
|
|
126
252
|
$parcel$interopDefault,
|
|
127
253
|
$parcel$global,
|
|
128
|
-
$parcel$defineInteropFlag
|
|
254
|
+
$parcel$defineInteropFlag,
|
|
255
|
+
$parcel$distDir,
|
|
256
|
+
$parcel$publicUrl,
|
|
257
|
+
$parcel$devServer,
|
|
258
|
+
$parcel$extendImportMap,
|
|
259
|
+
$parcel$import,
|
|
260
|
+
$parcel$resolve
|
|
129
261
|
};
|
package/lib/index.js
CHANGED
|
@@ -56,7 +56,7 @@ var _default = exports.default = new (_plugin().Packager)({
|
|
|
56
56
|
config,
|
|
57
57
|
options
|
|
58
58
|
}) {
|
|
59
|
-
var
|
|
59
|
+
var _conf$contents;
|
|
60
60
|
let packageKey = '@parcel/packager-js';
|
|
61
61
|
let conf = await config.getConfigFrom(options.projectRoot + '/index', [], {
|
|
62
62
|
packageKey
|
|
@@ -75,7 +75,7 @@ var _default = exports.default = new (_plugin().Packager)({
|
|
|
75
75
|
let packageName = await config.getConfigFrom(options.projectRoot + '/index', [], {
|
|
76
76
|
packageKey: 'name'
|
|
77
77
|
});
|
|
78
|
-
let name = (packageName === null || packageName === void 0
|
|
78
|
+
let name = (packageName === null || packageName === void 0 ? void 0 : packageName.contents) ?? '';
|
|
79
79
|
return {
|
|
80
80
|
parcelRequireName: 'parcelRequire' + (0, _rust().hashString)(name).slice(-4),
|
|
81
81
|
unstable_asyncBundleRuntime: Boolean(conf === null || conf === void 0 || (_conf$contents = conf.contents) === null || _conf$contents === void 0 ? void 0 : _conf$contents.unstable_asyncBundleRuntime)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/packager-js",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,17 +17,17 @@
|
|
|
17
17
|
"source": "src/index.js",
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">= 16.0.0",
|
|
20
|
-
"parcel": "^2.
|
|
20
|
+
"parcel": "^2.14.1"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@parcel/diagnostic": "2.
|
|
24
|
-
"@parcel/plugin": "2.
|
|
25
|
-
"@parcel/rust": "2.
|
|
23
|
+
"@parcel/diagnostic": "2.14.1",
|
|
24
|
+
"@parcel/plugin": "2.14.1",
|
|
25
|
+
"@parcel/rust": "2.14.1",
|
|
26
26
|
"@parcel/source-map": "^2.1.1",
|
|
27
|
-
"@parcel/types": "2.
|
|
28
|
-
"@parcel/utils": "2.
|
|
27
|
+
"@parcel/types": "2.14.1",
|
|
28
|
+
"@parcel/utils": "2.14.1",
|
|
29
29
|
"globals": "^13.2.0",
|
|
30
30
|
"nullthrows": "^1.1.1"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "86bddb4031136f4b1b76e1a9a4c8a53ff4280b42"
|
|
33
33
|
}
|
package/src/DevPackager.js
CHANGED
|
@@ -6,12 +6,14 @@ import {
|
|
|
6
6
|
relativeBundlePath,
|
|
7
7
|
countLines,
|
|
8
8
|
normalizeSeparators,
|
|
9
|
+
relativePath,
|
|
9
10
|
} from '@parcel/utils';
|
|
10
11
|
import SourceMap from '@parcel/source-map';
|
|
11
12
|
import invariant from 'assert';
|
|
12
13
|
import path from 'path';
|
|
13
14
|
import fs from 'fs';
|
|
14
15
|
import {replaceScriptDependencies, getSpecifier} from './utils';
|
|
16
|
+
import {helpers} from './helpers';
|
|
15
17
|
|
|
16
18
|
const PRELUDE = fs
|
|
17
19
|
.readFileSync(path.join(__dirname, 'dev-prelude.js'), 'utf8')
|
|
@@ -60,6 +62,7 @@ export class DevPackager {
|
|
|
60
62
|
let lineOffset = countLines(prefix);
|
|
61
63
|
let script: ?{|code: string, mapBuffer: ?Buffer|} = null;
|
|
62
64
|
|
|
65
|
+
let usedHelpers = 0;
|
|
63
66
|
this.bundle.traverse(node => {
|
|
64
67
|
let wrapped = first ? '' : ',';
|
|
65
68
|
|
|
@@ -87,6 +90,10 @@ export class DevPackager {
|
|
|
87
90
|
'all assets in a js bundle must be js assets',
|
|
88
91
|
);
|
|
89
92
|
|
|
93
|
+
if (typeof asset.meta.usedHelpers === 'number') {
|
|
94
|
+
usedHelpers |= asset.meta.usedHelpers;
|
|
95
|
+
}
|
|
96
|
+
|
|
90
97
|
// If this is the main entry of a script rather than a module, we need to hoist it
|
|
91
98
|
// outside the bundle wrapper function so that its variables are exposed as globals.
|
|
92
99
|
if (
|
|
@@ -171,7 +178,7 @@ export class DevPackager {
|
|
|
171
178
|
let entries = this.bundle.getEntryAssets();
|
|
172
179
|
let mainEntry = this.bundle.getMainEntry();
|
|
173
180
|
if (
|
|
174
|
-
(!this.isEntry() && this.bundle.env.outputFormat
|
|
181
|
+
(!this.isEntry() && this.bundle.env.outputFormat !== 'commonjs') ||
|
|
175
182
|
this.bundle.env.sourceType === 'script'
|
|
176
183
|
) {
|
|
177
184
|
// In async bundles we don't want the main entry to execute until we require it
|
|
@@ -180,6 +187,28 @@ export class DevPackager {
|
|
|
180
187
|
mainEntry = null;
|
|
181
188
|
}
|
|
182
189
|
|
|
190
|
+
let load = '';
|
|
191
|
+
if (usedHelpers & 4) {
|
|
192
|
+
load += helpers.$parcel$import(this.bundle.env, this.bundle, new Set());
|
|
193
|
+
load += 'newRequire.load = $parcel$import;\n';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (usedHelpers & 8) {
|
|
197
|
+
load += helpers.$parcel$resolve(this.bundle.env, this.bundle, new Set());
|
|
198
|
+
load += 'newRequire.resolve = $parcel$resolve;\n';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (usedHelpers & 16) {
|
|
202
|
+
load += helpers.$parcel$extendImportMap(this.bundle.env);
|
|
203
|
+
load += `newRequire.extendImportMap = $parcel$extendImportMap;\n`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (load) {
|
|
207
|
+
usedHelpers |= 1 | 2;
|
|
208
|
+
// Remove newlines to avoid messing up source maps
|
|
209
|
+
prefix = prefix.replace('// INSERT_LOAD_HERE', load.replace(/\n/g, ''));
|
|
210
|
+
}
|
|
211
|
+
|
|
183
212
|
let contents =
|
|
184
213
|
prefix +
|
|
185
214
|
'({' +
|
|
@@ -193,9 +222,41 @@ export class DevPackager {
|
|
|
193
222
|
mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null,
|
|
194
223
|
) +
|
|
195
224
|
', ' +
|
|
196
|
-
JSON.stringify(this.parcelRequireName)
|
|
197
|
-
|
|
198
|
-
|
|
225
|
+
JSON.stringify(this.parcelRequireName);
|
|
226
|
+
|
|
227
|
+
if (usedHelpers & 1) {
|
|
228
|
+
// Generate a relative path from this bundle to the root of the dist dir.
|
|
229
|
+
let distDir = relativePath(path.dirname(this.bundle.name), '');
|
|
230
|
+
if (!distDir.endsWith('/')) {
|
|
231
|
+
distDir += '/';
|
|
232
|
+
}
|
|
233
|
+
contents += ', ' + JSON.stringify(distDir);
|
|
234
|
+
} else if (usedHelpers & (2 | 32)) {
|
|
235
|
+
contents += ', null';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (usedHelpers & 2) {
|
|
239
|
+
// Ensure the public url always ends with a slash to code can easily join paths to it.
|
|
240
|
+
let publicUrl = this.bundle.target.publicUrl;
|
|
241
|
+
if (!publicUrl.endsWith('/')) {
|
|
242
|
+
publicUrl += '/';
|
|
243
|
+
}
|
|
244
|
+
contents += ', ' + JSON.stringify(publicUrl);
|
|
245
|
+
} else if (usedHelpers & 32) {
|
|
246
|
+
contents += ', null';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (usedHelpers & 32) {
|
|
250
|
+
let code = helpers.$parcel$devServer(
|
|
251
|
+
this.bundle.env,
|
|
252
|
+
this.bundle,
|
|
253
|
+
new Set(),
|
|
254
|
+
this.options,
|
|
255
|
+
);
|
|
256
|
+
contents += ', ' + code.slice('var $parcel$devServer = '.length, -2);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
contents += ')\n';
|
|
199
260
|
|
|
200
261
|
// The entry asset of a script bundle gets hoisted outside the bundle wrapper function
|
|
201
262
|
// so that its variables become globals. We need to replace any require calls for
|
|
@@ -242,6 +303,23 @@ export class DevPackager {
|
|
|
242
303
|
b,
|
|
243
304
|
)}");\n`;
|
|
244
305
|
}
|
|
306
|
+
} else if (this.bundle.env.isNode()) {
|
|
307
|
+
let bundles = this.bundleGraph.getReferencedBundles(this.bundle, {
|
|
308
|
+
includeInline: false,
|
|
309
|
+
});
|
|
310
|
+
for (let b of bundles) {
|
|
311
|
+
if (b.type !== 'js') {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (this.bundle.env.outputFormat === 'esmodule') {
|
|
315
|
+
importScripts += `import "${relativeBundlePath(this.bundle, b)}";\n`;
|
|
316
|
+
} else {
|
|
317
|
+
importScripts += `require("${relativeBundlePath(
|
|
318
|
+
this.bundle,
|
|
319
|
+
b,
|
|
320
|
+
)}");\n`;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
245
323
|
}
|
|
246
324
|
|
|
247
325
|
return (
|
|
@@ -40,21 +40,28 @@ const REPLACEMENT_RE =
|
|
|
40
40
|
/\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;
|
|
41
41
|
|
|
42
42
|
const BUILTINS = Object.keys(globals.builtin);
|
|
43
|
+
const BROWSER_BUILTINS = new Set([
|
|
44
|
+
...BUILTINS,
|
|
45
|
+
...Object.keys(globals.browser),
|
|
46
|
+
]);
|
|
47
|
+
const NODE_BUILTINS = new Set([...BUILTINS, ...Object.keys(globals.node)]);
|
|
43
48
|
const GLOBALS_BY_CONTEXT = {
|
|
44
|
-
browser:
|
|
49
|
+
browser: BROWSER_BUILTINS,
|
|
50
|
+
'react-client': BROWSER_BUILTINS,
|
|
45
51
|
'web-worker': new Set([...BUILTINS, ...Object.keys(globals.worker)]),
|
|
46
52
|
'service-worker': new Set([
|
|
47
53
|
...BUILTINS,
|
|
48
54
|
...Object.keys(globals.serviceworker),
|
|
49
55
|
]),
|
|
50
56
|
worklet: new Set([...BUILTINS]),
|
|
51
|
-
node:
|
|
52
|
-
'electron-main':
|
|
57
|
+
node: NODE_BUILTINS,
|
|
58
|
+
'electron-main': NODE_BUILTINS,
|
|
53
59
|
'electron-renderer': new Set([
|
|
54
60
|
...BUILTINS,
|
|
55
61
|
...Object.keys(globals.node),
|
|
56
62
|
...Object.keys(globals.browser),
|
|
57
63
|
]),
|
|
64
|
+
'react-server': NODE_BUILTINS,
|
|
58
65
|
};
|
|
59
66
|
|
|
60
67
|
const OUTPUT_FORMATS = {
|
|
@@ -131,12 +138,15 @@ export class ScopeHoistingPackager {
|
|
|
131
138
|
// picked up by another bundler later at which point runtimes will be added.
|
|
132
139
|
if (
|
|
133
140
|
this.bundle.env.isLibrary ||
|
|
134
|
-
this.bundle.env.outputFormat === 'commonjs'
|
|
141
|
+
this.bundle.env.outputFormat === 'commonjs' ||
|
|
142
|
+
(this.bundle.env.outputFormat === 'esmodule' && !this.isAsyncBundle)
|
|
135
143
|
) {
|
|
136
144
|
for (let b of this.bundleGraph.getReferencedBundles(this.bundle, {
|
|
137
145
|
recursive: false,
|
|
138
146
|
})) {
|
|
139
|
-
this.
|
|
147
|
+
if (this.bundle.env.isLibrary || b.type === 'js') {
|
|
148
|
+
this.externals.set(relativeBundlePath(this.bundle, b), new Map());
|
|
149
|
+
}
|
|
140
150
|
}
|
|
141
151
|
}
|
|
142
152
|
|
|
@@ -374,7 +384,11 @@ export class ScopeHoistingPackager {
|
|
|
374
384
|
// If the module has a namespace (e.g. commonjs), and this is not an entry, only export the namespace
|
|
375
385
|
// as default, without individual exports. This mirrors the importing logic in addExternal, avoiding
|
|
376
386
|
// extra unused exports and potential for non-identifier export names.
|
|
377
|
-
if (
|
|
387
|
+
if (
|
|
388
|
+
hasNamespace &&
|
|
389
|
+
!this.bundle.needsStableName &&
|
|
390
|
+
exportAs !== '*'
|
|
391
|
+
) {
|
|
378
392
|
continue;
|
|
379
393
|
}
|
|
380
394
|
|
|
@@ -502,6 +516,31 @@ export class ScopeHoistingPackager {
|
|
|
502
516
|
this.usedHelpers.add('$parcel$global');
|
|
503
517
|
}
|
|
504
518
|
|
|
519
|
+
let usedHelpers = asset.meta.usedHelpers;
|
|
520
|
+
if (typeof usedHelpers === 'number') {
|
|
521
|
+
if (usedHelpers & 1) {
|
|
522
|
+
this.usedHelpers.add('$parcel$distDir');
|
|
523
|
+
}
|
|
524
|
+
if (usedHelpers & 2) {
|
|
525
|
+
this.usedHelpers.add('$parcel$publicUrl');
|
|
526
|
+
}
|
|
527
|
+
if (usedHelpers & 4) {
|
|
528
|
+
this.needsPrelude = true;
|
|
529
|
+
this.usedHelpers.add('$parcel$import');
|
|
530
|
+
}
|
|
531
|
+
if (usedHelpers & 8) {
|
|
532
|
+
this.needsPrelude = true;
|
|
533
|
+
this.usedHelpers.add('$parcel$resolve');
|
|
534
|
+
}
|
|
535
|
+
if (usedHelpers & 16) {
|
|
536
|
+
this.needsPrelude = true;
|
|
537
|
+
this.usedHelpers.add('$parcel$extendImportMap');
|
|
538
|
+
}
|
|
539
|
+
if (usedHelpers & 32) {
|
|
540
|
+
this.usedHelpers.add('$parcel$devServer');
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
505
544
|
if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
|
|
506
545
|
const relPath = normalizeSeparators(
|
|
507
546
|
path.relative(this.bundle.target.distDir, path.dirname(asset.filePath)),
|
|
@@ -1357,7 +1396,12 @@ ${code}
|
|
|
1357
1396
|
for (let helper of this.usedHelpers) {
|
|
1358
1397
|
let currentHelper = helpers[helper];
|
|
1359
1398
|
if (typeof currentHelper === 'function') {
|
|
1360
|
-
currentHelper = helpers[helper](
|
|
1399
|
+
currentHelper = helpers[helper](
|
|
1400
|
+
this.bundle.env,
|
|
1401
|
+
this.bundle,
|
|
1402
|
+
this.usedHelpers,
|
|
1403
|
+
this.options,
|
|
1404
|
+
);
|
|
1361
1405
|
}
|
|
1362
1406
|
res += currentHelper;
|
|
1363
1407
|
if (enableSourceMaps) {
|
|
@@ -1371,7 +1415,9 @@ ${code}
|
|
|
1371
1415
|
let parentBundles = this.bundleGraph.getParentBundles(this.bundle);
|
|
1372
1416
|
let mightBeFirstJS =
|
|
1373
1417
|
parentBundles.length === 0 ||
|
|
1374
|
-
parentBundles.some(
|
|
1418
|
+
parentBundles.some(
|
|
1419
|
+
b => b.type !== 'js' || b.env.context !== this.bundle.env.context,
|
|
1420
|
+
) ||
|
|
1375
1421
|
this.bundleGraph
|
|
1376
1422
|
.getBundleGroupsContainingBundle(this.bundle)
|
|
1377
1423
|
.some(g => this.bundleGraph.isEntryBundleGroup(g)) ||
|
package/src/dev-prelude.js
CHANGED
|
@@ -6,7 +6,15 @@
|
|
|
6
6
|
// anything defined in a previous bundle is accessed via the
|
|
7
7
|
// orig method which is the require for previous bundles
|
|
8
8
|
|
|
9
|
-
(function (
|
|
9
|
+
(function (
|
|
10
|
+
modules,
|
|
11
|
+
entry,
|
|
12
|
+
mainEntry,
|
|
13
|
+
parcelRequireName,
|
|
14
|
+
distDir,
|
|
15
|
+
publicUrl,
|
|
16
|
+
devServer
|
|
17
|
+
) {
|
|
10
18
|
/* eslint-disable no-undef */
|
|
11
19
|
var globalObject =
|
|
12
20
|
typeof globalThis !== 'undefined'
|
|
@@ -25,6 +33,7 @@
|
|
|
25
33
|
typeof globalObject[parcelRequireName] === 'function' &&
|
|
26
34
|
globalObject[parcelRequireName];
|
|
27
35
|
|
|
36
|
+
var importMap = previousRequire.i || {};
|
|
28
37
|
var cache = previousRequire.cache || {};
|
|
29
38
|
// Do not use `require` to prevent Webpack from trying to bundle this call
|
|
30
39
|
var nodeRequire =
|
|
@@ -93,6 +102,7 @@
|
|
|
93
102
|
function Module(moduleName) {
|
|
94
103
|
this.id = moduleName;
|
|
95
104
|
this.bundle = newRequire;
|
|
105
|
+
this.require = nodeRequire;
|
|
96
106
|
this.exports = {};
|
|
97
107
|
}
|
|
98
108
|
|
|
@@ -101,6 +111,10 @@
|
|
|
101
111
|
newRequire.modules = modules;
|
|
102
112
|
newRequire.cache = cache;
|
|
103
113
|
newRequire.parent = previousRequire;
|
|
114
|
+
newRequire.distDir = distDir;
|
|
115
|
+
newRequire.publicUrl = publicUrl;
|
|
116
|
+
newRequire.devServer = devServer;
|
|
117
|
+
newRequire.i = importMap;
|
|
104
118
|
newRequire.register = function (id, exports) {
|
|
105
119
|
modules[id] = [
|
|
106
120
|
function (require, module) {
|
|
@@ -110,6 +124,10 @@
|
|
|
110
124
|
];
|
|
111
125
|
};
|
|
112
126
|
|
|
127
|
+
// Only insert newRequire.load when it is actually used.
|
|
128
|
+
// The code in this file is linted against ES5, so dynamic import is not allowed.
|
|
129
|
+
// INSERT_LOAD_HERE
|
|
130
|
+
|
|
113
131
|
Object.defineProperty(newRequire, 'root', {
|
|
114
132
|
get: function () {
|
|
115
133
|
return globalObject[parcelRequireName];
|
|
@@ -136,10 +154,6 @@
|
|
|
136
154
|
define(function () {
|
|
137
155
|
return mainExports;
|
|
138
156
|
});
|
|
139
|
-
|
|
140
|
-
// <script>
|
|
141
|
-
} else if (globalName) {
|
|
142
|
-
this[globalName] = mainExports;
|
|
143
157
|
}
|
|
144
158
|
}
|
|
145
159
|
});
|
package/src/helpers.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
|
-
import type {Environment} from '@parcel/types';
|
|
2
|
+
import type {Environment, NamedBundle, PluginOptions} from '@parcel/types';
|
|
3
|
+
import {relativePath} from '@parcel/utils';
|
|
4
|
+
import path from 'path';
|
|
3
5
|
|
|
4
6
|
export const prelude = (parcelRequireName: string): string => `
|
|
5
7
|
var $parcel$modules = {};
|
|
@@ -160,10 +162,151 @@ function $parcel$defineInteropFlag(a) {
|
|
|
160
162
|
}
|
|
161
163
|
`;
|
|
162
164
|
|
|
165
|
+
const $parcel$distDir = (env: Environment, bundle: NamedBundle): string => {
|
|
166
|
+
// Generate a relative path from this bundle to the root of the dist dir.
|
|
167
|
+
let distDir = relativePath(path.dirname(bundle.name), '');
|
|
168
|
+
if (!distDir.endsWith('/')) {
|
|
169
|
+
distDir += '/';
|
|
170
|
+
}
|
|
171
|
+
return `var $parcel$distDir = ${JSON.stringify(distDir)};\n`;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const $parcel$publicUrl = (env: Environment, bundle: NamedBundle): string => {
|
|
175
|
+
// Ensure the public url always ends with a slash to code can easily join paths to it.
|
|
176
|
+
let publicUrl = bundle.target.publicUrl;
|
|
177
|
+
if (!publicUrl.endsWith('/')) {
|
|
178
|
+
publicUrl += '/';
|
|
179
|
+
}
|
|
180
|
+
return `var $parcel$publicUrl = ${JSON.stringify(publicUrl)};\n`;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const $parcel$devServer = (
|
|
184
|
+
env: Environment,
|
|
185
|
+
bundle: NamedBundle,
|
|
186
|
+
_usedHelpers: Set<string>,
|
|
187
|
+
options: PluginOptions,
|
|
188
|
+
): string => {
|
|
189
|
+
if (options.hmrOptions) {
|
|
190
|
+
let {host = 'localhost', port} = options.hmrOptions;
|
|
191
|
+
let https = options.serveOptions ? options.serveOptions.https : false;
|
|
192
|
+
port = port ?? (options.serveOptions ? options.serveOptions.port : null);
|
|
193
|
+
if (port != null) {
|
|
194
|
+
let url = (https ? 'https://' : 'http://') + host + ':' + port;
|
|
195
|
+
return `var $parcel$devServer = ${JSON.stringify(url)};\n`;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return `var $parcel$devServer = null;\n`;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const $parcel$extendImportMap = (env: Environment): string => {
|
|
202
|
+
let defineImportMap = env.shouldScopeHoist
|
|
203
|
+
? 'parcelRequire.i ??= {}'
|
|
204
|
+
: 'importMap';
|
|
205
|
+
return `
|
|
206
|
+
function $parcel$extendImportMap(map) {
|
|
207
|
+
Object.assign(${defineImportMap}, map);
|
|
208
|
+
}
|
|
209
|
+
`;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const $parcel$import = (
|
|
213
|
+
env: Environment,
|
|
214
|
+
bundle: NamedBundle,
|
|
215
|
+
usedHelpers: Set<string>,
|
|
216
|
+
): string => {
|
|
217
|
+
usedHelpers.add('$parcel$distDir');
|
|
218
|
+
let distDir = env.shouldScopeHoist ? '$parcel$distDir' : 'distDir';
|
|
219
|
+
let importMap = env.shouldScopeHoist ? 'parcelRequire.i?.' : 'importMap';
|
|
220
|
+
return `
|
|
221
|
+
function $parcel$import(url) {
|
|
222
|
+
url = ${importMap}[url] || url;
|
|
223
|
+
return import(${distDir} + url);
|
|
224
|
+
}
|
|
225
|
+
`;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const $parcel$resolve = (
|
|
229
|
+
env: Environment,
|
|
230
|
+
bundle: NamedBundle,
|
|
231
|
+
usedHelpers: Set<string>,
|
|
232
|
+
): string => {
|
|
233
|
+
let distDir = env.shouldScopeHoist ? '$parcel$distDir' : 'distDir';
|
|
234
|
+
let publicUrl = env.shouldScopeHoist ? '$parcel$publicUrl' : 'publicUrl';
|
|
235
|
+
let importMap = env.shouldScopeHoist ? 'parcelRequire.i?.' : 'importMap';
|
|
236
|
+
if (env.context === 'react-server' || env.context === 'react-client') {
|
|
237
|
+
usedHelpers.add('$parcel$publicUrl');
|
|
238
|
+
return `
|
|
239
|
+
function $parcel$resolve(url) {
|
|
240
|
+
url = ${importMap}[url] || url;
|
|
241
|
+
return ${publicUrl} + url;
|
|
242
|
+
}
|
|
243
|
+
`;
|
|
244
|
+
} else if (
|
|
245
|
+
env.outputFormat === 'esmodule' &&
|
|
246
|
+
env.supports('import-meta-resolve')
|
|
247
|
+
) {
|
|
248
|
+
usedHelpers.add('$parcel$distDir');
|
|
249
|
+
return `
|
|
250
|
+
function $parcel$resolve(url) {
|
|
251
|
+
url = ${importMap}[url] || url;
|
|
252
|
+
return import.meta.resolve(${distDir} + url);
|
|
253
|
+
}
|
|
254
|
+
`;
|
|
255
|
+
} else if (
|
|
256
|
+
env.outputFormat === 'esmodule' &&
|
|
257
|
+
env.supports('import-meta-url')
|
|
258
|
+
) {
|
|
259
|
+
usedHelpers.add('$parcel$distDir');
|
|
260
|
+
return `
|
|
261
|
+
function $parcel$resolve(url) {
|
|
262
|
+
url = ${importMap}[url] || url;
|
|
263
|
+
return new URL(${distDir} + url, import.meta.url).toString();
|
|
264
|
+
}
|
|
265
|
+
`;
|
|
266
|
+
} else if (env.outputFormat === 'commonjs' || env.isNode()) {
|
|
267
|
+
usedHelpers.add('$parcel$distDir');
|
|
268
|
+
return `
|
|
269
|
+
function $parcel$resolve(url) {
|
|
270
|
+
url = ${importMap}[url] || url;
|
|
271
|
+
return new URL(${distDir} + url, 'file:' + __filename).toString();
|
|
272
|
+
}
|
|
273
|
+
`;
|
|
274
|
+
} else {
|
|
275
|
+
usedHelpers.add('$parcel$distDir');
|
|
276
|
+
return `
|
|
277
|
+
var $parcel$bundleURL;
|
|
278
|
+
function $parcel$resolve(url) {
|
|
279
|
+
url = ${importMap}[url] || url;
|
|
280
|
+
if (!$parcel$bundleURL) {
|
|
281
|
+
try {
|
|
282
|
+
throw new Error();
|
|
283
|
+
} catch (err) {
|
|
284
|
+
var matches = ('' + err.stack).match(
|
|
285
|
+
/(https?|file|ftp|(chrome|moz|safari-web)-extension):\\/\\/[^)\\n]+/g,
|
|
286
|
+
);
|
|
287
|
+
if (matches) {
|
|
288
|
+
$parcel$bundleURL = matches[0];
|
|
289
|
+
} else {
|
|
290
|
+
return ${distDir} + url;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return new URL(${distDir} + url, $parcel$bundleURL).toString();
|
|
295
|
+
}
|
|
296
|
+
`;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
|
|
163
300
|
export const helpers = {
|
|
164
301
|
$parcel$export,
|
|
165
302
|
$parcel$exportWildcard,
|
|
166
303
|
$parcel$interopDefault,
|
|
167
304
|
$parcel$global,
|
|
168
305
|
$parcel$defineInteropFlag,
|
|
306
|
+
$parcel$distDir,
|
|
307
|
+
$parcel$publicUrl,
|
|
308
|
+
$parcel$devServer,
|
|
309
|
+
$parcel$extendImportMap,
|
|
310
|
+
$parcel$import,
|
|
311
|
+
$parcel$resolve,
|
|
169
312
|
};
|
package/src/index.js
CHANGED