-
-
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 (#4038)
- Loading branch information
Gil Tayar
authored
Feb 24, 2020
1 parent
a995e33
commit 57be455
Showing
25 changed files
with
372 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const url = require('url'); | ||
const path = require('path'); | ||
|
||
const requireOrImport = async file => { | ||
file = path.resolve(file); | ||
|
||
if (path.extname(file) === '.mjs') { | ||
return import(url.pathToFileURL(file)); | ||
} | ||
// This 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 requireOrImport(file); | ||
postLoadFunc(file, result); | ||
} | ||
}; |
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
Oops, something went wrong.