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

rewriteRelativeImportExtensions does not rewrite the extension for an import expressions unless the file is detected to be a module #60599

Open
bradzacher opened this issue Nov 25, 2024 · 6 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@bradzacher
Copy link
Contributor

πŸ”Ž Search Terms

rewriteRelativeImportExtensions, import expression

πŸ•— Version & Regression Information

  • This changed between versions 5.7

⏯ Playground Link

https://www.typescriptlang.org/play/?rewriteRelativeImportExtensions=true#code/PTAEEsFsAcHsCcAuoDkA6YAzWs2IM4oDcAsAFBRxIAU6WOehAlOUA

πŸ’» Code

Test One

import('./foo.ts')

Test Two

import './foo.ts';
import('./foo.ts')

πŸ™ Actual behavior

JS Output

Test One

import('./foo.ts') // ❌

Test Two

import './foo.js'; // βœ…
import('./foo.js') // βœ…

πŸ™‚ Expected behavior

JS Output

Test One

import('./foo.js') // βœ…

Test Two

import './foo.js'; // βœ…
import('./foo.js') // βœ…

Additional information about the issue

No response

@jakebailey
Copy link
Member

jakebailey commented Nov 26, 2024

I don't think this has to do with the import being twice, but rather module detection? This one rewrites paths too: https://www.typescriptlang.org/play/?rewriteRelativeImportExtensions=true#code/JYWwDg9gTgLgBAcgHQHoBmEICYkwM4IDcAsAFCiSwAUy6muBAlGUA

@bradzacher
Copy link
Contributor Author

Looks like you might be right.

export {};
import('./foo.js')

playground

This also correctly transforms the extension.

@bradzacher bradzacher changed the title rewriteRelativeImportExtensions does not rewrite the extension for an import expressions unless there is an import declaration for the same module rewriteRelativeImportExtensions does not rewrite the extension for an import expressions unless the file is detected to be a module Nov 26, 2024
@jakebailey
Copy link
Member

@andrewbranch

@jakebailey
Copy link
Member

FWIW I'm not sure that this is incorrect behavior; this transformation was only intended on applying in modules, so if we don't think a file is a module, then it won't happen.

What condition are you running under where the file is not a module?

(Maybe we should rewrite imports in import nodes in scripts, given they can appear there?)

@bradzacher
Copy link
Contributor Author

If you're working in LTS node then the only way to access ESM in CJS is via import expressions.
But it's not a real usecase I had in mind -- I was just testing the feature and this surprised me because there was no mention of it being restricted on the announcement, and there aren't docs yet (https://www.typescriptlang.org/tsconfig/#rewriteRelativeImportExtensions :sad:).

@jakebailey
Copy link
Member

To be clear a "module" to us means "not a global script"; CJS are modules in this scheme.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

4 participants