Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: unflag import.meta.resolve #49028

Merged
merged 8 commits into from
Aug 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs changes
  • Loading branch information
guybedford authored and GeoffreyBooth committed Aug 13, 2023
commit f865a1d36d8fd8f4330a456aabaca0fe91523747
47 changes: 27 additions & 20 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
```

### `import.meta.resolve(specifier[, parent])`
### `import.meta.resolve(specifier)`

<!--
added:
Expand All @@ -337,36 +337,42 @@ changes:
- v14.18.0
pr-url: https://github.com/nodejs/node/pull/38587
description: Add support for WHATWG `URL` object to `parentURL` parameter.
- version:
- REPLACEME
pr-url: https://github.com/nodejs/node/pull/49028
description: Unflag import.meta.resolve, with `parentURL` parameter still
flagged.
-->

> Stability: 1 - Experimental
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved

This feature is only available with the `--experimental-import-meta-resolve`
command flag enabled.

* `specifier` {string} The module specifier to resolve relative to `parent`.
* `parent` {string|URL} The absolute parent module URL to resolve from. If none
is specified, the value of `import.meta.url` is used as the default.
* Returns: {string}

Provides a module-relative resolution function scoped to each module, returning
the URL string. In alignment with browser behavior, this now returns
synchronously.
* Returns: {string} The absolute (`file:`) URL string for the resolved module.

> **Caveat** This can result in synchronous file-system operations, which
> can impact performance similarly to `require.resolve`.
[`import.meta.resolve`][] is a module-relative resolution function scoped to
each module, returning the URL string.

```js
const dependencyAsset = import.meta.resolve('component-lib/asset.css');
// file:https:///app/node_modules/component-lib/asset.css
```

`import.meta.resolve` also accepts a second argument which is the parent module
from which to resolve:
All features of the Node.js module resolution are supported. Dependency
resolutions are subject to the permitted exports resolutions within the package.

```js
import.meta.resolve('./dep', import.meta.url);
// file:https:///app/dep
```

> **Caveat** This can result in synchronous file-system operations, which
> can impact performance similarly to `require.resolve`.

Previously, Node.js implemented an asynchonous resolver which also permitted
a second contextual argument. The implementation has since been updated to be
synchronous, with the second contextual `parent` URL still accessible behind the
`--experimental-import-meta-resolve` flag:

* `parent` {string|URL} An optional absolute parent module URL to resolve from.

## Interoperability with CommonJS

### `import` statements
Expand Down Expand Up @@ -501,8 +507,8 @@ They can instead be loaded with [`module.createRequire()`][] or

Relative resolution can be handled via `new URL('./local', import.meta.url)`.

For a complete `require.resolve` replacement, there is a flagged experimental
[`import.meta.resolve`][] API.
For a complete `require.resolve` replacement, there is the
[import.meta.resolve][] API.

Alternatively `module.createRequire()` can be used.

Expand Down Expand Up @@ -1672,7 +1678,7 @@ for ESM specifiers is [commonjs-extension-resolution-loader][].
[`data:` URLs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
[`export`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
[`import()`]: #import-expressions
[`import.meta.resolve`]: #importmetaresolvespecifier-parent
[`import.meta.resolve`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve
[`import.meta.url`]: #importmetaurl
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[`initialize`]: #initialize
Expand All @@ -1690,6 +1696,7 @@ for ESM specifiers is [commonjs-extension-resolution-loader][].
[cjs-module-lexer]: https://github.com/nodejs/cjs-module-lexer/tree/1.2.2
[commonjs-extension-resolution-loader]: https://github.com/nodejs/loaders-test/tree/main/commonjs-extension-resolution-loader
[custom https loader]: #https-loader
[import.meta.resolve]: #importmetaresolvespecifier
[load hook]: #loadurl-context-nextload
[percent-encoded]: url.md#percent-encoding-in-urls
[special scheme]: https://url.spec.whatwg.org/#special-scheme
Expand Down