@parcel/packager-js 2.3.2 → 2.5.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.
@@ -141,6 +141,12 @@ class DevPackager {
141
141
  wrapped += JSON.stringify(deps);
142
142
  wrapped += ']';
143
143
 
144
+ if (this.bundle.env.isNode() && asset.meta.has_node_replacements === true) {
145
+ const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
146
+ wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
147
+ wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
148
+ }
149
+
144
150
  if (this.bundle.env.sourceMap) {
145
151
  if (mapBuffer) {
146
152
  map.addBuffer(mapBuffer, lineOffset);
@@ -65,6 +65,16 @@ function _globals() {
65
65
  return data;
66
66
  }
67
67
 
68
+ function _path() {
69
+ const data = _interopRequireDefault(require("path"));
70
+
71
+ _path = function () {
72
+ return data;
73
+ };
74
+
75
+ return data;
76
+ }
77
+
68
78
  var _ESMOutputFormat = require("./ESMOutputFormat");
69
79
 
70
80
  var _CJSOutputFormat = require("./CJSOutputFormat");
@@ -249,7 +259,7 @@ class ScopeHoistingPackager {
249
259
  maxConcurrent: 32
250
260
  });
251
261
  let wrapped = [];
252
- this.bundle.traverseAssets((asset, shouldWrap) => {
262
+ this.bundle.traverseAssets(asset => {
253
263
  queue.add(async () => {
254
264
  let [code, map] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap ? asset.getMapBuffer() : null]);
255
265
  return [asset.id, {
@@ -258,12 +268,28 @@ class ScopeHoistingPackager {
258
268
  }];
259
269
  });
260
270
 
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')) {
271
+ if (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')) {
262
272
  this.wrappedAssets.add(asset.id);
263
273
  wrapped.push(asset);
264
- return true;
265
274
  }
266
275
  });
276
+
277
+ for (let wrappedAssetRoot of [...wrapped]) {
278
+ this.bundle.traverseAssets((asset, _, actions) => {
279
+ if (asset === wrappedAssetRoot) {
280
+ return;
281
+ }
282
+
283
+ if (this.wrappedAssets.has(asset.id)) {
284
+ actions.skipChildren();
285
+ return;
286
+ }
287
+
288
+ this.wrappedAssets.add(asset.id);
289
+ wrapped.push(asset);
290
+ }, wrappedAssetRoot);
291
+ }
292
+
267
293
  this.assetOutputs = new Map(await queue.run());
268
294
  return wrapped;
269
295
  }
@@ -394,6 +420,12 @@ class ScopeHoistingPackager {
394
420
  this.usedHelpers.add('$parcel$global');
395
421
  }
396
422
 
423
+ if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
424
+ const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
425
+ code = code.replace('$parcel$dirnameReplace', relPath);
426
+ code = code.replace('$parcel$filenameReplace', relPath);
427
+ }
428
+
397
429
  let [depMap, replacements] = this.buildReplacements(asset, deps);
398
430
  let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
399
431
 
@@ -682,10 +714,12 @@ ${code}
682
714
 
683
715
  let isWrapped = !this.bundle.hasAsset(resolvedAsset) || this.wrappedAssets.has(resolvedAsset.id) && resolvedAsset !== parentAsset;
684
716
  let staticExports = resolvedAsset.meta.staticExports !== false;
685
- let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset); // If the rsolved asset is wrapped, but imported at the top-level by this asset,
717
+ let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset); // If the resolved asset is wrapped, but imported at the top-level by this asset,
686
718
  // then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
687
719
 
688
- if (isWrapped && dep && !(dep !== null && dep !== void 0 && dep.meta.shouldWrap) && symbol !== false) {
720
+ if (isWrapped && dep && !(dep !== null && dep !== void 0 && dep.meta.shouldWrap) && symbol !== false && ( // Only do this if the asset is part of a different bundle (so it was definitely
721
+ // parcelRequire.register'ed there), or if it is indeed registered in this bundle.
722
+ !this.bundle.hasAsset(resolvedAsset) || !this.shouldSkipAsset(resolvedAsset))) {
689
723
  let hoisted = this.hoistedRequires.get(dep.id);
690
724
 
691
725
  if (!hoisted) {
@@ -714,7 +748,12 @@ ${code}
714
748
 
715
749
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
716
750
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
717
- return obj;
751
+ if (parentAsset === resolvedAsset && this.wrappedAssets.has(resolvedAsset.id)) {
752
+ // Directly use module.exports for wrapped assets importing themselves.
753
+ return 'module.exports';
754
+ } else {
755
+ return obj;
756
+ }
718
757
  } else if ((!staticExports || isWrapped || !symbol) && resolvedAsset !== parentAsset) {
719
758
  // If the resolved asset is wrapped or has non-static exports,
720
759
  // we need to use a member access off the namespace object rather
package/lib/index.js CHANGED
@@ -117,7 +117,8 @@ var _default = new (_plugin().Packager)({
117
117
  bundle,
118
118
  bundleGraph,
119
119
  contents,
120
- map
120
+ map,
121
+ getReplacement: s => JSON.stringify(s).slice(1, -1)
121
122
  }));
122
123
  }
123
124
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.3.2",
3
+ "version": "2.5.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.3.2"
20
+ "parcel": "^2.5.0"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/diagnostic": "2.3.2",
24
- "@parcel/hash": "2.3.2",
25
- "@parcel/plugin": "2.3.2",
23
+ "@parcel/diagnostic": "2.5.0",
24
+ "@parcel/hash": "2.5.0",
25
+ "@parcel/plugin": "2.5.0",
26
26
  "@parcel/source-map": "^2.0.0",
27
- "@parcel/utils": "2.3.2",
27
+ "@parcel/utils": "2.5.0",
28
28
  "globals": "^13.2.0",
29
29
  "nullthrows": "^1.1.1"
30
30
  },
31
- "gitHead": "47379bf8fabeb2cfe03ade8802d942388b153e5b"
31
+ "gitHead": "5cfb846d742eb86b1232e531be6e0e513551d838"
32
32
  }
@@ -1,7 +1,12 @@
1
1
  // @flow strict-local
2
2
  import type {BundleGraph, PluginOptions, NamedBundle} from '@parcel/types';
3
3
 
4
- import {PromiseQueue, relativeBundlePath, countLines} from '@parcel/utils';
4
+ import {
5
+ PromiseQueue,
6
+ relativeBundlePath,
7
+ countLines,
8
+ normalizeSeparators,
9
+ } from '@parcel/utils';
5
10
  import SourceMap from '@parcel/source-map';
6
11
  import invariant from 'assert';
7
12
  import path from 'path';
@@ -116,6 +121,20 @@ export class DevPackager {
116
121
  wrapped += JSON.stringify(deps);
117
122
  wrapped += ']';
118
123
 
124
+ if (
125
+ this.bundle.env.isNode() &&
126
+ asset.meta.has_node_replacements === true
127
+ ) {
128
+ const relPath = normalizeSeparators(
129
+ path.relative(
130
+ this.bundle.target.distDir,
131
+ path.dirname(asset.filePath),
132
+ ),
133
+ );
134
+ wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
135
+ wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
136
+ }
137
+
119
138
  if (this.bundle.env.sourceMap) {
120
139
  if (mapBuffer) {
121
140
  map.addBuffer(mapBuffer, lineOffset);
@@ -8,12 +8,18 @@ import type {
8
8
  NamedBundle,
9
9
  } from '@parcel/types';
10
10
 
11
- import {PromiseQueue, relativeBundlePath, countLines} from '@parcel/utils';
11
+ import {
12
+ PromiseQueue,
13
+ relativeBundlePath,
14
+ countLines,
15
+ normalizeSeparators,
16
+ } from '@parcel/utils';
12
17
  import SourceMap from '@parcel/source-map';
13
18
  import nullthrows from 'nullthrows';
14
19
  import invariant from 'assert';
15
20
  import ThrowableDiagnostic from '@parcel/diagnostic';
16
21
  import globals from 'globals';
22
+ import path from 'path';
17
23
 
18
24
  import {ESMOutputFormat} from './ESMOutputFormat';
19
25
  import {CJSOutputFormat} from './CJSOutputFormat';
@@ -241,7 +247,7 @@ export class ScopeHoistingPackager {
241
247
  async loadAssets(): Promise<Array<Asset>> {
242
248
  let queue = new PromiseQueue({maxConcurrent: 32});
243
249
  let wrapped = [];
244
- this.bundle.traverseAssets((asset, shouldWrap) => {
250
+ this.bundle.traverseAssets(asset => {
245
251
  queue.add(async () => {
246
252
  let [code, map] = await Promise.all([
247
253
  asset.getCode(),
@@ -251,7 +257,6 @@ export class ScopeHoistingPackager {
251
257
  });
252
258
 
253
259
  if (
254
- shouldWrap ||
255
260
  asset.meta.shouldWrap ||
256
261
  this.isAsyncBundle ||
257
262
  this.bundle.env.sourceType === 'script' ||
@@ -262,10 +267,25 @@ export class ScopeHoistingPackager {
262
267
  ) {
263
268
  this.wrappedAssets.add(asset.id);
264
269
  wrapped.push(asset);
265
- return true;
266
270
  }
267
271
  });
268
272
 
273
+ for (let wrappedAssetRoot of [...wrapped]) {
274
+ this.bundle.traverseAssets((asset, _, actions) => {
275
+ if (asset === wrappedAssetRoot) {
276
+ return;
277
+ }
278
+
279
+ if (this.wrappedAssets.has(asset.id)) {
280
+ actions.skipChildren();
281
+ return;
282
+ }
283
+
284
+ this.wrappedAssets.add(asset.id);
285
+ wrapped.push(asset);
286
+ }, wrappedAssetRoot);
287
+ }
288
+
269
289
  this.assetOutputs = new Map(await queue.run());
270
290
  return wrapped;
271
291
  }
@@ -405,6 +425,14 @@ export class ScopeHoistingPackager {
405
425
  this.usedHelpers.add('$parcel$global');
406
426
  }
407
427
 
428
+ if (this.bundle.env.isNode() && asset.meta.has_node_replacements) {
429
+ const relPath = normalizeSeparators(
430
+ path.relative(this.bundle.target.distDir, path.dirname(asset.filePath)),
431
+ );
432
+ code = code.replace('$parcel$dirnameReplace', relPath);
433
+ code = code.replace('$parcel$filenameReplace', relPath);
434
+ }
435
+
408
436
  let [depMap, replacements] = this.buildReplacements(asset, deps);
409
437
  let [prepend, prependLines, append] = this.buildAssetPrelude(asset, deps);
410
438
  if (prependLines > 0) {
@@ -732,9 +760,18 @@ ${code}
732
760
  let staticExports = resolvedAsset.meta.staticExports !== false;
733
761
  let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset);
734
762
 
735
- // If the rsolved asset is wrapped, but imported at the top-level by this asset,
763
+ // If the resolved asset is wrapped, but imported at the top-level by this asset,
736
764
  // then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
737
- if (isWrapped && dep && !dep?.meta.shouldWrap && symbol !== false) {
765
+ if (
766
+ isWrapped &&
767
+ dep &&
768
+ !dep?.meta.shouldWrap &&
769
+ symbol !== false &&
770
+ // Only do this if the asset is part of a different bundle (so it was definitely
771
+ // parcelRequire.register'ed there), or if it is indeed registered in this bundle.
772
+ (!this.bundle.hasAsset(resolvedAsset) ||
773
+ !this.shouldSkipAsset(resolvedAsset))
774
+ ) {
738
775
  let hoisted = this.hoistedRequires.get(dep.id);
739
776
  if (!hoisted) {
740
777
  hoisted = new Map();
@@ -778,7 +815,15 @@ ${code}
778
815
 
779
816
  if (imported === '*' || exportSymbol === '*' || isDefaultInterop) {
780
817
  // Resolve to the namespace object if requested or this is a CJS default interop reqiure.
781
- return obj;
818
+ if (
819
+ parentAsset === resolvedAsset &&
820
+ this.wrappedAssets.has(resolvedAsset.id)
821
+ ) {
822
+ // Directly use module.exports for wrapped assets importing themselves.
823
+ return 'module.exports';
824
+ } else {
825
+ return obj;
826
+ }
782
827
  } else if (
783
828
  (!staticExports || isWrapped || !symbol) &&
784
829
  resolvedAsset !== parentAsset
package/src/index.js CHANGED
@@ -72,6 +72,7 @@ export default (new Packager({
72
72
  bundleGraph,
73
73
  contents,
74
74
  map,
75
+ getReplacement: s => JSON.stringify(s).slice(1, -1),
75
76
  }));
76
77
  }
77
78