Skip to content

Commit

Permalink
Merge fa51418 into c667d10
Browse files Browse the repository at this point in the history
  • Loading branch information
outsideris committed Jan 20, 2021
2 parents c667d10 + fa51418 commit 6464eb4
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions lib/esm-utils.js
Expand Up @@ -2,10 +2,40 @@ const path = require('path');
const url = require('url');

const formattedImport = async file => {
if (path.isAbsolute(file)) {
return import(url.pathToFileURL(file));
try {
if (path.isAbsolute(file)) {
return await import(url.pathToFileURL(file));
}
return import(file);
} catch (err) {
if (err.name === 'SyntaxError') {
// This is a hack to print where syntax error is from.
// In dynamic import, it doesn't contain filename linenumber in errer.stack
// See https://github.com/nodejs/modules/issues/471
// FIXME: remove this hack after nodejs suppurt
return await new Promise((resolve, reject) => {
const {spawn} = require('child_process');

const proc = spawn(process.execPath, [
'--unhandled-rejections=throw',
'-e',
`import('${file}')`
]);

let errMsg = '';

proc.stderr.on('data', data => {
errMsg += data;
});

proc.on('exit', () => {
const err = new Error(errMsg);
err.stack = errMsg;
reject(err);
});
});
}
}
return import(file);
};

exports.requireOrImport = async file => {
Expand Down

0 comments on commit 6464eb4

Please sign in to comment.