-
-
Notifications
You must be signed in to change notification settings - Fork 878
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 option to generate ESM exports instead of CJS (#1523) #1861
Add option to generate ESM exports instead of CJS (#1523) #1861
Conversation
This is great, thank you! Please see comments - let me know what you think |
Okay cool, this will actually be my first Open Source PR. I have done a few things here or there, but this one feels much bigger and certainly the one that took the most time 😅. Great repo by the way, good tests, linting and safeguards. I did struggle a little bit to get it going Windows, but managed with WSL (used to many projects only supporting Linux development) |
…r#1523) - Renamed exportEsm to esm - Extracted common code - Changed invalid export names to rather throw an error
Changes applied, let me know what you think |
Thank you, this is a very solid piece of work - merged, release is coming. |
Pleasure. On the docs and additional PR, I am busy writing/rewriting this https://ajv.js.org/standalone.html doc to have that concrete example I mentioned. I am also including knowledge/the options about ES5/ES6 and CJS/ESM exports + examples. Might be able to get it done tomorrow night, otherwise only sometime next week (mid-week) - Just for if you want to hold off on the release for this doc change. |
Releases just happen, no problem if it’s after that. |
What issue does this pull request resolve?
Adds an option so that the generated code can do both CJS and ESM exports. #1523
What changes did you make?
Adde an extra flag
code.exportEsm
to generate code with ESM exports instead of CJS.Is there anything that requires more attention while reviewing?
I wanted to use the
code.es5
flag and then make the assumption that ES5 code uses CJS and ES6 uses ESM. But this would have been a breaking change because by default the code is exporting as ES6. So I thought it best to add an additional flag calledcode.exportEsm
. It defaults to false so it introduces no breaking changes.CJS (default) creates exports as
module.exports =
for single exports andexports.
orexports[
for multiple exports. ESM creates exports asexport const
andexport default
for single exports, where theexport const
has been given a fixed name ofvalidate
. This is just to cater for both import methods. Then for the multiple exports, theexport default
option is not available(or has not been implemented) and must be done with named exports as inexport const
.I tested both scenarios for when the code has a single and multiple exports. Note that ESM can not export with special characters in the name, CJS can because it then uses the array syntax like
exports[https://example.com/string.json]
. I didn't just want it to fail the ESM version when the CJS passed so instead I used some regex to replace special characters and then the ESM export would look likeexport const https___example_com_string_json
I also needed to add a new devDependencies called
module-from-string
to be able to test the generated code. It is similar torequire-from-string
. The package is almost a year old with not a lot of stars or downloads but looks stable and was good enough for the tests. I am not sure how else to test ESM modules, open to suggestions if you do not like this method.