Skip to content

Commit d45e9ce

Browse files
arcanisaduh95
andauthored
Apply suggestions from code review
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 0d0685a commit d45e9ce

6 files changed

Lines changed: 15 additions & 36 deletions

File tree

doc/api/packages.md

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ See [the package examples repository][] for details.
953953
added: REPLACEME
954954
-->
955955

956-
> Stability: 1 - Experimental
956+
> Stability: 1 - Experimental. Enable this API with [`--experimental-package-map`][].
957957
958958
Package maps provide a mechanism to control package resolution without relying
959959
on the `node_modules` folder structure. When enabled via the
@@ -969,15 +969,6 @@ This feature is useful for:
969969
* **Multiple versions**: Allow different packages to depend on different
970970
versions of the same dependency.
971971

972-
### Enabling package maps
973-
974-
Package maps are enabled by passing the `--experimental-package-map` flag
975-
with a path to the configuration file:
976-
977-
```bash
978-
node --experimental-package-map=./package-map.json app.js
979-
```
980-
981972
### Configuration file format
982973

983974
The package map configuration file is a JSON file with a `packages` object.
@@ -1107,7 +1098,7 @@ originates from:
11071098

11081099
In the example above both `lib-old` and `lib-new` use the same `./lib` folder to
11091100
store their sources, the only difference being in which version of `react` they'll
1110-
access when performing require calls.
1101+
access when performing `require` calls or using `import`.
11111102

11121103
Because multiple package entries share the same path, resolving a bare specifier
11131104
from a file within that path is ambiguous unless the originating package ID is
@@ -1120,21 +1111,6 @@ and propagate it from each resolution result to subsequent resolution requests.
11201111
This ensures that when `lib` requires `react`, the runtime knows whether the
11211112
request comes from `lib-old` or `lib-new` and can select the correct dependency.
11221113

1123-
### CommonJS and ES modules
1124-
1125-
Package maps work with both CommonJS (`require()`) and ES modules (`import`).
1126-
The resolution behavior is identical for both module systems.
1127-
1128-
```cjs
1129-
// CommonJS
1130-
const utils = require('@myorg/utils');
1131-
```
1132-
1133-
```mjs
1134-
// ES modules
1135-
import utils from '@myorg/utils';
1136-
```
1137-
11381114
### Interaction with other resolution
11391115

11401116
Package maps only apply to bare specifiers that are not Node.js builtin

lib/internal/modules/cjs/loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ function tryPackageMapResolveCJS(request, parent, conditions) {
670670
const message = getRequireStackMessage(request, requireStack);
671671
// eslint-disable-next-line no-restricted-syntax
672672
const err = new Error(message);
673-
err.code = 'MODULE_NOT_FOUND';
674-
err.requireStack = requireStack;
673+
setOwnProperty(err, 'code', 'MODULE_NOT_FOUND');
674+
setOwnProperty(err, 'requireStack', requireStack);
675675
throw err;
676676
}
677677

@@ -1518,8 +1518,8 @@ Module._resolveFilename = function(request, parent, isMain, options) {
15181518

15191519
// eslint-disable-next-line no-restricted-syntax
15201520
const err = new Error(message);
1521-
err.code = 'MODULE_NOT_FOUND';
1522-
err.requireStack = requireStack;
1521+
setOwnProperty(err, 'code', 'MODULE_NOT_FOUND');
1522+
setOwnProperty(err, 'requireStack', requireStack);
15231523

15241524
throw err;
15251525
};

lib/internal/modules/package_map.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class PackageMap {
7171
throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, 'missing "packages" object');
7272
}
7373

74-
for (const { 0: key, 1: entry } of ObjectEntries(data.packages)) {
74+
const entries = ObjectEntries(data.packages);
75+
for (let i = 0; i < entries.length; i++) {
76+
const { 0: key, 1: entry } = entry;
7577
if (!entry.path) {
7678
throw new ERR_PACKAGE_MAP_INVALID(this.#configPath, `package "${key}" is missing "path" field`);
7779
}
@@ -99,8 +101,8 @@ class PackageMap {
99101

100102
// TODO(arcanis): Duplicates should be tolerated, but it requires changing module IDs
101103
// to include the package ID whenever a package map is used.
102-
if (this.#pathToKey.has(absolutePath)) {
103-
const existingKey = this.#pathToKey.get(absolutePath);
104+
const existingKey = this.#pathToKey.get(absolutePath);
105+
if (existingKey != null) {
104106
throw new ERR_PACKAGE_MAP_INVALID(
105107
this.#configPath,
106108
`packages "${existingKey}" and "${key}" have the same path "${entry.path}"; this will be supported in the future`,

test/es-module/test-esm-package-map.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ describe('ESM: --experimental-package-map', () => {
3939
`import dep from 'dep-a'; console.log(dep);`,
4040
], { cwd: fixtures.path('package-map/root') });
4141

42-
assert.strictEqual(code, 0, stderr);
42+
assert.strictEqual(stderr, '');
4343
assert.match(stdout, /dep-a-value/);
44+
assert.strictEqual(code, 0);
4445
});
4546

4647
it('resolves a subpath export', async () => {

test/parallel/test-package-map-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ describe('--experimental-package-map CLI behavior', () => {
3939
encoding: 'utf8',
4040
});
4141

42-
assert.strictEqual(status, 0);
4342
assert.match(stderr, /ExperimentalWarning/);
4443
assert.match(stderr, /[Pp]ackage map/);
44+
assert.strictEqual(status, 0);
4545
});
4646

4747
it('accepts relative path', () => {

test/parallel/test-require-package-map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ writeFileSync(fileUrlFixturePath, JSON.stringify({
2828
},
2929
}));
3030

31-
describe('CJS: --experimental-package-map', () => {
31+
describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PARALLEL }, () => {
3232

3333
describe('basic resolution', () => {
3434
it('resolves require() through package map', () => {

0 commit comments

Comments
 (0)