-
Notifications
You must be signed in to change notification settings - Fork 6
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
Incorrect ESM build #10
Comments
Thanks for reporting. I think it might have to do with the recent changes regarding importing JSON in the rollup config. I should have tested this better. I will try some things out. |
I feel you are so close! So far the way I managed to make it work for both commonjs and ESM in both javascript and typescript, you need to output 2 Here's what the root package.json could look like {
"exports": {
".": {
"types": {
"import": "./dist-es/index.d.ts",
"require": "./dist/index.d.ts"
},
"import": "./dist-es/rand-seed.mjs",
"require": "./dist/rand-seed.js",
"default": "./dist-es/rand-seed.mjs"
}
},
"main": "dist/rand-seed.js",
"module": "dist-es/rand-seed.mjs",
"files": [ "dist", "dist-es" ],
"types": "dist/index.d.ts",
} I recommend you test your changes in a fork of my repro, it should be a good indicator if you got it working |
What a headache this is... Anyway, I think I've finally managed to solve it now. I tried the latest release in your codesandbox repro and everything seems to run / build now. Please let me know if there's still something not working for you and thanks for the help with this @Cellule! |
I agree, this is a horrible nightmare that we can't seem to wake up from I feel you went 1 step in the right direction by converting to ESM first. Here's what I think can solve this once and for all (I hope)
// dist-cjs/index.d.ts
// @ts-expect-error -- ESM/CJS warning
export * from "../dist/index.js"
// @ts-expect-error -- ESM/CJS warning
export {default} from "../dist/index.js"
// ^ This should tell typescript to shut up about the "incompatibility" error where a commonjs file is trying to import an ESM one // dist-cjs/package.json
{ "type": "commonjs" }
// ^ This overrides the default type of the package for the index.d.ts file in dist-cjs
{
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist-cjs/index.d.ts", // Notice how typings for commonjs points to dist-cjs instead of dist
"default": "./dist/index.cjs"
}
}
},
"type": "module",
"files": [
"dist",
"dist-cjs" // don't forget to include this otherwise the folder won't be published
],
} |
Could you tell me what the error is you are getting? I created a simple sandbox of my own, testing out the CJS build for a typescript project and it seems to run without any problems now, unless I'm missing something? |
Yeah I updated my sandbox https://codesandbox.io/p/devbox/confident-orla-xy9d9n I get 2 errors
first issue is because typescript thinks the commonjs file I can't access your devbox (make sure it's public in the "share" section) |
You should be able to acces it now. I think your issues might be because the "module" property in your tsconfig.json is set to "NodeNext". I've set it to "CommonJS" in my devbox example and that seems to work. |
Yeah, but "NodeNext" is a valid option even in commonjs |
Fair point. In fact, it seems the TypeScript documentation recommends it for modern projects. I'll have another go at it when I have time. I'd like to find a solution for this, but boy oh boy, this is not very fun stuff. I'm using Rollup for bundling / transpiling, so ideally I find a way to make this all work purely using that instead of having to manually create any "patching" files myself. Or maybe it's time to look for another tool for bundling that makes this all a little easier (if such a thing exists). |
OK, feel like I am another step closer. 3 of the 4 builds work now at least with v2.1.4. |
There are a few issues with the current ESM build output
See example of errors here https://codesandbox.io/p/devbox/confident-orla-xy9d9n?file=%2Findex.js
At runtime it seems it's not loading the .es.js version of the package
If I add
Then it correctly loads rand-seed.es.js. However since the package is commonjs, I then get other errors.
Would need to isolate rand-seed.es.js in another folder and add another package.json with just
{ "type": "module" }
in it.Plus in typescript I get the following error
I'm not sure how to fix all of this with rollup, so far I've only done dual commonjs/esm build with only typescript
Here's what I use for instance where the package is primarily ESM then I isolate the commonjs build
The text was updated successfully, but these errors were encountered: