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

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,18 +159,21 @@ 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) {
176
+ if (usedHelpers & 2) {
174
177
  contents += ', ' + JSON.stringify(this.bundle.target.publicUrl);
175
178
  }
176
179
  contents += ')\n';
@@ -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)));
@@ -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,13 @@ 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
+ return `var $parcel$publicUrl = ${JSON.stringify(bundle.target.publicUrl)};\n`;
148
+ };
149
+ const $parcel$import = env => {
150
+ return `var $parcel$import = ${fnExpr(env, ['url'], ['return import($parcel$distDir + "/" + url);'])};\n`;
148
151
  };
149
152
  const helpers = exports.helpers = {
150
153
  $parcel$export,
@@ -153,5 +156,6 @@ const helpers = exports.helpers = {
153
156
  $parcel$global,
154
157
  $parcel$defineInteropFlag,
155
158
  $parcel$distDir,
156
- $parcel$publicUrl
159
+ $parcel$publicUrl,
160
+ $parcel$import
157
161
  };
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.1789+0b82b13d6",
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.1787+0b82b13d6"
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.1789+0b82b13d6",
24
+ "@parcel/plugin": "2.0.0-dev.1789+0b82b13d6",
25
+ "@parcel/rust": "2.13.3-dev.3412+0b82b13d6",
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.1789+0b82b13d6",
28
+ "@parcel/utils": "2.0.0-dev.1789+0b82b13d6",
29
29
  "globals": "^13.2.0",
30
30
  "nullthrows": "^1.1.1"
31
31
  },
32
- "gitHead": "61f02c2f339013deb2ce38ff59edbe15d62e7cb5"
32
+ "gitHead": "0b82b13d6c0dae58ae636983bbd57dbcf94fff89"
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,18 +208,18 @@ 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) {
222
+ if (usedHelpers & 2) {
216
223
  contents += ', ' + JSON.stringify(this.bundle.target.publicUrl);
217
224
  }
218
225
 
@@ -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) {
@@ -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,15 +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(
175
+ return `var $parcel$publicUrl = ${JSON.stringify(
176
176
  bundle.target.publicUrl,
177
177
  )};\n`;
178
178
  };
179
179
 
180
+ const $parcel$import = (env: Environment): string => {
181
+ return `var $parcel$import = ${fnExpr(
182
+ env,
183
+ ['url'],
184
+ ['return import($parcel$distDir + "/" + url);'],
185
+ )};\n`;
186
+ };
187
+
180
188
  export const helpers = {
181
189
  $parcel$export,
182
190
  $parcel$exportWildcard,
@@ -185,4 +193,5 @@ export const helpers = {
185
193
  $parcel$defineInteropFlag,
186
194
  $parcel$distDir,
187
195
  $parcel$publicUrl,
196
+ $parcel$import,
188
197
  };