-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for Node.JS native ES modules
- Loading branch information
Showing
26 changed files
with
428 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This file is allowed to use async/await because it is not exposed to browsers (see the `eslintrc`), | ||
// and Node supports async/await in all its non-dead version. | ||
|
||
const url = require('url'); | ||
const path = require('path'); | ||
|
||
exports.requireOrImport = async file => { | ||
file = path.resolve(file); | ||
|
||
if (path.extname(file) === '.mjs') { | ||
return import(url.pathToFileURL(file)); | ||
} | ||
// This way of figuring out whether a test file is CJS or ESM is currently the only known | ||
// way of figuring out whether a file is CJS or ESM. | ||
// If Node.js or the community establish a better procedure for that, we can fix this code. | ||
// Another option here would be to always use `import()`, as this also supports CJS, but I would be | ||
// wary of using it for _all_ existing test files, till ESM is fully stable. | ||
try { | ||
return require(file); | ||
} catch (err) { | ||
if (err.code === 'ERR_REQUIRE_ESM') { | ||
return import(url.pathToFileURL(file)); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}; | ||
|
||
exports.loadFilesAsync = async (files, preLoadFunc, postLoadFunc) => { | ||
for (const file of files) { | ||
preLoadFunc(file); | ||
const result = await exports.requireOrImport(file); | ||
postLoadFunc(file, result); | ||
} | ||
}; |
Oops, something went wrong.