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

Rollup throws TRANSPILED_ESM_CONFIG error when importing ESM module #4617

Closed
flying-sheep opened this issue Aug 21, 2022 · 14 comments · Fixed by #4549
Closed

Rollup throws TRANSPILED_ESM_CONFIG error when importing ESM module #4617

flying-sheep opened this issue Aug 21, 2022 · 14 comments · Fixed by #4549

Comments

@flying-sheep
Copy link

Rollup Version

2.78.1

Operating System (or Browser)

Linux/Firefox

Node Version (if applicable)

No response

Link To Reproduction

https://replit.com/@flying-sheep/rollup-repro-esm-from-ts

Expected Behaviour

rollup.config.ts gets compiled to ESM and imports <projdir>/node_modules/@mdx-js/rollup/index.js as ESM

Actual Behaviour

rollup.config.ts seems to get compiled to CJS despite target: ES2020 in the tsconfig.json.

@lukastaegert
Copy link
Member

I very much recommend reading the documentation for this one: https://rollupjs.org/guide/en/#using-untranspiled-config-files

Rollup CLI will also print this link to the documentation next to the error.

@lukastaegert
Copy link
Member

lukastaegert commented Aug 21, 2022

As it is TypeScript, the only way to support this is via transpilation to CommonJS, unfortunately. The problem is that in order to run the transpiled file in position of the original file, we rely on a hack of Node's CommonJS API. The corresponding ESM API is still very experimental and cannot be used productively as far as I know, nor is it supported in any but the latest Node versions. You can however place your config in another folder with a package.json file with type: "commonjs"

@flying-sheep
Copy link
Author

But wouldn't i still be unable to import that ESM module from commonjs? How can I configure rollup to compile a typescript config to ESM and use that?

@lukastaegert
Copy link
Member

You just cannot at the moment. Currently, the best possibility for us would be to build upon this API https://nodejs.org/api/esm.html#loaders but not only is it experimental, but the need to configure it upfront via Node CLI flags makes it very hard to use.

@lukastaegert
Copy link
Member

One escape hatch could be dynamic imports. I think they do not work at the moment because they are converted to require expressions during transpilation, but we can easily make them work by keeping them untranspiled in Rollup 3 and that would still be valid CommonJS.

@flying-sheep
Copy link
Author

flying-sheep commented Aug 23, 2022

I think I understand the problems here now. On the other hand, the number of ESM only modules will only increase, and I don’t think forcing people to use CJS instead of typescript is a great thing.

It seems impossible to do this at the moment, because rollup always compiles to CJS:

format: 'cjs',

Can you provide a CLI option to allow a workaround?

@flying-sheep flying-sheep changed the title Rollup throws TRANSPILED_ESM_CONFIG error when importing ESM module with type:module in package.json Rollup throws TRANSPILED_ESM_CONFIG error when importing ESM module Aug 23, 2022
@lukastaegert
Copy link
Member

Actually you made me think, and if we accept that we do it a little more slowly, we could just make it work by writing the transpiled file to disk and then dynamically importing it. If this works out, I will change the mechanism for Rollup 3 because you are right, tying ourselves to CommonJS this way is a problem. I will let you know once I have something for you to play around with.

@flying-sheep
Copy link
Author

That’s great, thank you!

@lukastaegert
Copy link
Member

Fix at #4621

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4621 as part of rollup@3.0.0-5. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-5 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4621 as part of rollup@3.0.0-6. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-6 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4621 as part of rollup@3.0.0-7. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-7 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4621 as part of rollup@3.0.0-8. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.0.0-8 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4621 as part of rollup@3.0.0. You can test it via npm install rollup.

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

Successfully merging a pull request may close this issue.

3 participants