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

Rewrite relative import extensions with flag #59767

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Aug 26, 2024

This PR adds --rewriteRelativeImportExtensions to support transforming module specifiers from e.g. "./utils.ts" to "./utils.js". A module specifier in an import declaration, export declaration, or import assignment (import foo = require("...")) will be rewritten during JS emit if it meets all of these criteria:

  • it begins with ./ or ../
  • it does not end in a declaration file name (.d.ts, .d.mts, .d.cts, .d.*.ts)
  • it ends in .ts, .tsx, .mts, or .cts

Errors are issued in an attempt to catch common mistakes:

  • A module specifier will get rewritten but will break after rewriting:
    • "./foo.ts" actually resolves to ./foo.ts.ts or foo.ts/index.ts
    • "../other-project/src/index.ts" belongs to another project where outputs go to a different outDir
  • A module specifier will not get rewritten but will break after emitting as-is:
    • "#blah/foo.ts" resolves to foo.ts where the .ts matches through a wildcard

TODO:

  • Rewriting/shimming of async import
  • Rewriting/shimming of require in JS input files?

Fixes #59597

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Aug 26, 2024
@andrewbranch
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 26, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 26, 2024

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/163457/artifacts?artifactName=tgz&fileId=2225118E5057943A4B80A3A9D8FE2EA5696226BA40FE09011C7C2241BF80844302&fileName=/typescript-5.7.0-insiders.20240826.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@andrewbranch andrewbranch changed the title [experiment] Rewrite relative import extensions with flag Rewrite relative import extensions with flag Sep 11, 2024
@microsoft microsoft deleted a comment from typescript-bot Sep 17, 2024
@andrewbranch
Copy link
Member Author

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 17, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
pack this ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 17, 2024

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/163603/artifacts?artifactName=tgz&fileId=366D2974C150D261A1702BA0C2511DBAF6432824433FEBBEB4D1A438DE1953C602&fileName=/typescript-5.7.0-insiders.20240917.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite imports with ".ts" extension to ".js" to support Nodejs's "--experimental-strip-types"
2 participants