-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support --require of ESM; closes #4281 #4304
Changes from 1 commit
5ef04be
f28fd67
e9834fd
ebc2a07
c411c30
a6029b9
cc401ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
const url = require('url'); | ||
const path = require('path'); | ||
|
||
const requireOrImport = async file => { | ||
file = path.resolve(file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved |
||
|
||
exports.requireOrImport = async file => { | ||
if (path.extname(file) === '.mjs') { | ||
return import(url.pathToFileURL(file)); | ||
return import(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. | ||
|
@@ -15,7 +12,7 @@ const requireOrImport = async file => { | |
return require(file); | ||
} catch (err) { | ||
if (err.code === 'ERR_REQUIRE_ESM') { | ||
return import(url.pathToFileURL(file)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it was required for Windows. |
||
return import(file); | ||
} else { | ||
throw err; | ||
} | ||
|
@@ -25,7 +22,7 @@ const requireOrImport = async file => { | |
exports.loadFilesAsync = async (files, preLoadFunc, postLoadFunc) => { | ||
for (const file of files) { | ||
preLoadFunc(file); | ||
const result = await requireOrImport(file); | ||
const result = await exports.requireOrImport(path.resolve(file)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the file in question is a package name, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. |
||
postLoadFunc(file, result); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work for packages?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should,
fs.existsSync
will return false for a package name, so it is not resolved at all (passed raw) to requireOrImport (e.g.requireOrImport('@babel/register')
This behavior is unchanged in this PR