@parcel/packager-js 2.0.0 → 2.2.0

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.
@@ -125,7 +125,9 @@ class DevPackager {
125
125
  for (let dep of dependencies) {
126
126
  let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
127
127
 
128
- if (resolved) {
128
+ if (this.bundleGraph.isDependencySkipped(dep)) {
129
+ deps[(0, _utils2.getSpecifier)(dep)] = false;
130
+ } else if (resolved) {
129
131
  deps[(0, _utils2.getSpecifier)(dep)] = this.bundleGraph.getAssetPublicId(resolved);
130
132
  }
131
133
  }
@@ -124,7 +124,7 @@ class ScopeHoistingPackager {
124
124
  async package() {
125
125
  var _sourceMap;
126
126
 
127
- await this.loadAssets();
127
+ let wrappedAssets = await this.loadAssets();
128
128
  this.buildExportedSymbols(); // If building a library, the target is actually another bundler rather
129
129
  // than the final output that could be loaded in a browser. So, loader
130
130
  // runtimes are excluded, and instead we add imports into the entry bundle
@@ -137,19 +137,13 @@ class ScopeHoistingPackager {
137
137
  for (let b of bundles) {
138
138
  this.externals.set((0, _utils().relativeBundlePath)(this.bundle, b), new Map());
139
139
  }
140
- } // Add each asset that is directly connected to the bundle. Dependencies will be handled
141
- // by replacing `import` statements in the code.
142
-
140
+ }
143
141
 
144
142
  let res = '';
145
143
  let lineCount = 0;
146
144
  let sourceMap = null;
147
- this.bundle.traverseAssets((asset, _, actions) => {
148
- if (this.seenAssets.has(asset.id)) {
149
- actions.skipChildren();
150
- return;
151
- }
152
145
 
146
+ let processAsset = asset => {
153
147
  let [content, map, lines] = this.visitAsset(asset);
154
148
 
155
149
  if (sourceMap && map) {
@@ -160,6 +154,25 @@ class ScopeHoistingPackager {
160
154
 
161
155
  res += content + '\n';
162
156
  lineCount += lines + 1;
157
+ }; // Hoist wrapped asset to the top of the bundle to ensure that they are registered
158
+ // before they are used.
159
+
160
+
161
+ for (let asset of wrappedAssets) {
162
+ if (!this.seenAssets.has(asset.id)) {
163
+ processAsset(asset);
164
+ }
165
+ } // Add each asset that is directly connected to the bundle. Dependencies will be handled
166
+ // by replacing `import` statements in the code.
167
+
168
+
169
+ this.bundle.traverseAssets((asset, _, actions) => {
170
+ if (this.seenAssets.has(asset.id)) {
171
+ actions.skipChildren();
172
+ return;
173
+ }
174
+
175
+ processAsset(asset);
163
176
  actions.skipChildren();
164
177
  });
165
178
  let [prelude, preludeLines] = this.buildBundlePrelude();
@@ -235,6 +248,7 @@ class ScopeHoistingPackager {
235
248
  let queue = new (_utils().PromiseQueue)({
236
249
  maxConcurrent: 32
237
250
  });
251
+ let wrapped = [];
238
252
  this.bundle.traverseAssets((asset, shouldWrap) => {
239
253
  queue.add(async () => {
240
254
  let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
@@ -246,10 +260,12 @@ class ScopeHoistingPackager {
246
260
 
247
261
  if (shouldWrap || 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')) {
248
262
  this.wrappedAssets.add(asset.id);
263
+ wrapped.push(asset);
249
264
  return true;
250
265
  }
251
266
  });
252
267
  this.assetOutputs = new Map(await queue.run());
268
+ return wrapped;
253
269
  }
254
270
 
255
271
  buildExportedSymbols() {
@@ -555,7 +571,7 @@ ${code}
555
571
  if (imported === '*') {
556
572
  replacement = renamed;
557
573
  } else if (imported === 'default') {
558
- replacement = `$parcel$interopDefault(${renamed})`;
574
+ replacement = `($parcel$interopDefault(${renamed}))`;
559
575
  this.usedHelpers.add('$parcel$interopDefault');
560
576
  } else {
561
577
  replacement = this.getPropertyAccess(renamed, imported);
@@ -6,7 +6,7 @@
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(modules, entry, mainEntry, parcelRequireName, globalName) {
9
+ (function (modules, entry, mainEntry, parcelRequireName, globalName) {
10
10
  /* eslint-disable no-undef */
11
11
  var globalObject =
12
12
  typeof globalThis !== 'undefined'
@@ -80,11 +80,13 @@
80
80
  return cache[name].exports;
81
81
 
82
82
  function localRequire(x) {
83
- return newRequire(localRequire.resolve(x));
83
+ var res = localRequire.resolve(x);
84
+ return res === false ? {} : newRequire(res);
84
85
  }
85
86
 
86
87
  function resolve(x) {
87
- return modules[name][1][x] || x;
88
+ var id = modules[name][1][x];
89
+ return id != null ? id : x;
88
90
  }
89
91
  }
90
92
 
@@ -99,9 +101,9 @@
99
101
  newRequire.modules = modules;
100
102
  newRequire.cache = cache;
101
103
  newRequire.parent = previousRequire;
102
- newRequire.register = function(id, exports) {
104
+ newRequire.register = function (id, exports) {
103
105
  modules[id] = [
104
- function(require, module) {
106
+ function (require, module) {
105
107
  module.exports = exports;
106
108
  },
107
109
  {},
@@ -109,7 +111,7 @@
109
111
  };
110
112
 
111
113
  Object.defineProperty(newRequire, 'root', {
112
- get: function() {
114
+ get: function () {
113
115
  return globalObject[parcelRequireName];
114
116
  },
115
117
  });
@@ -131,7 +133,7 @@
131
133
 
132
134
  // RequireJS
133
135
  } else if (typeof define === 'function' && define.amd) {
134
- define(function() {
136
+ define(function () {
135
137
  return mainExports;
136
138
  });
137
139
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,16 +17,16 @@
17
17
  "source": "src/index.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0"
20
+ "parcel": "^2.2.0"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "^2.0.0",
24
- "@parcel/hash": "^2.0.0",
25
- "@parcel/plugin": "^2.0.0",
23
+ "@parcel/diagnostic": "^2.2.0",
24
+ "@parcel/hash": "^2.2.0",
25
+ "@parcel/plugin": "^2.2.0",
26
26
  "@parcel/source-map": "^2.0.0",
27
- "@parcel/utils": "^2.0.0",
27
+ "@parcel/utils": "^2.2.0",
28
28
  "globals": "^13.2.0",
29
29
  "nullthrows": "^1.1.1"
30
30
  },
31
- "gitHead": "84da50ae6943bff9741e2fc76d2f4968074cbbd6"
31
+ "gitHead": "4745cd3023f8d5a5adcf9e565d5b82d1418dc262"
32
32
  }
@@ -98,10 +98,11 @@ export class DevPackager {
98
98
  let dependencies = this.bundleGraph.getDependencies(asset);
99
99
  for (let dep of dependencies) {
100
100
  let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
101
- if (resolved) {
102
- deps[getSpecifier(dep)] = this.bundleGraph.getAssetPublicId(
103
- resolved,
104
- );
101
+ if (this.bundleGraph.isDependencySkipped(dep)) {
102
+ deps[getSpecifier(dep)] = false;
103
+ } else if (resolved) {
104
+ deps[getSpecifier(dep)] =
105
+ this.bundleGraph.getAssetPublicId(resolved);
105
106
  }
106
107
  }
107
108
 
@@ -28,7 +28,8 @@ const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
28
28
 
29
29
  // General regex used to replace imports with the resolved code, references with resolutions,
30
30
  // and count the number of newlines in the file for source maps.
31
- 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;
31
+ const REPLACEMENT_RE =
32
+ /\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;
32
33
 
33
34
  const BUILTINS = Object.keys(globals.builtin);
34
35
  const GLOBALS_BY_CONTEXT = {
@@ -108,7 +109,7 @@ export class ScopeHoistingPackager {
108
109
  }
109
110
 
110
111
  async package(): Promise<{|contents: string, map: ?SourceMap|}> {
111
- await this.loadAssets();
112
+ let wrappedAssets = await this.loadAssets();
112
113
  this.buildExportedSymbols();
113
114
 
114
115
  // If building a library, the target is actually another bundler rather
@@ -126,17 +127,10 @@ export class ScopeHoistingPackager {
126
127
  }
127
128
  }
128
129
 
129
- // Add each asset that is directly connected to the bundle. Dependencies will be handled
130
- // by replacing `import` statements in the code.
131
130
  let res = '';
132
131
  let lineCount = 0;
133
132
  let sourceMap = null;
134
- this.bundle.traverseAssets((asset, _, actions) => {
135
- if (this.seenAssets.has(asset.id)) {
136
- actions.skipChildren();
137
- return;
138
- }
139
-
133
+ let processAsset = asset => {
140
134
  let [content, map, lines] = this.visitAsset(asset);
141
135
  if (sourceMap && map) {
142
136
  sourceMap.addSourceMap(map, lineCount);
@@ -146,6 +140,25 @@ export class ScopeHoistingPackager {
146
140
 
147
141
  res += content + '\n';
148
142
  lineCount += lines + 1;
143
+ };
144
+
145
+ // Hoist wrapped asset to the top of the bundle to ensure that they are registered
146
+ // before they are used.
147
+ for (let asset of wrappedAssets) {
148
+ if (!this.seenAssets.has(asset.id)) {
149
+ processAsset(asset);
150
+ }
151
+ }
152
+
153
+ // Add each asset that is directly connected to the bundle. Dependencies will be handled
154
+ // by replacing `import` statements in the code.
155
+ this.bundle.traverseAssets((asset, _, actions) => {
156
+ if (this.seenAssets.has(asset.id)) {
157
+ actions.skipChildren();
158
+ return;
159
+ }
160
+
161
+ processAsset(asset);
149
162
  actions.skipChildren();
150
163
  });
151
164
 
@@ -225,8 +238,9 @@ export class ScopeHoistingPackager {
225
238
  };
226
239
  }
227
240
 
228
- async loadAssets() {
241
+ async loadAssets(): Promise<Array<Asset>> {
229
242
  let queue = new PromiseQueue({maxConcurrent: 32});
243
+ let wrapped = [];
230
244
  this.bundle.traverseAssets((asset, shouldWrap) => {
231
245
  queue.add(async () => {
232
246
  let [code, map] = await Promise.all([
@@ -247,11 +261,13 @@ export class ScopeHoistingPackager {
247
261
  .some(dep => dep.meta.shouldWrap && dep.specifierType !== 'url')
248
262
  ) {
249
263
  this.wrappedAssets.add(asset.id);
264
+ wrapped.push(asset);
250
265
  return true;
251
266
  }
252
267
  });
253
268
 
254
269
  this.assetOutputs = new Map(await queue.run());
270
+ return wrapped;
255
271
  }
256
272
 
257
273
  buildExportedSymbols() {
@@ -580,7 +596,7 @@ ${code}
580
596
  if (imported === '*') {
581
597
  replacement = renamed;
582
598
  } else if (imported === 'default') {
583
- replacement = `$parcel$interopDefault(${renamed})`;
599
+ replacement = `($parcel$interopDefault(${renamed}))`;
584
600
  this.usedHelpers.add('$parcel$interopDefault');
585
601
  } else {
586
602
  replacement = this.getPropertyAccess(renamed, imported);
@@ -1043,10 +1059,8 @@ ${code}
1043
1059
  }
1044
1060
 
1045
1061
  // The output format may have specific things to add at the start of the bundle (e.g. imports).
1046
- let [
1047
- outputFormatPrelude,
1048
- outputFormatLines,
1049
- ] = this.outputFormat.buildBundlePrelude();
1062
+ let [outputFormatPrelude, outputFormatLines] =
1063
+ this.outputFormat.buildBundlePrelude();
1050
1064
  res += outputFormatPrelude;
1051
1065
  lines += outputFormatLines;
1052
1066
 
@@ -6,7 +6,7 @@
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(modules, entry, mainEntry, parcelRequireName, globalName) {
9
+ (function (modules, entry, mainEntry, parcelRequireName, globalName) {
10
10
  /* eslint-disable no-undef */
11
11
  var globalObject =
12
12
  typeof globalThis !== 'undefined'
@@ -80,11 +80,13 @@
80
80
  return cache[name].exports;
81
81
 
82
82
  function localRequire(x) {
83
- return newRequire(localRequire.resolve(x));
83
+ var res = localRequire.resolve(x);
84
+ return res === false ? {} : newRequire(res);
84
85
  }
85
86
 
86
87
  function resolve(x) {
87
- return modules[name][1][x] || x;
88
+ var id = modules[name][1][x];
89
+ return id != null ? id : x;
88
90
  }
89
91
  }
90
92
 
@@ -99,9 +101,9 @@
99
101
  newRequire.modules = modules;
100
102
  newRequire.cache = cache;
101
103
  newRequire.parent = previousRequire;
102
- newRequire.register = function(id, exports) {
104
+ newRequire.register = function (id, exports) {
103
105
  modules[id] = [
104
- function(require, module) {
106
+ function (require, module) {
105
107
  module.exports = exports;
106
108
  },
107
109
  {},
@@ -109,7 +111,7 @@
109
111
  };
110
112
 
111
113
  Object.defineProperty(newRequire, 'root', {
112
- get: function() {
114
+ get: function () {
113
115
  return globalObject[parcelRequireName];
114
116
  },
115
117
  });
@@ -131,7 +133,7 @@
131
133
 
132
134
  // RequireJS
133
135
  } else if (typeof define === 'function' && define.amd) {
134
- define(function() {
136
+ define(function () {
135
137
  return mainExports;
136
138
  });
137
139