-
-
Notifications
You must be signed in to change notification settings - Fork 45
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
feat: Add ES6 module export #680
Conversation
Hi @spocke, thanks for this great PR! This will already be a breaking change, but I am trying to keep upgrading as simple as possible. That means that I'd like to avoid any changes that aren't necessary for the ES modules conversion. Could you revert eg. removing default exports? |
It pretty much compiles to the same thing this is what the default exports index.js looks like:
This is what the new index.js file looks like:
Unfortunately rolling it back doesn't work for our use case. We use this with node and .mjs files and there the modules doesn't support the default property at least I couldn't get it to run without getting undefined functions. Also not sure how bundlers deal with export default and tree shaking when the default is a object they tend to not tree shake since it's then a mutable thing so much harder to track. I could add |
Follow up on that mjs issue to reproduce that issue if you use:
With the default exports approach my node 16 throws this error: However with the version in this PR it doesn't produce that error. However the mixed default and direct exports seems to not produce any errors in mjs files so I guess it just favors the es6 modules. |
As far as I can tell nobody imports |
@@ -1,3 +1,3 @@ | |||
export * from "./parse"; | |||
export { default as parse } from "./parse"; |
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.
Won't imports have to be updated to include a .js
extension?
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.
Technically yes in order to get these to work in a browser it needs to have an extension. However both eslink-pluign-node and jest breaks if I change this to have .js extensions.
There is a long thread here:
microsoft/TypeScript#16577
Microsoft don't want to convert these paths to js paths on the fly but they seem to support them now however the rest of the tools for linting and testing here doesn't seem to suppot that. There are workaround for this that for example transpile the js files before being pushed to npm. I've seen people having scripts that just regexp replace this in source. It all depends on how much one want it to work with js modules in the browser. Bundlers will look for default file extensions so this is only an issue if you are loading scripts with <script type="module" src="main.js"></script>
.
@@ -45,7 +48,10 @@ | |||
}, | |||
"license": "BSD-2-Clause", | |||
"jest": { | |||
"preset": "ts-jest" | |||
"preset": "ts-jest", |
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.
Is there a way to test the ESM version of the module as well? Eg. if a __dirname
was missed somewhere — would that be caught?
"target": "ES2019", | ||
"module": "es2015", | ||
"outDir": "lib/es", | ||
"moduleResolution": "node" |
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.
Does moduleResolution
have to be set here?
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.
If you don't specify node it will classic if you change module to es2015.
Described here: https://www.typescriptlang.org/docs/handbook/module-resolution.html#module-resolution-strategies
Left some comments. Happy to go forward with this, I'm just trying to understand possible gotchas. |
Thanks @spocke! Using this as an opportunity to modernize the library a bit more, will get a new release out soon. |
This PR adds es2015 module support for exports it also compiles those modules in ES2019 syntax format.
I moved the types to a separate file since that made it easier to import them all and re-export them I changed the index so that it's more explicit what functions are being exported.
Had to add roots to jest because it was trying to run the es6 modules though node and that was breaking not sure why not that familiar with jest.
I also added
sideEffects: false
to the package.json since this module doesn't have any sideeffects from importing a module that I can see so that should reduce the bundle size if you only use the parser for example.