@parcel/packager-js 2.14.2 → 2.14.4

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.
@@ -72,6 +72,7 @@ class DevPackager {
72
72
  let prefix = this.getPrefix();
73
73
  let lineOffset = (0, _utils().countLines)(prefix);
74
74
  let script = null;
75
+ let externals = new Set();
75
76
  let usedHelpers = 0;
76
77
  this.bundle.traverse(node => {
77
78
  let wrapped = first ? '' : ',';
@@ -111,6 +112,9 @@ class DevPackager {
111
112
  } else {
112
113
  // An external module - map placeholder to original specifier.
113
114
  deps[specifier] = dep.specifier;
115
+ if (this.bundle.env.outputFormat === 'esmodule') {
116
+ externals.add(dep.specifier);
117
+ }
114
118
  }
115
119
  }
116
120
 
@@ -178,7 +182,16 @@ class DevPackager {
178
182
  // Remove newlines to avoid messing up source maps
179
183
  prefix = prefix.replace('// INSERT_LOAD_HERE', load.replace(/\n/g, ''));
180
184
  }
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);
185
+ let externalImports = '';
186
+ let externalMap = '{';
187
+ let e = 0;
188
+ for (let external of externals) {
189
+ let name = `__parcelExternal${e++}`;
190
+ externalImports += `import * as ${name} from ${JSON.stringify(external)};\n`;
191
+ externalMap += `${JSON.stringify(external)}: ${name},`;
192
+ }
193
+ externalMap += '}';
194
+ let contents = externalImports + prefix + '({' + assets + '},' + JSON.stringify(entries.map(asset => this.bundleGraph.getAssetPublicId(asset))) + ', ' + JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) + ', ' + JSON.stringify(this.parcelRequireName) + ', ' + externalMap;
182
195
  if (usedHelpers & 1) {
183
196
  // Generate a relative path from this bundle to the root of the dist dir.
184
197
  let distDir = (0, _utils().relativePath)(_path().default.dirname(this.bundle.name), '');
@@ -205,6 +218,53 @@ class DevPackager {
205
218
  }
206
219
  contents += ')\n';
207
220
 
221
+ // Add ES module exports from the entry asset.
222
+ if (this.bundle.env.outputFormat === 'esmodule' && mainEntry && (this.bundle.env.isLibrary || !this.bundle.env.isBrowser())) {
223
+ let hasNamespace = mainEntry.symbols.hasExportSymbol('*');
224
+ let importedSymbols = new Map();
225
+ let exportedSymbols = new Map();
226
+ for (let {
227
+ exportAs,
228
+ symbol,
229
+ exportSymbol
230
+ } of this.bundleGraph.getExportedSymbols(mainEntry)) {
231
+ if (typeof symbol === 'string') {
232
+ if (hasNamespace && exportAs !== '*') {
233
+ continue;
234
+ }
235
+ if (exportAs === '*') {
236
+ exportAs = 'default';
237
+ }
238
+ let id = (0, _utils2.makeValidIdentifier)(exportSymbol);
239
+ if (id === 'default') {
240
+ id = '_default';
241
+ }
242
+ importedSymbols.set(exportSymbol, id);
243
+ exportedSymbols.set(exportAs, id);
244
+ }
245
+ }
246
+ contents += 'let {';
247
+ for (let [key, value] of importedSymbols) {
248
+ contents += (0, _utils2.isValidIdentifier)(key) ? key : JSON.stringify(key);
249
+ if (value !== key) {
250
+ contents += ': ';
251
+ contents += value;
252
+ }
253
+ contents += ', ';
254
+ }
255
+ contents += '} = ' + this.parcelRequireName + '(' + JSON.stringify(this.bundleGraph.getAssetPublicId(mainEntry)) + ');\n';
256
+ contents += 'export {';
257
+ for (let [exportAs, ident] of exportedSymbols) {
258
+ contents += ident;
259
+ if (exportAs !== ident) {
260
+ contents += ' as ';
261
+ contents += (0, _utils2.isValidIdentifier)(exportAs) ? exportAs : JSON.stringify(exportAs);
262
+ }
263
+ contents += ', ';
264
+ }
265
+ contents += '};\n';
266
+ }
267
+
208
268
  // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
209
269
  // so that its variables become globals. We need to replace any require calls for
210
270
  // runtimes with a parcelRequire call.
@@ -273,7 +273,7 @@ class ScopeHoistingPackager {
273
273
  return wrapped;
274
274
  }
275
275
  buildExportedSymbols() {
276
- if (!this.bundle.env.isLibrary || this.bundle.env.outputFormat !== 'esmodule') {
276
+ if (!this.bundle.env.isLibrary && this.bundle.env.isBrowser() || this.bundle.env.outputFormat !== 'esmodule') {
277
277
  return;
278
278
  }
279
279
 
@@ -11,6 +11,7 @@
11
11
  entry,
12
12
  mainEntry,
13
13
  parcelRequireName,
14
+ externals,
14
15
  distDir,
15
16
  publicUrl,
16
17
  devServer
@@ -44,6 +45,9 @@
44
45
  function newRequire(name, jumped) {
45
46
  if (!cache[name]) {
46
47
  if (!modules[name]) {
48
+ if (externals[name]) {
49
+ return externals[name];
50
+ }
47
51
  // if we cannot find the module within our internal map or
48
52
  // cache jump to the current global require ie. the last bundle
49
53
  // that was added to the page.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.14.2",
3
+ "version": "2.14.4",
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.14.2"
20
+ "parcel": "^2.14.4"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "2.14.2",
24
- "@parcel/plugin": "2.14.2",
25
- "@parcel/rust": "2.14.2",
23
+ "@parcel/diagnostic": "2.14.4",
24
+ "@parcel/plugin": "2.14.4",
25
+ "@parcel/rust": "2.14.4",
26
26
  "@parcel/source-map": "^2.1.1",
27
- "@parcel/types": "2.14.2",
28
- "@parcel/utils": "2.14.2",
27
+ "@parcel/types": "2.14.4",
28
+ "@parcel/utils": "2.14.4",
29
29
  "globals": "^13.2.0",
30
30
  "nullthrows": "^1.1.1"
31
31
  },
32
- "gitHead": "5fc9ddce4557abd007442154cb1c42d55e5cf4fc"
32
+ "gitHead": "c09fee751b61e2f9d52164f88248b9c668614134"
33
33
  }
@@ -12,7 +12,12 @@ import SourceMap from '@parcel/source-map';
12
12
  import invariant from 'assert';
13
13
  import path from 'path';
14
14
  import fs from 'fs';
15
- import {replaceScriptDependencies, getSpecifier} from './utils';
15
+ import {
16
+ replaceScriptDependencies,
17
+ getSpecifier,
18
+ makeValidIdentifier,
19
+ isValidIdentifier,
20
+ } from './utils';
16
21
  import {helpers} from './helpers';
17
22
 
18
23
  const PRELUDE = fs
@@ -61,6 +66,7 @@ export class DevPackager {
61
66
  let prefix = this.getPrefix();
62
67
  let lineOffset = countLines(prefix);
63
68
  let script: ?{|code: string, mapBuffer: ?Buffer|} = null;
69
+ let externals = new Set();
64
70
 
65
71
  let usedHelpers = 0;
66
72
  this.bundle.traverse(node => {
@@ -116,6 +122,9 @@ export class DevPackager {
116
122
  } else {
117
123
  // An external module - map placeholder to original specifier.
118
124
  deps[specifier] = dep.specifier;
125
+ if (this.bundle.env.outputFormat === 'esmodule') {
126
+ externals.add(dep.specifier);
127
+ }
119
128
  }
120
129
  }
121
130
 
@@ -209,7 +218,20 @@ export class DevPackager {
209
218
  prefix = prefix.replace('// INSERT_LOAD_HERE', load.replace(/\n/g, ''));
210
219
  }
211
220
 
221
+ let externalImports = '';
222
+ let externalMap = '{';
223
+ let e = 0;
224
+ for (let external of externals) {
225
+ let name = `__parcelExternal${e++}`;
226
+ externalImports += `import * as ${name} from ${JSON.stringify(
227
+ external,
228
+ )};\n`;
229
+ externalMap += `${JSON.stringify(external)}: ${name},`;
230
+ }
231
+ externalMap += '}';
232
+
212
233
  let contents =
234
+ externalImports +
213
235
  prefix +
214
236
  '({' +
215
237
  assets +
@@ -222,7 +244,9 @@ export class DevPackager {
222
244
  mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null,
223
245
  ) +
224
246
  ', ' +
225
- JSON.stringify(this.parcelRequireName);
247
+ JSON.stringify(this.parcelRequireName) +
248
+ ', ' +
249
+ externalMap;
226
250
 
227
251
  if (usedHelpers & 1) {
228
252
  // Generate a relative path from this bundle to the root of the dist dir.
@@ -258,6 +282,70 @@ export class DevPackager {
258
282
 
259
283
  contents += ')\n';
260
284
 
285
+ // Add ES module exports from the entry asset.
286
+ if (
287
+ this.bundle.env.outputFormat === 'esmodule' &&
288
+ mainEntry &&
289
+ (this.bundle.env.isLibrary || !this.bundle.env.isBrowser())
290
+ ) {
291
+ let hasNamespace = mainEntry.symbols.hasExportSymbol('*');
292
+ let importedSymbols = new Map();
293
+ let exportedSymbols = new Map();
294
+ for (let {
295
+ exportAs,
296
+ symbol,
297
+ exportSymbol,
298
+ } of this.bundleGraph.getExportedSymbols(mainEntry)) {
299
+ if (typeof symbol === 'string') {
300
+ if (hasNamespace && exportAs !== '*') {
301
+ continue;
302
+ }
303
+
304
+ if (exportAs === '*') {
305
+ exportAs = 'default';
306
+ }
307
+
308
+ let id = makeValidIdentifier(exportSymbol);
309
+ if (id === 'default') {
310
+ id = '_default';
311
+ }
312
+ importedSymbols.set(exportSymbol, id);
313
+ exportedSymbols.set(exportAs, id);
314
+ }
315
+ }
316
+
317
+ contents += 'let {';
318
+ for (let [key, value] of importedSymbols) {
319
+ contents += isValidIdentifier(key) ? key : JSON.stringify(key);
320
+ if (value !== key) {
321
+ contents += ': ';
322
+ contents += value;
323
+ }
324
+ contents += ', ';
325
+ }
326
+
327
+ contents +=
328
+ '} = ' +
329
+ this.parcelRequireName +
330
+ '(' +
331
+ JSON.stringify(this.bundleGraph.getAssetPublicId(mainEntry)) +
332
+ ');\n';
333
+ contents += 'export {';
334
+
335
+ for (let [exportAs, ident] of exportedSymbols) {
336
+ contents += ident;
337
+ if (exportAs !== ident) {
338
+ contents += ' as ';
339
+ contents += isValidIdentifier(exportAs)
340
+ ? exportAs
341
+ : JSON.stringify(exportAs);
342
+ }
343
+ contents += ', ';
344
+ }
345
+
346
+ contents += '};\n';
347
+ }
348
+
261
349
  // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
262
350
  // so that its variables become globals. We need to replace any require calls for
263
351
  // runtimes with a parcelRequire call.
@@ -363,7 +363,7 @@ export class ScopeHoistingPackager {
363
363
 
364
364
  buildExportedSymbols() {
365
365
  if (
366
- !this.bundle.env.isLibrary ||
366
+ (!this.bundle.env.isLibrary && this.bundle.env.isBrowser()) ||
367
367
  this.bundle.env.outputFormat !== 'esmodule'
368
368
  ) {
369
369
  return;
@@ -11,6 +11,7 @@
11
11
  entry,
12
12
  mainEntry,
13
13
  parcelRequireName,
14
+ externals,
14
15
  distDir,
15
16
  publicUrl,
16
17
  devServer
@@ -44,6 +45,9 @@
44
45
  function newRequire(name, jumped) {
45
46
  if (!cache[name]) {
46
47
  if (!modules[name]) {
48
+ if (externals[name]) {
49
+ return externals[name];
50
+ }
47
51
  // if we cannot find the module within our internal map or
48
52
  // cache jump to the current global require ie. the last bundle
49
53
  // that was added to the page.