diff --git a/packages/babel-plugin-transform-typescript/README.md b/packages/babel-plugin-transform-typescript/README.md index 9286ac31e8..6cc77d1e22 100644 --- a/packages/babel-plugin-transform-typescript/README.md +++ b/packages/babel-plugin-transform-typescript/README.md @@ -56,6 +56,15 @@ require("@babel/core").transform("code", { plugins: ["@babel/plugin-transform-typescript"] }); ``` +## Options + +### `jsxPragma` + +`string` + +Replace the function used when compiling JSX expressions. + +This is so that we know that the import is not a type import, and should not be removed [const_enum]: https://www.typescriptlang.org/docs/handbook/enums.html#const-enums [namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.js index e336eb9621..ac1e51d477 100644 --- a/packages/babel-plugin-transform-typescript/src/index.js +++ b/packages/babel-plugin-transform-typescript/src/index.js @@ -20,7 +20,7 @@ interface State { programPath: any; } -export default declare(api => { +export default declare((api, { jsxPragma = "React" }) => { api.assertVersion(7); return { @@ -269,11 +269,11 @@ export default declare(api => { } } - if (binding.identifier.name != "React") { + if (binding.identifier.name !== jsxPragma) { return true; } - // "React" is referenced as a value if there are any JSX elements in the code. + // "React" or the JSX pragma is referenced as a value if there are any JSX elements in the code. let sourceFileHasJsx = false; programPath.traverse({ JSXElement() { diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/input.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/input.mjs new file mode 100644 index 0000000000..b2ac3334b3 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/input.mjs @@ -0,0 +1,3 @@ +// Don't elide Preact if a JSX element appears somewhere. +import { h, render } from "preact"; +
; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json new file mode 100644 index 0000000000..bba55263ce --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["syntax-jsx", ["transform-typescript", { "jsxPragma": "h" }]] +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/output.mjs new file mode 100644 index 0000000000..8260408197 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/output.mjs @@ -0,0 +1,3 @@ +// Don't elide Preact if a JSX element appears somewhere. +import { h } from "preact"; +
; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/input.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/input.mjs new file mode 100644 index 0000000000..b656b79fdc --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/input.mjs @@ -0,0 +1,2 @@ +import { FooBar, h } from "preact"; +const x: FooBar = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/output.mjs new file mode 100644 index 0000000000..0773757932 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact/output.mjs @@ -0,0 +1 @@ +const x = 0; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json index fa715c3ccb..2552742a15 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json @@ -1,3 +1,3 @@ { - "plugins": ["syntax-jsx"] + "plugins": ["syntax-jsx", "transform-typescript"] } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json index fa715c3ccb..2552742a15 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json @@ -1,3 +1,3 @@ { - "plugins": ["syntax-jsx"] + "plugins": ["syntax-jsx", "transform-typescript"] }