Skip to content

Commit

Permalink
Conditionally generate url for import
Browse files Browse the repository at this point in the history
Windows compatible
  • Loading branch information
JacobLey committed May 28, 2020
1 parent 5ef04be commit f28fd67
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/esm-utils.js
@@ -1,8 +1,16 @@
const path = require('path');
const url = require('url');

const formattedImport = async file => {
if (path.isAbsolute(file)) {
return import(url.pathToFileURL(file));
}
return import(file);
};

exports.requireOrImport = async file => {
if (path.extname(file) === '.mjs') {
return import(file);
return formattedImport(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.
Expand All @@ -12,7 +20,7 @@ exports.requireOrImport = async file => {
return require(file);
} catch (err) {
if (err.code === 'ERR_REQUIRE_ESM') {
return import(file);
return formattedImport(file);
} else {
throw err;
}
Expand Down

0 comments on commit f28fd67

Please sign in to comment.