mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
Signed-off-by: dansatch <dansatch98@gmail.com> PR-URL: https://github.com/nodejs/node/pull/66054 Fixes: https://github.com/nodejs/node/issues/66043 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
// Test package map path handling: containment, external paths, longest-path-wins.
|
|
import '../common/index.mjs';
|
|
import { spawnSyncAndAssert, spawnSyncAndExit } from '../common/child_process.js';
|
|
import * as fixtures from '../common/fixtures.mjs';
|
|
|
|
// Path containment: does not match pkg-other as being inside pkg.
|
|
spawnSyncAndAssert(process.execPath, [
|
|
'--no-warnings',
|
|
'--experimental-package-map',
|
|
fixtures.path('package-map/package-map-path-prefix.json'),
|
|
'--input-type=module',
|
|
'--eval', `import pkg from 'pkg'; console.log(pkg);`,
|
|
], { cwd: fixtures.path('package-map/pkg-other') }, {
|
|
stdout: /pkg-value/,
|
|
trim: true,
|
|
});
|
|
|
|
// External paths: resolves packages outside the package map directory.
|
|
spawnSyncAndAssert(process.execPath, [
|
|
'--no-warnings',
|
|
'--experimental-package-map',
|
|
fixtures.path('package-map/nested-project/package-map-external-deps.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,
|
|
});
|
|
|
|
// 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',
|
|
'--experimental-package-map',
|
|
fixtures.path('package-map/package-map-url-encoded-spaces.json'),
|
|
'--input-type=module',
|
|
'--eval', `import dep from 'dep-with-spaces'; console.log(dep);`,
|
|
], { cwd: fixtures.path('package-map/root') }, {
|
|
stdout: /dep-with-spaces-value/,
|
|
trim: true,
|
|
});
|
|
|
|
// Longest path wins: nested package uses its own dependencies, not the parent's.
|
|
{
|
|
const longestPathMap = fixtures.path('package-map/package-map-longest-path.json');
|
|
const cwd = fixtures.path('package-map/root');
|
|
|
|
spawnSyncAndAssert(process.execPath, [
|
|
'--no-warnings',
|
|
'--experimental-package-map', longestPathMap,
|
|
'--input-type=module',
|
|
'--eval', `import inner from 'inner'; console.log(inner);`,
|
|
], { cwd }, { stdout: /inner using dep-a-value/, trim: true });
|
|
|
|
// Root does not list dep-a, so importing it from root should fail.
|
|
spawnSyncAndExit(process.execPath, [
|
|
'--no-warnings',
|
|
'--experimental-package-map', longestPathMap,
|
|
'--input-type=module',
|
|
'--eval', `import dep from 'dep-a';`,
|
|
], { cwd }, {
|
|
status: 1,
|
|
signal: null,
|
|
stderr: /ERR_MODULE_NOT_FOUND/,
|
|
});
|
|
}
|