Skip to content

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

Open

Description

πŸ”Ž 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

Activity

  1. jakebailey commented on Nov 26, 2024

    @jakebailey
    Member

    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

  2. bradzacher commented on Nov 26, 2024

    @bradzacher
    ContributorAuthor

    Looks like you might be right.

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

    playground

    This also correctly transforms the extension.

  3. 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[/+] on Nov 26, 2024
  4. jakebailey commented on Nov 26, 2024

    @jakebailey
    Member
  5. jakebailey commented on Nov 26, 2024

    @jakebailey
    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?)

  6. bradzacher commented on Nov 26, 2024

    @bradzacher
    ContributorAuthor

    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:).

  7. jakebailey commented on Nov 26, 2024

    @jakebailey
    Member

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

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

Metadata

Metadata

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions