GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Rewrite relative import extensions with flag by andrewbranch · Pull Request #59767 · microsoft/TypeScript Conversation
|
| Scenario | Example input | Operation | Example output |
|---|---|---|---|
| Import declarations | import "./foo.ts" |
Rewrite | import "./foo.js", require("./foo.js") |
| Export declarations | export * from "./foo.ts" |
Rewrite | export * from "./foo.js" |
| Import equals | import foo = require("./foo.ts") |
Rewrite | import foo = require("./foo.js") |
| Import calls with qualifying literal argument | import("./foo.ts") |
Rewrite | import("./foo.js") |
| Require calls in JS with qualifying literal argument | require("./foo.ts") |
Rewrite | require("./foo.js") |
| Import calls with non-literal argument | import(getPath()) |
Shim | import(__rewriteRelativeImportExtension(getPath())) |
| Require calls in JS with non-literal argument | require(getPath()) |
Shim | require(__rewriteRelativeImportExtension(getPath())) |
| Import calls with non-qualifying literal argument | import("foo") |
None | import("foo") |
| Require calls with non-qualifying literal argument | require("foo") |
None | require("foo") |
| Require calls in TS | require("./foo.ts") |
None | require("./foo.ts") |
| Indirected require calls | (require)("./foo.ts") |
None | (require)("./foo.ts") |
This PR doesn’t change how module resolution works, so it’s still possible to reference a .ts file by a .js module specifier:
// @Filename: a.ts export {}; // @Filename: b.ts import {} from "./a.js"; // Always ok
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 or node --experimental-strip-types. That would be a breaking change and isn’t included in this PR.
tslib PR: microsoft/tslib#270
Fixes #59597
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TypeScript Bot (@typescript-bot) pack this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Starting jobs; this comment will be updated as builds start and complete.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Hey Andrew Branch (@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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 Daniel Rosenwasser (@DanielRosenwasser) and Ryan Cavanaugh (@RyanCavanaugh) are aware of the changes, just as a heads up. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Yes. Plain |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
## 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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ryan Cavanaugh (@RyanCavanaugh)
While |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 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.
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
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Andrew Branch (@andrewbranch) Ryan Cavanaugh (@RyanCavanaugh) Shouldn't calls to |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Rewrite imports with ".ts" extension to ".js" to support Nodejs's "--experimental-strip-types"
There was an error while loading. Please reload this page.