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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ES Modules in @babel/preset-typescript #14219

Closed
1 task
LandonSchropp opened this issue Feb 1, 2022 · 3 comments
Closed
1 task

Support ES Modules in @babel/preset-typescript #14219

LandonSchropp opened this issue Feb 1, 2022 · 3 comments
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@LandonSchropp
Copy link

馃捇

  • Would you like to work on this feature?

What problem are you trying to solve?

Apologies if this is captured elsewhere. I'm trying to sort out all of the moving parts with configuring Babel/TypeScript, so I may have missed something.

I'm setting up a TypeScript Node.js project, and I'd like to be able to do the following:

  • Use TypeScript.
  • Use the import/export syntax in my files.
  • Use top-level await (requires "type": "module" in package.json)
  • Use Jest (requires Babel)

Describe the solution you'd like

Ideally, I'd be able to have one TypeScript configuration and use that for everything, including running my project, type checking, and running my tests. Here's what I'd expect the configuration to look like:

// babel.config.json
{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-typescript"
  ]
}
// package.json
{
  "type": "module",
  ...
}

Describe alternatives you've considered

Prior to adding Jest as a requirement, I was using ts-node. However, with Jest, I need to be able to use Babel. Ideally, I'd be able to use the same configuration and tools for everything, to ensure the code works consistently.

Documentation, Adoption, Migration Strategy

No response

@babel-bot
Copy link
Collaborator

Hey @LandonSchropp! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

@nicolo-ribaudo
Copy link
Member

Hi! Sorry this issue didn't get noticed 馃檪

I think all the four requirements you have should work, but you might need to tell Babel not to compile ESM to CJS:

{
  "presets": [
    ["@babel/preset-env", { "modules": false }],
    "@babel/preset-typescript"
  ]
}

However, you will need to compile your files ahead of time (using @babel/cli): Babel does not support running "at runtime" yet like ts-node when using native ESM, because Node.js doesn't have a stable ESM loader API yet.

@LandonSchropp
Copy link
Author

Not a problem at all! Thanks for the reply. 馃檪

Ah, I wasn't aware of the modules setting in preset-env. That's really helpful to know.

Babel does not support running "at runtime" yet like ts-node when using native ESM, because Node.js doesn't have a stable ESM loader API yet.

That makes sense. Fingers crossed that lands sometime soon.

Thanks!

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 19, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants