Skip to content

@babel/preset-typescript: have a flag disable rewriteImportExtensions for dynamic imports #17114

@eXory2024

Description

@eXory2024

💻

  • Would you like to work on this feature?

What problem are you trying to solve?

I use rewriteImportExtensions to have babel automatically convert my static imports from .mts to .mjs.

However, I had to realize that @babel/preset-typescript also applies this to dynamic imports:

const something = await import(path.join(someVariable, "./myFile.mts"))

Which effectively results in:

const something = await import(path.join(someVariable, "./myFile.mjs"))

Which isn't what I expected babel to do since it's a dynamic import.

However, I fully understand that there's a valid use case to automatically have babel rewrite dynamic imports.

Since newer versions of node support running TypeScript files directly (i.e. where it is allowed to have an .mts file extension), I would like to propose an additional flag that controls whether dynamic imports should be rewritten (or left alone) as well.

Describe the solution you'd like

A flag like rewriteDynamicImportExtensions that controls whether dynamic imports should be rewritten or not.

The default value could be set to whatever rewriteImportExtensions is set to, so current users will have the same behaviour as before. This means it's a non-breaking change for existing users, but allows users like me to opt out of the feature.

Describe alternatives you've considered

The alternative is to set rewriteImportExtensions to false and do my own rewriting that ignores dynamic imports.

Documentation, Adoption, Migration Strategy

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions