-
Notifications
You must be signed in to change notification settings - Fork 207
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
esm output is invalid #1941
Comments
Thanks for reporting this @samuelstroschein Adding the I wonder what is the mid 2023 best practice for publishing a package which is both ESM + commonjs compatible? |
I found this: https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html But their solution (compiling twice) seems a bit complicated (e.g: generating package.json variants from a template). |
Thoughts while I am on the train:
The code seems to be tiny/can be bundled with the main package itself to ease compilation/deployment etc.
It seems like adding exports to all packages (which can contain both ESM and CJS exports) and using two TypeScript configs is sufficient. |
The project started as a large single package, and it seems I was trying to slowly split it up into sub-components.
This would probably work as I don't believe any of the sub-packages have module level state that would be duplicated (unlike the root package). But this will also add complexity as the project already uses a different approach (gen-esm-wrapper) for the root project. Perhaps it is time to re-evaluate being an ESM only module. But that would require more work (e.g adjusting the old website code to work with ESM) |
The new package.exports field allows sub-nesting. "exports": {
"./utilities": "./src/utilities/index.js"
"./": "./src/index.js"
} import * as utilities from "chevrotain/utilities" What approach would you prefer if I open a PR?
I double checked NPM. Not a single sub-package has a dependent that is not chevrotain itself. I take that as an indication that publishing the sub-packages like utils, types, etc. has no benefit to the community. Moving the sub-packages into chevrotrain should ease the maintenance and be a trivial change but with a slight risk that someone somewhere on the web directly imports a sub-package outside of chevrotain. EDIT: 1 seems more complicated than thought because there appears to be a global build script/I can't simply change the build script on a package level. |
I found this post to be useful on how to support both module systems: https://evertpot.com/universal-commonjs-esm-typescript-packages/ |
Thanks for the article @mattbishop My problem with the double compilation approach is that each compiled version would have its own "state" (file level) and that can cause issues. There are already some edge cases where Chevrotain's "file level" state can cause issues: I wonder how relevant is going ESM only nowadays for library authors? |
None to be honest 😢 ,
And I'd rather slowly extract more logic out of the root chevrotain packages into sub-packages instead of adding more inside the monolith. |
The "perfect" solution would be to switch to ESM only.
A possible alternative would be to implement the ESM wrapper approach used in the root chevrotain package for the sub-packages as well. While this library has not been updated in a while... using the same approach as in the root chevrotain package |
What are you trying to achieve with sub-packages?
Adding The sub-packages are invalid JS packages but valid TS packages. TypeScript, and thus esbuild, should be able to resolve TypeScript if a package uses TypeScript itself. |
I don't quite understand what is a "valid TypeScript package, because TypeScript is a design-time only tool. {
"exports": {
"types": "./lib/src/api.d.ts",
"require": "./lib/src/api.js",
}
} But do note that there is no esm output for those sub-packages so you can't add the If you can solve the problem by modifying your esbuild configuration, that may be a valid workaround. |
Modern bundler settings require the exports field to be present. See Chevrotain#1941
@bd82 Thank you for maintaining chevrotain and responding to my inquiries. I opened a PR that addresses the issue and has been tested on my local machine #1943 and bd82/regexp-to-ast#130. Do you have the time to merge and publish the PRs? No response is needed for the stuff below. Just trying to help you reduce the maintenance work on Chevrotain.
You seem to be optimizing for something else than maintainability/ease of deployment.
|
Modern bundler settings require the exports field to be present. See #1941
I've merged #1943
Yeah the transition into a (partial) mono-repo was done at a time I was investing more time into Chevrotian... Although it does seem to help in pulling in regexp-to-ast from the other repo (reduce number of repos being maintained) in this case. |
Closing this because merged. Thank you @bd82 ! |
/"neutral" leads to errors if an npm module does not have an "exports" field see Chevrotain/chevrotain#1941 . also typescript itself is not compatible with "neutral"
Problem
The ESM output seems to be invalid. Esbuild throws an error upon bundling chevrotain in an application for
platform: neutral
:Proposal
The bug seems to be an easy fix by adding "exports" to the package.json file. This issue is quite urgent for us, would you accept a PR from me?
The text was updated successfully, but these errors were encountered: