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

Final ESM tweaks #2882

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 1 addition & 4 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default {
};
```

### Configuring module formats
## Configuring module formats

Node.js can only load non-standard extension as ES Modules when using [experimental loaders](https://nodejs.org/docs/latest/api/esm.html#esm_experimental_loaders). To use this you'll also have to configure AVA to `import()` your test file.

Expand All @@ -264,9 +264,6 @@ As with the array form, you need to explicitly list `js`, `cjs`, and `mjs` exten
`ava.config.js`:
```js
export default {
nonSemVerExperiments: {
configurableModuleFormat: true
},
extensions: {
js: true,
ts: 'module'
Expand Down
13 changes: 7 additions & 6 deletions lib/worker/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,19 @@ const run = async options => {

const require = createRequire(import.meta.url);
const load = async ref => {
for (const extension of extensionsToLoadAsModules) {
if (ref.endsWith(`.${extension}`)) {
return import(pathToFileURL(ref)); // eslint-disable-line node/no-unsupported-features/es-syntax
}
}

for (const provider of providers) {
if (provider.canLoad(ref)) {
return provider.load(ref, {requireFn: require});
}
}

for (const extension of extensionsToLoadAsModules) {
if (ref.endsWith(`.${extension}`)) {
return import(pathToFileURL(ref)); // eslint-disable-line node/no-unsupported-features/es-syntax
}
}

// We still support require() since it's more easily monkey-patched.
return require(ref);
};

Expand Down