Make the typescript preset require a filename unless the user configured it for general use.
This commit is contained in:
parent
ca1c98b255
commit
43aa61d6be
@ -9,6 +9,16 @@ export default declare((api, options) => {
|
||||
|
||||
return {
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
// If the file has already enabled TS, assume that this is not a
|
||||
// valid Flowtype file.
|
||||
if (
|
||||
parserOpts.plugins.some(
|
||||
p => (Array.isArray(p) ? p[0] : p) === "typescript",
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
parserOpts.plugins.push(["flow", { all }]);
|
||||
},
|
||||
};
|
||||
|
||||
@ -5,6 +5,16 @@ export default declare(api => {
|
||||
|
||||
return {
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
// If the Typescript plugin already ran, it will have decided whether
|
||||
// or not this is a TSX file.
|
||||
if (
|
||||
parserOpts.plugins.some(
|
||||
p => (Array.isArray(p) ? p[0] : p) === "typescript",
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
parserOpts.plugins.push("jsx");
|
||||
},
|
||||
};
|
||||
|
||||
@ -1,15 +1,43 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
|
||||
export default declare(api => {
|
||||
function removePlugin(plugins, name) {
|
||||
const indices = [];
|
||||
plugins.forEach((plugin, i) => {
|
||||
const n = Array.isArray(plugin) ? plugin[0] : plugin;
|
||||
|
||||
if (n === name) {
|
||||
indices.unshift(i);
|
||||
}
|
||||
});
|
||||
|
||||
for (const i of indices) {
|
||||
plugins.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export default declare((api, { isTSX }) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
return {
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
const { plugins } = parserOpts;
|
||||
// If the Flow syntax plugin already ran, remove it since Typescript
|
||||
// takes priority.
|
||||
removePlugin(plugins, "flow");
|
||||
|
||||
// If the JSX syntax plugin already ran, remomove it because JSX handling
|
||||
// in TS depends on the extensions, and is purely dependent on 'isTSX'.
|
||||
removePlugin(plugins, "jsx");
|
||||
|
||||
parserOpts.plugins.push(
|
||||
"typescript",
|
||||
"objectRestSpread",
|
||||
"classProperties",
|
||||
);
|
||||
|
||||
if (isTSX) {
|
||||
parserOpts.plugins.push("jsx");
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"plugins": ["syntax-jsx", ["transform-typescript", { "jsxPragma": "h" }]]
|
||||
"plugins": [["transform-typescript", { "jsxPragma": "h", "isTSX": true }]]
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"plugins": ["syntax-jsx", "transform-typescript"]
|
||||
"plugins": [["transform-typescript", { "isTSX": true }]]
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{
|
||||
"plugins": ["syntax-jsx", "transform-typescript"]
|
||||
"plugins": [["transform-typescript", { "isTSX": true }]]
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@babel/helper-plugin-utils": "7.0.0-beta.47",
|
||||
"@babel/plugin-syntax-jsx": "7.0.0-beta.47",
|
||||
"@babel/plugin-transform-react-display-name": "7.0.0-beta.47",
|
||||
"@babel/plugin-transform-react-jsx": "7.0.0-beta.47",
|
||||
"@babel/plugin-transform-react-jsx-self": "7.0.0-beta.47",
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import transformReactJSX from "@babel/plugin-transform-react-jsx";
|
||||
import transformSyntaxJSX from "@babel/plugin-syntax-jsx";
|
||||
import transformReactDisplayName from "@babel/plugin-transform-react-display-name";
|
||||
import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
|
||||
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
|
||||
@ -27,7 +26,6 @@ export default declare((api, opts) => {
|
||||
transformReactJSX,
|
||||
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns },
|
||||
],
|
||||
transformSyntaxJSX,
|
||||
transformReactDisplayName,
|
||||
|
||||
development && transformReactJSXSource,
|
||||
|
||||
@ -1,10 +1,42 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import transformTypeScript from "@babel/plugin-transform-typescript";
|
||||
|
||||
export default declare((api, { jsxPragma }) => {
|
||||
api.assertVersion(7);
|
||||
export default declare(
|
||||
(api, { jsxPragma, allExtensions = false, isTSX = false }) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
return {
|
||||
plugins: [[transformTypeScript, { jsxPragma }]],
|
||||
};
|
||||
});
|
||||
if (typeof allExtensions !== "boolean") {
|
||||
throw new Error(".allExtensions must be a boolean, or undefined");
|
||||
}
|
||||
if (typeof isTSX !== "boolean") {
|
||||
throw new Error(".allExtensions must be a boolean, or undefined");
|
||||
}
|
||||
|
||||
if (isTSX && !allExtensions) {
|
||||
throw new Error("isTSX:true requires allExtensions:true");
|
||||
}
|
||||
|
||||
return {
|
||||
overrides: allExtensions
|
||||
? [
|
||||
{
|
||||
plugins: [[transformTypeScript, { jsxPragma, isTSX }]],
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
// Only set 'test' if explicitly requested, since it requires that
|
||||
// Babel is being called`
|
||||
test: /\.ts$/,
|
||||
plugins: [[transformTypeScript, { jsxPragma }]],
|
||||
},
|
||||
{
|
||||
// Only set 'test' if explicitly requested, since it requires that
|
||||
// Babel is being called`
|
||||
test: /\.tsx$/,
|
||||
plugins: [[transformTypeScript, { jsxPragma, isTSX: true }]],
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
});
|
||||
it("handles the typescript preset", () => {
|
||||
const output = Babel.transform("var a: string;", {
|
||||
presets: ["typescript"],
|
||||
presets: [["typescript", { allExtensions: true }]],
|
||||
}).code;
|
||||
expect(output).toBe("var a;");
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user