Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/package_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class PackageMap {

let absolutePath;
try {
absolutePath = fileURLToPath(packageURL);
absolutePath = pathResolve(fileURLToPath(packageURL));
} catch (err) {
const error = new ERR_PACKAGE_MAP_INVALID(
this.#configPath,
Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-esm-package-map-paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ spawnSyncAndAssert(process.execPath, [
trim: true,
});

// Directory-form URLs match package files even when the map is nested below them.
spawnSyncAndAssert(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/nested-project/map-dir/package-map-parent-url.json'),
'--input-type=module',
'--eval', `import dep from 'dep-a'; console.log(dep);`,
], { cwd: fixtures.path('package-map/nested-project/src') }, {
stdout: /dep-a-value/,
trim: true,
});

// URL fields are decoded as URLs before being converted to paths.
spawnSyncAndAssert(process.execPath, [
'--no-warnings',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"packages": {
"app": {
"url": "..",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "../../dep-a/",
"dependencies": {}
}
}
}
12 changes: 12 additions & 0 deletions test/fixtures/package-map/package-map-directory-urls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"packages": {
"root": {
"url": "./root/",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "./dep-a/",
"dependencies": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"packages": {
"root": {
"url": "./root",
"dependencies": {"dep-a": "dep-a"}
},
"dep-a": {
"url": "./dep-a",
"dependencies": {}
},
"dep-a-directory": {
"url": "./dep-a/",
"dependencies": {}
}
}
}
51 changes: 51 additions & 0 deletions test/parallel/test-require-package-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ writeFileSync(fileUrlFixturePath, JSON.stringify({
describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PARALLEL }, () => {

describe('basic resolution', () => {
it('resolves dependencies from a parent directory URL', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/nested-project/map-dir/package-map-parent-url.json'),
'-e',
`const dep = require('dep-a'); console.log(dep.default);`,
], {
cwd: fixtures.path('package-map/nested-project/src'),
encoding: 'utf8',
});

assert.strictEqual(stderr, '');
assert.match(stdout, /dep-a-value/);
assert.strictEqual(status, 0, stderr);
});

it('resolves packages with directory-form URLs', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/package-map-directory-urls.json'),
'-e',
`const dep = require('dep-a'); console.log(dep.default);`,
], {
cwd: fixtures.path('package-map/root'),
encoding: 'utf8',
});

assert.strictEqual(stderr, '');
assert.match(stdout, /dep-a-value/);
assert.strictEqual(status, 0, stderr);
});

it('resolves require() through package map', () => {
const { status, stdout, stderr } = spawnSync(process.execPath, [
'--no-warnings',
Expand Down Expand Up @@ -197,6 +231,23 @@ describe('CJS: --experimental-package-map', { concurrency: !process.env.TEST_PAR
assert.match(stderr, /pkg-b/);
assert.notStrictEqual(status, 0, stderr);
});

it('throws for package URLs differing only by a trailing separator', () => {
const { status, stderr } = spawnSync(process.execPath, [
'--no-warnings',
'--experimental-package-map',
fixtures.path('package-map/package-map-duplicate-directory-path.json'),
'-e',
`require('dep-a');`,
], {
cwd: fixtures.path('package-map/root'),
encoding: 'utf8',
});

assert.match(stderr, /ERR_PACKAGE_MAP_INVALID/);
assert.match(stderr, /dep-a-directory/);
assert.notStrictEqual(status, 0, stderr);
});
});

describe('conditional exports', () => {
Expand Down