loader: implement package maps · nodejs/node@b3cfb55 · GitHub
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

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+
<a id="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+
<a id="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+
<a id="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
<a id="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

0 commit comments

Comments
 (0)