@parcel/packager-js 2.0.0-dev.1781 → 2.0.0-dev.1795

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.
@@ -71,7 +71,7 @@ class DevPackager {
71
71
  let prefix = this.getPrefix();
72
72
  let lineOffset = (0, _utils().countLines)(prefix);
73
73
  let script = null;
74
- let usedMetaProps = 0;
74
+ let usedHelpers = 0;
75
75
  this.bundle.traverse(node => {
76
76
  let wrapped = first ? '' : ',';
77
77
  if (node.type === 'dependency') {
@@ -88,8 +88,8 @@ class DevPackager {
88
88
  if (node.type === 'asset') {
89
89
  let asset = node.value;
90
90
  (0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
91
- if (typeof asset.meta.importMetaProps === 'number') {
92
- usedMetaProps |= asset.meta.importMetaProps;
91
+ if (typeof asset.meta.usedHelpers === 'number') {
92
+ usedHelpers |= asset.meta.usedHelpers;
93
93
  }
94
94
 
95
95
  // If this is the main entry of a script rather than a module, we need to hoist it
@@ -159,19 +159,27 @@ class DevPackager {
159
159
  });
160
160
  mainEntry = null;
161
161
  }
162
+ if (usedHelpers & 4) {
163
+ prefix = prefix.replace('// INSERT_LOAD_HERE', 'newRequire.load = function (url) { return import(distDir + "/" + url); }');
164
+ }
162
165
  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);
163
- if (usedMetaProps & 1) {
166
+ if (usedHelpers & 1) {
164
167
  // Generate a relative path from this bundle to the root of the dist dir.
165
168
  let distDir = (0, _utils().relativePath)(_path().default.dirname(this.bundle.name), '');
166
169
  if (distDir.endsWith('/')) {
167
170
  distDir = distDir.slice(0, -1);
168
171
  }
169
172
  contents += ', ' + JSON.stringify(distDir);
170
- } else if (usedMetaProps & 2) {
173
+ } else if (usedHelpers & 2) {
171
174
  contents += ', null';
172
175
  }
173
- if (usedMetaProps & 2) {
174
- contents += ', ' + JSON.stringify(this.bundle.target.publicUrl);
176
+ if (usedHelpers & 2) {
177
+ // Ensure the public url always ends with a slash to code can easily join paths to it.
178
+ let publicUrl = this.bundle.target.publicUrl;
179
+ if (!publicUrl.endsWith('/')) {
180
+ publicUrl += '/';
181
+ }
182
+ contents += ', ' + JSON.stringify(publicUrl);
175
183
  }
176
184
  contents += ')\n';
177
185
 
@@ -393,14 +393,17 @@ class ScopeHoistingPackager {
393
393
  if (code.includes('$parcel$global')) {
394
394
  this.usedHelpers.add('$parcel$global');
395
395
  }
396
- let importMetaProps = asset.meta.importMetaProps;
397
- if (typeof importMetaProps === 'number') {
398
- if (importMetaProps & 1) {
396
+ let usedHelpers = asset.meta.usedHelpers;
397
+ if (typeof usedHelpers === 'number') {
398
+ if (usedHelpers & 1) {
399
399
  this.usedHelpers.add('$parcel$distDir');
400
400
  }
401
- if (importMetaProps & 2) {
401
+ if (usedHelpers & 2) {
402
402
  this.usedHelpers.add('$parcel$publicUrl');
403
403
  }
404
+ if (usedHelpers & 4) {
405
+ this.usedHelpers.add('$parcel$import');
406
+ }
404
407
  }
405
408
  if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
406
409
  const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
@@ -1008,7 +1011,7 @@ ${code}
1008
1011
  // Add the prelude if this is potentially the first JS bundle to load in a
1009
1012
  // particular context (e.g. entry scripts in HTML, workers, etc.).
1010
1013
  let parentBundles = this.bundleGraph.getParentBundles(this.bundle);
1011
- 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';
1014
+ 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';
1012
1015
  if (mightBeFirstJS) {
1013
1016
  let preludeCode = (0, _helpers.prelude)(this.parcelRequireName);
1014
1017
  res += preludeCode;
@@ -112,6 +112,10 @@
112
112
  ];
113
113
  };
114
114
 
115
+ // Only insert newRequire.load when it is actually used.
116
+ // The code in this file is linted against ES5, so dynamic import is not allowed.
117
+ // INSERT_LOAD_HERE
118
+
115
119
  Object.defineProperty(newRequire, 'root', {
116
120
  get: function () {
117
121
  return globalObject[parcelRequireName];
package/lib/helpers.js CHANGED
@@ -141,10 +141,18 @@ const $parcel$distDir = (env, bundle) => {
141
141
  if (distDir.endsWith('/')) {
142
142
  distDir = distDir.slice(0, -1);
143
143
  }
144
- return `const $parcel$distDir = ${JSON.stringify(distDir)};\n`;
144
+ return `var $parcel$distDir = ${JSON.stringify(distDir)};\n`;
145
145
  };
146
146
  const $parcel$publicUrl = (env, bundle) => {
147
- return `const $parcel$publicUrl = ${JSON.stringify(bundle.target.publicUrl)};\n`;
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$import = env => {
155
+ return `var $parcel$import = ${fnExpr(env, ['url'], ['return import($parcel$distDir + "/" + url);'])};\n`;
148
156
  };
149
157
  const helpers = exports.helpers = {
150
158
  $parcel$export,
@@ -153,5 +161,6 @@ const helpers = exports.helpers = {
153
161
  $parcel$global,
154
162
  $parcel$defineInteropFlag,
155
163
  $parcel$distDir,
156
- $parcel$publicUrl
164
+ $parcel$publicUrl,
165
+ $parcel$import
157
166
  };
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 _packageName$contents, _conf$contents;
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 || (_packageName$contents = packageName.contents) === null || _packageName$contents === void 0 ? void 0 : _packageName$contents.name) ?? '';
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.0.0-dev.1781+61f02c2f3",
3
+ "version": "2.0.0-dev.1795+9f297b15c",
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.0.0-dev.1779+61f02c2f3"
20
+ "parcel": "^2.0.0-dev.1793+9f297b15c"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "2.0.0-dev.1781+61f02c2f3",
24
- "@parcel/plugin": "2.0.0-dev.1781+61f02c2f3",
25
- "@parcel/rust": "2.13.3-dev.3404+61f02c2f3",
23
+ "@parcel/diagnostic": "2.0.0-dev.1795+9f297b15c",
24
+ "@parcel/plugin": "2.0.0-dev.1795+9f297b15c",
25
+ "@parcel/rust": "2.13.3-dev.3418+9f297b15c",
26
26
  "@parcel/source-map": "^2.1.1",
27
- "@parcel/types": "2.0.0-dev.1781+61f02c2f3",
28
- "@parcel/utils": "2.0.0-dev.1781+61f02c2f3",
27
+ "@parcel/types": "2.0.0-dev.1795+9f297b15c",
28
+ "@parcel/utils": "2.0.0-dev.1795+9f297b15c",
29
29
  "globals": "^13.2.0",
30
30
  "nullthrows": "^1.1.1"
31
31
  },
32
- "gitHead": "61f02c2f339013deb2ce38ff59edbe15d62e7cb5"
32
+ "gitHead": "9f297b15c3cc3b74136232040b3149f0baae0060"
33
33
  }
@@ -61,7 +61,7 @@ export class DevPackager {
61
61
  let lineOffset = countLines(prefix);
62
62
  let script: ?{|code: string, mapBuffer: ?Buffer|} = null;
63
63
 
64
- let usedMetaProps = 0;
64
+ let usedHelpers = 0;
65
65
  this.bundle.traverse(node => {
66
66
  let wrapped = first ? '' : ',';
67
67
 
@@ -89,8 +89,8 @@ export class DevPackager {
89
89
  'all assets in a js bundle must be js assets',
90
90
  );
91
91
 
92
- if (typeof asset.meta.importMetaProps === 'number') {
93
- usedMetaProps |= asset.meta.importMetaProps;
92
+ if (typeof asset.meta.usedHelpers === 'number') {
93
+ usedHelpers |= asset.meta.usedHelpers;
94
94
  }
95
95
 
96
96
  // If this is the main entry of a script rather than a module, we need to hoist it
@@ -186,6 +186,13 @@ export class DevPackager {
186
186
  mainEntry = null;
187
187
  }
188
188
 
189
+ if (usedHelpers & 4) {
190
+ prefix = prefix.replace(
191
+ '// INSERT_LOAD_HERE',
192
+ 'newRequire.load = function (url) { return import(distDir + "/" + url); }',
193
+ );
194
+ }
195
+
189
196
  let contents =
190
197
  prefix +
191
198
  '({' +
@@ -201,19 +208,24 @@ export class DevPackager {
201
208
  ', ' +
202
209
  JSON.stringify(this.parcelRequireName);
203
210
 
204
- if (usedMetaProps & 1) {
211
+ if (usedHelpers & 1) {
205
212
  // Generate a relative path from this bundle to the root of the dist dir.
206
213
  let distDir = relativePath(path.dirname(this.bundle.name), '');
207
214
  if (distDir.endsWith('/')) {
208
215
  distDir = distDir.slice(0, -1);
209
216
  }
210
217
  contents += ', ' + JSON.stringify(distDir);
211
- } else if (usedMetaProps & 2) {
218
+ } else if (usedHelpers & 2) {
212
219
  contents += ', null';
213
220
  }
214
221
 
215
- if (usedMetaProps & 2) {
216
- contents += ', ' + JSON.stringify(this.bundle.target.publicUrl);
222
+ if (usedHelpers & 2) {
223
+ // Ensure the public url always ends with a slash to code can easily join paths to it.
224
+ let publicUrl = this.bundle.target.publicUrl;
225
+ if (!publicUrl.endsWith('/')) {
226
+ publicUrl += '/';
227
+ }
228
+ contents += ', ' + JSON.stringify(publicUrl);
217
229
  }
218
230
 
219
231
  contents += ')\n';
@@ -516,14 +516,17 @@ export class ScopeHoistingPackager {
516
516
  this.usedHelpers.add('$parcel$global');
517
517
  }
518
518
 
519
- let importMetaProps = asset.meta.importMetaProps;
520
- if (typeof importMetaProps === 'number') {
521
- if (importMetaProps & 1) {
519
+ let usedHelpers = asset.meta.usedHelpers;
520
+ if (typeof usedHelpers === 'number') {
521
+ if (usedHelpers & 1) {
522
522
  this.usedHelpers.add('$parcel$distDir');
523
523
  }
524
- if (importMetaProps & 2) {
524
+ if (usedHelpers & 2) {
525
525
  this.usedHelpers.add('$parcel$publicUrl');
526
526
  }
527
+ if (usedHelpers & 4) {
528
+ this.usedHelpers.add('$parcel$import');
529
+ }
527
530
  }
528
531
 
529
532
  if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
@@ -1395,7 +1398,9 @@ ${code}
1395
1398
  let parentBundles = this.bundleGraph.getParentBundles(this.bundle);
1396
1399
  let mightBeFirstJS =
1397
1400
  parentBundles.length === 0 ||
1398
- parentBundles.some(b => b.type !== 'js') ||
1401
+ parentBundles.some(
1402
+ b => b.type !== 'js' || b.env.context !== this.bundle.env.context,
1403
+ ) ||
1399
1404
  this.bundleGraph
1400
1405
  .getBundleGroupsContainingBundle(this.bundle)
1401
1406
  .some(g => this.bundleGraph.isEntryBundleGroup(g)) ||
@@ -112,6 +112,10 @@
112
112
  ];
113
113
  };
114
114
 
115
+ // Only insert newRequire.load when it is actually used.
116
+ // The code in this file is linted against ES5, so dynamic import is not allowed.
117
+ // INSERT_LOAD_HERE
118
+
115
119
  Object.defineProperty(newRequire, 'root', {
116
120
  get: function () {
117
121
  return globalObject[parcelRequireName];
package/src/helpers.js CHANGED
@@ -168,12 +168,23 @@ const $parcel$distDir = (env: Environment, bundle: NamedBundle): string => {
168
168
  if (distDir.endsWith('/')) {
169
169
  distDir = distDir.slice(0, -1);
170
170
  }
171
- return `const $parcel$distDir = ${JSON.stringify(distDir)};\n`;
171
+ return `var $parcel$distDir = ${JSON.stringify(distDir)};\n`;
172
172
  };
173
173
 
174
174
  const $parcel$publicUrl = (env: Environment, bundle: NamedBundle): string => {
175
- return `const $parcel$publicUrl = ${JSON.stringify(
176
- bundle.target.publicUrl,
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$import = (env: Environment): string => {
184
+ return `var $parcel$import = ${fnExpr(
185
+ env,
186
+ ['url'],
187
+ ['return import($parcel$distDir + "/" + url);'],
177
188
  )};\n`;
178
189
  };
179
190
 
@@ -185,4 +196,5 @@ export const helpers = {
185
196
  $parcel$defineInteropFlag,
186
197
  $parcel$distDir,
187
198
  $parcel$publicUrl,
199
+ $parcel$import,
188
200
  };
package/src/index.js CHANGED
@@ -60,7 +60,7 @@ export default (new Packager({
60
60
  },
61
61
  );
62
62
 
63
- let name = packageName?.contents?.name ?? '';
63
+ let name = packageName?.contents ?? '';
64
64
  return {
65
65
  parcelRequireName: 'parcelRequire' + hashString(name).slice(-4),
66
66
  unstable_asyncBundleRuntime: Boolean(