-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for ESM config files (#987)
Co-authored-by: Tomer <tomer@corevo.io>
- Loading branch information
Showing
7 changed files
with
146 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// this file is imported by import-helper to detect whether dynamic imports are supported. |
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 @@ | ||
async function supportsDynamicImport() { | ||
try { | ||
// imports are cached. | ||
// no need to worry about perf here. | ||
// Don't remove .js: extension must be included for ESM imports! | ||
await import('./dummy-file.js'); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Imports a JSON, CommonJS or ESM module | ||
* based on feature detection. | ||
* | ||
* @param modulePath path to the module to import | ||
* @returns {Promise<unknown>} the imported module. | ||
*/ | ||
async function importModule(modulePath) { | ||
// JSON modules are still behind a flag. Fallback to require for now. | ||
// https://nodejs.org/api/esm.html#json-modules | ||
if (!modulePath.endsWith('.json') && (await supportsDynamicImport())) { | ||
return import(modulePath); | ||
} | ||
|
||
// mimics what `import()` would return for | ||
// cjs modules. | ||
return { default: require(modulePath) }; | ||
} | ||
|
||
module.exports = { | ||
supportsDynamicImport, | ||
importModule, | ||
}; |
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