Skip to content

Commit

Permalink
[@types/node] Update the definition of import.meta.resolve(…) for `…
Browse files Browse the repository at this point in the history
…node`.

This was previously available:

- behind a flag,
- returning a `Promise`, and
- with an optional second `parent` parameter.

As of `node` v20.6.0 (https://nodejs.org/en/blog/release/v20.6.0), it is now:

- unflagged (with a single param),
- synchronous, and
- with the `parent` parameter only being supported behind the `--experimental-import-meta-resolve` flag.

To minimize the potential of confusion that would come from mixing the
unflagged version with flagged parameter, this change only documents the
unflagged version. This also matches the API implemented by all
browsers, avoiding the potential misunderstandings about the second
parameter in codebased with mixed frontend and backend code.

Note that there was a bug that sometimes returned a `URL` instead of a
`string` in v20.6.0, but this was fixed in v20.8.0
(nodejs/node#49695).
  • Loading branch information
lgarron committed Oct 12, 2023
1 parent b6a2343 commit 663a218
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
10 changes: 2 additions & 8 deletions types/node/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,12 @@ declare module 'module' {
interface ImportMeta {
url: string;
/**
* @experimental
* This feature is only available with the `--experimental-import-meta-resolve`
* command flag enabled.
*
* Provides a module-relative resolution function scoped to each module, returning
* the URL string.
*
* @param specified The module specifier to resolve relative to `parent`.
* @param parent The absolute parent module URL to resolve from. If none
* is specified, the value of `import.meta.url` is used as the default.
* @param specifier The module specifier to resolve relative to the current module's URL (`import.meta.url`).
*/
resolve?(specified: string, parent?: string | URL): Promise<string>;
resolve(specifier: string): string;
}
}
export = Module;
Expand Down
4 changes: 2 additions & 2 deletions types/node/test/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ const entry: Module.SourceMapping = smap.findEntry(1, 1);
{
const importmeta: ImportMeta = {} as any; // Fake because we cannot really access the true `import.meta` with the current build target
importmeta.url; // $ExpectType string
importmeta.resolve!('local', '/parent'); // $ExpectType Promise<string>
importmeta.resolve!('local', new URL('https://parent.module')); // $ExpectType Promise<string>
importmeta.resolve!('bare-specifier'); // $ExpectType string
importmeta.resolve!('./relative-specifier'); // $ExpectType string
}

0 comments on commit 663a218

Please sign in to comment.