From 60e727443b79f3814c4b96afc126705c44e514b1 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Mon, 1 Nov 2021 17:22:58 +0100 Subject: [PATCH] Final ESM tweaks * 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. --- docs/06-configuration.md | 5 +---- lib/worker/base.js | 13 +++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/docs/06-configuration.md b/docs/06-configuration.md index eae555d07..fa5a5ed86 100644 --- a/docs/06-configuration.md +++ b/docs/06-configuration.md @@ -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. @@ -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' diff --git a/lib/worker/base.js b/lib/worker/base.js index 685f24662..100dba757 100644 --- a/lib/worker/base.js +++ b/lib/worker/base.js @@ -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); };