From f28fd6728c7ffce6d12e1c9340d6f55063a93310 Mon Sep 17 00:00:00 2001 From: Jacob Ley Date: Thu, 28 May 2020 10:54:42 -0400 Subject: [PATCH] Conditionally generate url for import Windows compatible --- lib/esm-utils.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/esm-utils.js b/lib/esm-utils.js index e94580eea8..604f883d9a 100644 --- a/lib/esm-utils.js +++ b/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. @@ -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; }