Commit 2ac8377
authored
feat: implement module type resolution algorithm
resolves #527
fixes #508
This PR adds a new `module_type` option and also to the returned result.
```ts
export interface NapiResolveOptions {
/**
* Resolve [ResolveResult::moduleType].
*
* Default `false`
*/
moduleType?: boolean
}
```
```ts
export interface ResolveResult {
/**
* Module type for this path.
*
* The module type is computed `ESM_FILE_FORMAT` from the [ESM resolution algorithm specification](https://nodejs.org/docs/latest/api/esm.html#resolution-algorithm-specification).
*
* The algorithm uses the file extension or finds the closest `package.json` with the `type` field.
*/
moduleType?: ModuleType
}
export declare const enum ModuleType {
Module = 'module',
CommonJs = 'commonjs',
Json = 'json',
Wasm = 'wasm',
Addon = 'addon'
}
```
Node.js will treat the following as [ES
modules](https://nodejs.org/api/esm.html):
* Files with an .mjs extension.
* Files with a .js extension when the nearest parent package.json file
contains a top-level ["type"](https://nodejs.org/api/packages.html#type)
field with a value of "module".
Node.js will treat the following as
[CommonJS](https://nodejs.org/api/modules.html):
* Files with a .cjs extension.
* Files with a .js extension when the nearest parent package.json file
contains a top-level field
["type"](https://nodejs.org/api/packages.html#type) with a value of
"commonjs".
See
* https://nodejs.org/api/packages.html#determining-module-system
* `ESM_FILE_FORMAT` in
https://nodejs.org/docs/latest/api/esm.html#resolution-algorithm-specificationESM_FILE_FORMAT from the spec (#535)1 parent ff7bd35 commit 2ac8377
File tree
35 files changed
+289
-16
lines changed- examples
- fixtures
- misc/module-type
- cjs
- esm
- no
- pnpm
- napi
- src
- tests
- src
- tests
- tests
35 files changed
+289
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
Whitespace-only changes.
Whitespace-only changes.
Whitespace-only changes.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
Whitespace-only changes.
0 commit comments