-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Enhancement: Support .mjs & .cjs extensions #65
Comments
@phated This will definitely be a breaking change as you cannot Thus you would have to use That would require to break backwards compatibility because e.g. in Node.js 12 |
That’s incorrect, as |
@ExE-Boss you must have missed this in the node docs:
Gulp uses require to load files, so cjs won't "just work". |
That is factually incorrect, as
This is because |
Interpret is used to register require extensions and doesn't allow for node's fallback behavior. I also believe node didn't used to have that fallback behavior, but I'd need to check when I'm not on mobile. |
Node has had the fallback to |
That is wrong because even with interpret you are still calling node's |
I have recently seen an issue on storybook at storybookjs/storybook#9854 However, by adding the following in interpret '.mjs': [
{
module: '@babel/register',
register: function(hook) {
hook({
extensions: '.mjs',
rootMode: 'upward-optional',
ignore: [ignoreNonBabelAndNodeModules],
});
},
},
], And also I might be ignorant about this but shouldn't that already help? |
@yannbf That attempt still relies on a transpiler (in this case babel) to work. We can already do that using the esm package. What this issue is about, however, is leveraging Node's native support for esm modules so that no transpiler is necessary. |
Hey @Haringat got it! Thanks for explaining. |
As for backwards compatibility, at least in Node.js 10.19.0 and 12.16.2, function loadGulpFile(filename) {
try {
return Promise.resolve(require(filename));
}
catch (e) {
return import(filename).catch(() => { throw e; });
}
} |
@snoack whoa! great information, I think that is definitely something we need to investigate. Want to help out? |
I already had a quick look at |
@snoack yep, that's right here: https://github.com/gulpjs/gulp-cli/blob/master/index.js#L204-L206 |
Thanks, but it seems that a version-specific wrapper is imported there, while the Gulpfile itself is imported from that wrapper (e.g. here). Do I understand that correctly? Would it be a problem to make that wrapper asynchronous (required to import ES modules in there)? Also where is BTW, what is the target in terms of backwards compatibility? I was under the impression that Node.js 10 (the oldest still supported LTS version) is as far back as anyone cares about backwards compatibility. But looking at your CI, I'm impressed to see tests passing on versions as old as Node.js 0.10. Do I see that right? It's not necessarily a deal breaker, but I might have to look some more into backwards compatibility if you plan to keep supporting all of those versions of Node.js. |
@snoack supporting this will need to require 10+, which will require a major bump in gulp-cli. People will be able to install the major version globally for backwards-compat with gulp, but it will also ship with gulp 5. You are correct that gulp is loaded there and it is determined by Liftoff. If interpret defines |
This is more correct, as it ensures that the correct error is reported: function loadGulpFile(filename) {
try {
return Promise.resolve(require(filename));
}
catch (errRequire) {
return import(filename).catch(errImport => {
if (errRequire.code === "ERR_REQUIRE_ESM") {
throw errImport;
}
throw errRequire;
});
}
} |
@phated I had a go. It's still WIP, but I could already use some feedback. BTW, it seems I found a way to keep things backwards-compatible all the way back to Node.js 0.10. |
😍 Oh my, this would make you my favorite person! I won't have time to review tonight, but hoping to check it out soon. |
I finished up my pull request (with test case and everything). Feel free to review. |
@phated, I skimmed through afe7c29. But I am not familiar enough with I just want to clarify on Node.js >=10.15.3, I'd expect |
@snoack if the |
Yeah, that is what is confusing me. Why not simply setting If some project has a Anyway, as long as |
@snoack yeah, unfortunately the reasoning is buried under levels of indirection. The |
Testing this locally now and it is working. Needed to add 1 more update to your changes and should be good. |
@snoack sure, but for the sake of fun, open a node REPL on node 10.21 like |
Awesome, well appreciated! (Curious about that change though.) |
Gulp supports the `.mjs` file extension for config files (tracked at gulpjs/interpret#65). It looks like `.cjs` is not supported yet (gulpjs/interpret#68).
Gulp supports the `.mjs` file extension for config files (tracked at gulpjs/interpret#65). It looks like `.cjs` is not supported yet (gulpjs/interpret#68).
Supersedes #50 & #59 - Please see them for additional context
We currently have support for ESModules using the
.esm.js
extension and theesm
module to transpile them on the fly. However, node 13 (and the upcoming 14) support the.mjs
and.cjs
extensions. I've run into a bunch of troubles reviewing, interpreting, and testing those features, but I'd like to support them.I'd love some help here @ulrichb @cedx or anyone else with experience.
One other note: I believe this will be a breaking change because it seems the
.cjs
extension can't be used outside a.mjs
instance.The text was updated successfully, but these errors were encountered: