-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34c8793
commit bfa56c4
Showing
11 changed files
with
76 additions
and
27 deletions.
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
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
50 changes: 39 additions & 11 deletions
50
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 |
---|---|---|
@@ -1,25 +1,53 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import type { types as t, NodePath } from "@babel/core"; | ||
|
||
export default declare(function ({ types: t }) { | ||
export default declare(function ({ types: t, template }) { | ||
function maybeReplace( | ||
source: t.ArgumentPlaceholder | t.SpreadElement | t.Expression, | ||
path: NodePath, | ||
) { | ||
if (!source) return; | ||
if (t.isStringLiteral(source)) { | ||
if (/[\\/]/.test(source.value)) { | ||
source.value = source.value | ||
.replace(/(\.[mc]?)ts$/, "$1js") | ||
.replace(/\.tsx$/, ".js"); | ||
} | ||
return; | ||
} | ||
|
||
path.replaceWith( | ||
template.expression | ||
.ast`(${source} + "").replace(/([\\/].*\.[mc]?)tsx?$/, "$1js")`, | ||
); | ||
} | ||
|
||
return { | ||
name: "preset-typescript/plugin-rewrite-ts-imports", | ||
visitor: { | ||
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"({ | ||
node, | ||
}: NodePath< | ||
t.ImportDeclaration | t.ExportAllDeclaration | t.ExportNamedDeclaration | ||
>) { | ||
const { source } = node; | ||
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"( | ||
path: NodePath< | ||
| t.ImportDeclaration | ||
| t.ExportAllDeclaration | ||
| t.ExportNamedDeclaration | ||
>, | ||
) { | ||
const node = path.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") | ||
.replace(/\.tsx$/, ".js"); | ||
if (kind === "value") { | ||
maybeReplace(node.source, path.get("source")); | ||
} | ||
}, | ||
CallExpression(path) { | ||
if (t.isImport(path.node.callee)) { | ||
maybeReplace(path.node.arguments[0], path.get("arguments.0")); | ||
} | ||
}, | ||
ImportExpression(path) { | ||
maybeReplace(path.node.source, path.get("source")); | ||
}, | ||
}, | ||
}; | ||
}); |
2 changes: 2 additions & 0 deletions
2
...et-typescript/test/fixtures/opts/rewriteImportExtensions-createImportExpressions/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,2 @@ | ||
import("./a.ts"); | ||
import(a); |
7 changes: 7 additions & 0 deletions
7
...ypescript/test/fixtures/opts/rewriteImportExtensions-createImportExpressions/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,7 @@ | ||
{ | ||
"sourceType": "module", | ||
"presets": [["typescript", { "rewriteImportExtensions": true }]], | ||
"parserOpts": { | ||
"createImportExpressions": true | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...-typescript/test/fixtures/opts/rewriteImportExtensions-createImportExpressions/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,2 @@ | ||
import("./a.js"); | ||
import((a + "").replace(/([\/].*.[mc]?)tsx?$/, "$1js")); |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import get from "./get.js"; | ||
import getPrototypeOf from "./getPrototypeOf.js"; | ||
function _superPropertyGet(t, e, o, r) { | ||
var p = get(getPrototypeOf(1 & r ? t.prototype : t), e, o); | ||
function _superPropGet(t, o, e, r) { | ||
var p = get(getPrototypeOf(1 & r ? t.prototype : t), o, e); | ||
return 2 & r && "function" == typeof p ? function (t) { | ||
return p.apply(o, t); | ||
return p.apply(e, t); | ||
} : p; | ||
} | ||
export { _superPropertyGet as default }; | ||
export { _superPropGet as default }; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import set from "./set.js"; | ||
import getPrototypeOf from "./getPrototypeOf.js"; | ||
function _superPropertySet(t, e, o, r, p, f) { | ||
function _superPropSet(t, e, o, r, p, f) { | ||
return set(getPrototypeOf(f ? t.prototype : t), e, o, r, p); | ||
} | ||
export { _superPropertySet as default }; | ||
export { _superPropSet as default }; |