-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
rewriteImportExtensions
option to TS preset (#15913)
* Add `rewriteImportExtensions` option to TS preset * Review * Update snapshot
- Loading branch information
1 parent
d11682e
commit 54d30f2
Showing
7 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/babel-preset-typescript/src/plugin-rewrite-ts-imports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import type { types as t } from "@babel/core"; | ||
import type { NodePath } from "@babel/traverse"; | ||
|
||
export default declare(function ({ types: t }) { | ||
return { | ||
name: "preset-typescript/plugin-rewrite-ts-imports", | ||
visitor: { | ||
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"({ | ||
node, | ||
}: NodePath< | ||
t.ImportDeclaration | t.ExportAllDeclaration | t.ExportNamedDeclaration | ||
>) { | ||
const { source } = node; | ||
const kind = t.isImportDeclaration(node) | ||
? node.importKind | ||
: node.exportKind; | ||
if (kind === "value" && source && /[\\/]/.test(source.value)) { | ||
source.value = source.value.replace(/(\.[mc]?)ts$/, "$1js"); | ||
} | ||
}, | ||
}, | ||
}; | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/babel-preset-typescript/test/fixtures/opts/rewriteImportExtensions/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import "./a.ts"; | ||
import "./a.mts"; | ||
import "./a.cts"; | ||
import "a-package/file.ts"; | ||
// Bare import, it's either a node package or remapped by an import map | ||
import "soundcloud.ts"; |
4 changes: 4 additions & 0 deletions
4
packages/babel-preset-typescript/test/fixtures/opts/rewriteImportExtensions/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"sourceType": "module", | ||
"presets": [["typescript", { "rewriteImportExtensions": true }]] | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/babel-preset-typescript/test/fixtures/opts/rewriteImportExtensions/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import "./a.js"; | ||
import "./a.mjs"; | ||
import "./a.cjs"; | ||
import "a-package/file.js"; | ||
// Bare import, it's either a node package or remapped by an import map | ||
import "soundcloud.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters