Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit b3cfb55

Browse files
arcanisaduh95
authored andcommitted
loader: implement package maps
Signed-off-by: Mael Nison <nison.mael@gmail.com> PR-URL: #62239 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 1bde0ef commit b3cfb55

66 files changed

Lines changed: 1811 additions & 34 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

β€Ždoc/api/cli.mdβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,27 @@ added:
11481148
11491149
Enable experimental support for the network inspection with Chrome DevTools.
11501150

1151+
### `--experimental-package-map=<path>`
1152+
1153+
<!-- YAML
1154+
added: REPLACEME
1155+
-->
1156+
1157+
> Stability: 1 - Experimental
1158+
1159+
Enable experimental package map resolution. The `path` argument specifies the
1160+
location of a JSON configuration file that defines package resolution mappings.
1161+
1162+
```bash
1163+
node --experimental-package-map=./package-map.json app.js
1164+
```
1165+
1166+
When enabled, bare specifier resolution consults the package map for resolution.
1167+
This allows explicit control over which packages can import which dependencies.
1168+
1169+
See [Package maps][] for details on the configuration file format and
1170+
resolution algorithm.
1171+
11511172
### `--experimental-print-required-tla`
11521173

11531174
<!-- YAML
@@ -3579,6 +3600,7 @@ one is included in the list below.
35793600
*`--experimental-json-modules`
35803601
*`--experimental-loader`
35813602
*`--experimental-modules`
3603+
*`--experimental-package-map`
35823604
*`--experimental-print-required-tla`
35833605
*`--experimental-quic`
35843606
*`--experimental-require-module`
@@ -4170,6 +4192,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
41704192
[Navigator API]: globals.md#navigator
41714193
[Node.js issue tracker]: https://github.com/nodejs/node/issues
41724194
[OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html
4195+
[Package maps]: packages.md#package-maps
41734196
[Permission Model]: permissions.md#permission-model
41744197
[REPL]: repl.md
41754198
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage

β€Ždoc/api/errors.mdβ€Ž

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,77 @@ A given value is out of the accepted range.
24992499
The `package.json`[`"imports"`][] field does not define the given internal
25002500
package specifier mapping.
25012501

2502+
<aid="ERR_PACKAGE_MAP_EXTERNAL_FILE"></a>
2503+
2504+
### `ERR_PACKAGE_MAP_EXTERNAL_FILE`
2505+
2506+
<!-- YAML
2507+
added: REPLACEME
2508+
-->
2509+
2510+
A module attempted to resolve a bare specifier using the [package map][], but
2511+
the importing file is not located within any package defined in the map.
2512+
2513+
```console
2514+
$ node --experimental-package-map=./package-map.json /tmp/script.js
2515+
Error [ERR_PACKAGE_MAP_EXTERNAL_FILE]: Cannot resolve "dep-a" from "/tmp/script.js": file is not within any package defined in /path/to/package-map.json
2516+
```
2517+
2518+
To fix this error, ensure the importing file is inside one of the package
2519+
directories listed in the package map, or add a new package entry whose `url`
2520+
covers the importing file.
2521+
2522+
<aid="ERR_PACKAGE_MAP_INVALID"></a>
2523+
2524+
### `ERR_PACKAGE_MAP_INVALID`
2525+
2526+
<!-- YAML
2527+
added: REPLACEME
2528+
-->
2529+
2530+
The [package map][] configuration file is invalid. This can occur when:
2531+
2532+
* The file does not exist at the specified path.
2533+
* The file contains invalid JSON.
2534+
* The file is missing the required `packages` object.
2535+
* A package entry is missing the required `url` field.
2536+
* Two package entries have the same `url` value.
2537+
2538+
```console
2539+
$ node --experimental-package-map=./missing.json app.js
2540+
Error [ERR_PACKAGE_MAP_INVALID]: Invalid package map at "./missing.json": file not found
2541+
```
2542+
2543+
<aid="ERR_PACKAGE_MAP_KEY_NOT_FOUND"></a>
2544+
2545+
### `ERR_PACKAGE_MAP_KEY_NOT_FOUND`
2546+
2547+
<!-- YAML
2548+
added: REPLACEME
2549+
-->
2550+
2551+
A package's `dependencies` object in the [package map][] references a package
2552+
key that is not defined in the `packages` object.
2553+
2554+
```json
2555+
{
2556+
"packages": {
2557+
"app": {
2558+
"url": "./app",
2559+
"dependencies": {
2560+
"foo": "nonexistent"
2561+
}
2562+
}
2563+
}
2564+
}
2565+
```
2566+
2567+
In this example, `"nonexistent"` is referenced as a dependency target but not
2568+
defined in `packages`, which will throw this error.
2569+
2570+
To fix this error, ensure all package keys referenced in `dependencies` values
2571+
are defined in the `packages` object.
2572+
25022573
<aid="ERR_PACKAGE_PATH_NOT_EXPORTED"></a>
25032574

25042575
### `ERR_PACKAGE_PATH_NOT_EXPORTED`
@@ -4531,6 +4602,7 @@ An error occurred trying to allocate memory. This should never happen.
45314602
[domains]: domain.md
45324603
[event emitter-based]: events.md#class-eventemitter
45334604
[file descriptors]: https://en.wikipedia.org/wiki/File_descriptor
4605+
[package map]: packages.md#package-maps
45344606
[relative URL]: https://url.spec.whatwg.org/#relative-url-string
45354607
[self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name
45364608
[special scheme]: https://url.spec.whatwg.org/#special-scheme

β€Ždoc/api/esm.mdβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ The default loader has the following properties
949949
* Fails on unknown extensions for `file:` loading
950950
(supports only `.cjs`, `.js`, and `.mjs`)
951951
952+
When the [`--experimental-package-map`][] flag is enabled, bare specifier
953+
resolution first consults the package map configuration. If the importing
954+
module is within a mapped package and the specifier matches a declared
955+
dependency, the package map resolution takes precedence. See [Package maps][]
956+
for details.
957+
952958
### Resolution algorithm
953959
954960
The algorithm to load an ES module specifier is given through the
@@ -1312,13 +1318,15 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
13121318
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
13131319
[Module customization hooks]: module.md#customization-hooks
13141320
[Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification
1321+
[Package maps]: packages.md#package-maps
13151322
[Source Phase Imports]: https://github.com/tc39/proposal-source-phase-imports
13161323
[Terminology]: #terminology
13171324
[Text modules]: #text-modules
13181325
[URL]: https://url.spec.whatwg.org/
13191326
[WebAssembly JS String Builtins Proposal]: https://github.com/WebAssembly/js-string-builtins
13201327
[`"exports"`]: packages.md#exports
13211328
[`"type"`]: packages.md#type
1329+
[`--experimental-package-map`]: cli.md#--experimental-package-mappath
13221330
[`--input-type`]: cli.md#--input-typetype
13231331
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
13241332
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

β€Ždoc/api/modules.mdβ€Ž

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ require(X) from module at path Y
353353
4. If X begins with '#'
354354
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
355355
5. LOAD_PACKAGE_SELF(X, dirname(Y))
356-
6. LOAD_NODE_MODULES(X, dirname(Y))
357-
7. THROW "not found"
356+
6. If a package map PACKAGE_MAP exists,
357+
a. Find the package ID for the package owning Y
358+
1. Let PARENT_PACKAGE_ID be FIND_PACKAGE_ID(dirname(Y), PACKAGE_MAP)
359+
b. LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
360+
7. LOAD_NODE_MODULES(X, dirname(Y))
361+
8. THROW "not found"
358362
359363
MAYBE_DETECT_AND_LOAD(X)
360364
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
@@ -398,9 +402,11 @@ LOAD_AS_DIRECTORY(X)
398402
2. LOAD_INDEX(X)
399403
400404
LOAD_NODE_MODULES(X, START)
401-
1. let DIRS = NODE_MODULES_PATHS(START)
402-
2. for each DIR in DIRS:
403-
a. LOAD_PACKAGE_EXPORTS(X, DIR)
405+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
406+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
407+
2. let DIRS = NODE_MODULES_PATHS(START)
408+
3. for each DIR in DIRS:
409+
a. LOAD_PACKAGE_EXPORTS(SUBPATH, DIR/NAME)
404410
b. LOAD_AS_FILE(DIR/X)
405411
c. LOAD_AS_DIRECTORY(DIR/X)
406412
@@ -415,6 +421,25 @@ NODE_MODULES_PATHS(START)
415421
d. let I = I - 1
416422
5. return DIRS + GLOBAL_FOLDERS
417423
424+
FIND_PACKAGE_ID(PATH, PACKAGE_MAP)
425+
1. Find the PACKAGE_ID for the entry whose "path" is a parent directory of PATH
426+
2. If multiple entries are found, THROW "ambiguous resolution"
427+
3. If no entry was found, THROW "external file".
428+
4. return PACKAGE_ID
429+
430+
LOAD_PACKAGE_MAP(X, PARENT_PACKAGE_ID, PACKAGE_MAP)
431+
1. Try to interpret X as a combination of NAME and SUBPATH where the name
432+
may have a @scope/ prefix and the subpath begins with a slash (`/`).
433+
2. Find the package map entry for key PARENT_PACKAGE_ID
434+
3. Look up NAME in the entry's "dependencies" map.
435+
4. If NAME is not found, THROW "not found".
436+
5. Let TARGET be PACKAGE_MAP.packages[dependencies[name]]
437+
6. Let PACKAGE_PATH be the resolved path of TARGET.
438+
7. LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_PATH)
439+
8. LOAD_AS_FILE(PACKAGE_PATH/SUBPATH)
440+
9. LOAD_AS_DIRECTORY(PACKAGE_PATH/SUBPATH)
441+
10. THROW "not found"
442+
418443
LOAD_PACKAGE_IMPORTS(X, DIR)
419444
1. Find the closest package scope SCOPE to DIR.
420445
2. If no scope was found, return.
@@ -426,19 +451,15 @@ LOAD_PACKAGE_IMPORTS(X, DIR)
426451
CONDITIONS) defined in the ESM resolver.
427452
6. RESOLVE_ESM_MATCH(MATCH).
428453
429-
LOAD_PACKAGE_EXPORTS(X, DIR)
430-
1. Try to interpret X as a combination of NAME and SUBPATH where the name
431-
may have a @scope/ prefix and the subpath begins with a slash (`/`).
432-
2. If X does not match this pattern or DIR/NAME/package.json is not a file,
433-
return.
434-
3. Parse DIR/NAME/package.json, and look for "exports" field.
435-
4. If "exports" is null or undefined, return.
436-
5. If `--no-require-module` is not enabled
454+
LOAD_PACKAGE_EXPORTS(SUBPATH, PACKAGE_DIR)
455+
1. Parse PACKAGE_DIR/package.json, and look for "exports" field.
456+
2. If "exports" is null or undefined, return.
457+
3. If `--no-require-module` is not enabled
437458
a. let CONDITIONS = ["node", "require", "module-sync"]
438459
b. Else, let CONDITIONS = ["node", "require"]
439-
6. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(DIR/NAME), "." + SUBPATH,
460+
4. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(PACKAGE_DIR), "." + SUBPATH,
440461
`package.json` "exports", CONDITIONS) defined in the ESM resolver.
441-
7. RESOLVE_ESM_MATCH(MATCH)
462+
5. RESOLVE_ESM_MATCH(MATCH)
442463
443464
LOAD_PACKAGE_SELF(X, DIR)
444465
1. Find the closest package scope SCOPE to DIR.

0 commit comments

Comments
Β (0)