Skip to content
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

Add ESM support #214

Merged
merged 1 commit into from
Jun 3, 2020
Merged

Add ESM support #214

merged 1 commit into from
Jun 3, 2020

Conversation

snoack
Copy link
Contributor

@snoack snoack commented May 27, 2020

.eslintignore Outdated Show resolved Hide resolved
var exported = require(env.configPath);
requireOrImport(env.configPath, function(err, exported) {
if (err) {
console.error(err);
Copy link
Contributor Author

@snoack snoack May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, if require() failed we got an unhandled exception on the module level. So I use console.error()+exit() here to mimick the old behavior as close as possible, or would you rather log this error differently? (I noticed that you generally seem to use gulplog).

Also I'm aware that I still have to do similar changes to the other lib/versioned/*/index.js scripts. But I'd first like to get your feedback on the general approach before moving forward. What do you think? Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works well! Good solution :)

importESM = new Function('id', 'return import(id);');
} catch (e) {
importESM = null;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js <10 errors out with a SyntaxError when loading a script that uses import(). So I dynamically create a function for that and catch the SyntaxError. That way we can keep supporting all Node.js versions all the way back to 0.10.

@snoack snoack changed the title WIP: ESM support Add ESM support May 28, 2020
index.js Outdated Show resolved Hide resolved
cjs = require(path);
} catch (e) {
if (pathToFileURL && importESM && e.code === 'ERR_REQUIRE_ESM') {
var url = pathToFileURL(path);
Copy link
Contributor Author

@snoack snoack Jun 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added this as I realized that import() fails if providing a Windows file path.

@phated
Copy link
Member

phated commented Jun 3, 2020

This is absolutely amazing! Thank you for all the work you've done here 🙇

I'm going to get this merged and add some of your comments from GitHub into the source for "future us"

I believe I also need to add the .mjs extension to the 1.x and 2.x release streams of interpret.

@phated phated merged commit 15d0648 into gulpjs:master Jun 3, 2020
@phated
Copy link
Member

phated commented Jun 3, 2020

Sorry this is taking so long. I didn't realize getting the .mjs extension supported (and tested) properly in interpret would take so long. I'm hoping to have this released before I close my laptop for the night.

@phated
Copy link
Member

phated commented Jun 3, 2020

I just pushed up the remaining changes that needed to land for this to work with interpret/rechoir/liftoff and autodetect the extension. However, CI needs to run and it's super late for me, so I will have to get this released tomorrow afternoon.

Sorry for the delay.

@phated
Copy link
Member

phated commented Jun 3, 2020

Running into some issues with ESM support on node 10 for Windows. Need to dig into that.

@snoack
Copy link
Contributor Author

snoack commented Jun 3, 2020

Do you have more details? Tests pass on my Windows machine with Node.js 10.21.0.

@phated
Copy link
Member

phated commented Jun 3, 2020

@phated
Copy link
Member

phated commented Jun 3, 2020

Of course I create a branch and start debugging and it starts working 🤷.

@snoack
Copy link
Contributor Author

snoack commented Jun 3, 2020

FWIW, I was not able to reproduce this locally on Windows 10, not even with Node.js 10.20.1 (32-bit), the exact same version you got on Appveyor.

However, a quick web search for 3221225477 (the exit status Node.js was failing with on Appveyor) indicates some kind of crash likely related to environmental issues.

@phated
Copy link
Member

phated commented Jun 4, 2020

Thanks for digging in! It definitely seems like a weird issue with AppVeyor. Anyway, this has just been released as v2.3.0! Thanks for all your work here 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants