-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Rewrite relative import extensions with flag #59767
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
Conversation
|
@typescript-bot pack this |
|
Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your and then running There is also a playground for this build and an npm module you can use via |
7159132 to
2b06e62
Compare
|
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. |
bf25d52 to
a398ad1
Compare
ef2101a to
0d95ce2
Compare
|
Hey, I was reading the table in the PR description and I'm getting confused by these two lines:
Does it mean that |
|
Yes. Plain |
## Summary <!-- Succinctly describe your change, providing context, what you've changed, and why. --> Ensures support for `typescript@~5.7.0` by adding a new PR check. See [Announcing TypeScript 5.7 - TypeScript](https://devblogs.microsoft.com/typescript/announcing-typescript-5-7/). Ideally we would take advantage of the new `rewriteRelativeImportExtensions` added in microsoft/TypeScript#59767 which would allow us to correctly import `.ts` extensions instead of requiring `.js`, ready for better Deno/Bun support, as well as Node support when running `.ts` files directly. Unfortunately, `typescript<5.0.0` doesn't support the compiled output; we'll have to wait for v4 to use that. ## Checklist <!-- Tick these items off as you progress. --> <!-- If an item isn't applicable, ideally please strikeout the item by wrapping it in "~~"" and suffix it with "N/A My reason for skipping this." --> <!-- e.g. "- [ ] ~~Added tests~~ N/A Only touches docs" --> - [ ] ~Added a [docs PR](https://github.com/inngest/website) that references this PR~ N/A KTLO - [x] Added unit/integration tests - [x] Added changesets if applicable ## Related - https://devblogs.microsoft.com/typescript/announcing-typescript-5-7/ - microsoft/TypeScript#59767
|
Just out of curiosity, I’m facing an issue where my .d.ts files don’t have an extension in the path. For example: export * as v1alpha2 from './v1alpha2/index.gen';. Why is the .ts extension mandatory to transform it into .js? Thank you. |
While |
| text: ` | ||
| var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) { | ||
| if (typeof path === "string" && /^\\.\\.?\\//.test(path)) { | ||
| return path.replace(/\\.(tsx)$|((?:\\.d)?)((?:\\.[^./]+?)?)\\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a path contains .jsx it is preserved even with the preserveJsx argument.
Should the regex be /\.([jt]sx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i?
This now works:
console.log(rewriteRelativeImportExtension("./main.tsx"));
console.log(rewriteRelativeImportExtension("./main.ts"));
console.log(rewriteRelativeImportExtension("./main.jsx"));
console.log(rewriteRelativeImportExtension("./main.jsx", true));
console.log(rewriteRelativeImportExtension("./main.js"));./main.js
./main.js
./main.js
./main.jsx
./main.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you clarify your actual behavior and expected behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I'm writing a plugin that is based on __rewriteRelativeImportExtension. For Typescript is fine, however for esbuild the .jsx can the transformed to .js.
|
@andrewbranch @RyanCavanaugh Shouldn't calls to |
This PR adds
--rewriteRelativeImportExtensionsto support transforming module specifiers from e.g."./utils.ts"to"./utils.js". A module specifier will be statically rewritten or shimmed during JS emit if it meets all of these criteria:./or../.d.ts,.d.mts,.d.cts,.d.*.ts).ts,.tsx,.mts, or.ctsError checking
Errors are issued in an attempt to catch common mistakes:
"./foo.ts"actually resolves to./foo.ts.tsorfoo.ts/index.ts"../other-project/src/index.ts"belongs to another project where outputs go to a different outDir"#blah/foo.ts"resolves tofoo.tswhere the.tsmatches through a wildcardSyntax support
import "./foo.ts"import "./foo.js",require("./foo.js")export * from "./foo.ts"export * from "./foo.js"import foo = require("./foo.ts")import foo = require("./foo.js")import("./foo.ts")import("./foo.js")require("./foo.ts")require("./foo.js")import(getPath())import(__rewriteRelativeImportExtension(getPath()))require(getPath())require(__rewriteRelativeImportExtension(getPath()))import("foo")import("foo")require("foo")require("foo")require("./foo.ts")require("./foo.ts")(require)("./foo.ts")(require)("./foo.ts")Limitations and future work
This PR doesn’t change how module resolution works, so it’s still possible to reference a
.tsfile by a.jsmodule specifier:We discussed that it’s probably desirable to turn this functionality off under
--rewriteRelativeImportExtensions(and--allowImportingTsExtensions), since these options are a strong indicator that the user is going to run the input TS files in a TS-native runtime like Bun ornode --experimental-strip-types. That would be a breaking change and isn’t included in this PR.tslib PR: microsoft/tslib#270
Fixes #59597