Skip to content

Commit

Permalink
Final ESM tweaks
Browse files Browse the repository at this point in the history
* Allow providers to load files even if they're ESM.
* Clarify why we still require().
* Update documentation since module format configuration is no longer experimental.
  • Loading branch information
novemberborn committed Nov 1, 2021
1 parent 60b7cf8 commit 88e7680
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
5 changes: 1 addition & 4 deletions docs/06-configuration.md
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
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

0 comments on commit 88e7680

Please sign in to comment.