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 {
|
return {
|
||||||
manipulateOptions(opts, parserOpts) {
|
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 }]);
|
parserOpts.plugins.push(["flow", { all }]);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,6 +5,16 @@ export default declare(api => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
manipulateOptions(opts, parserOpts) {
|
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");
|
parserOpts.plugins.push("jsx");
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,15 +1,43 @@
|
|||||||
import { declare } from "@babel/helper-plugin-utils";
|
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);
|
api.assertVersion(7);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
manipulateOptions(opts, parserOpts) {
|
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(
|
parserOpts.plugins.push(
|
||||||
"typescript",
|
"typescript",
|
||||||
"objectRestSpread",
|
"objectRestSpread",
|
||||||
"classProperties",
|
"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",
|
"main": "lib/index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/helper-plugin-utils": "7.0.0-beta.47",
|
"@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-display-name": "7.0.0-beta.47",
|
||||||
"@babel/plugin-transform-react-jsx": "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",
|
"@babel/plugin-transform-react-jsx-self": "7.0.0-beta.47",
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { declare } from "@babel/helper-plugin-utils";
|
import { declare } from "@babel/helper-plugin-utils";
|
||||||
import transformReactJSX from "@babel/plugin-transform-react-jsx";
|
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 transformReactDisplayName from "@babel/plugin-transform-react-display-name";
|
||||||
import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
|
import transformReactJSXSource from "@babel/plugin-transform-react-jsx-source";
|
||||||
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
|
import transformReactJSXSelf from "@babel/plugin-transform-react-jsx-self";
|
||||||
@ -27,7 +26,6 @@ export default declare((api, opts) => {
|
|||||||
transformReactJSX,
|
transformReactJSX,
|
||||||
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns },
|
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns },
|
||||||
],
|
],
|
||||||
transformSyntaxJSX,
|
|
||||||
transformReactDisplayName,
|
transformReactDisplayName,
|
||||||
|
|
||||||
development && transformReactJSXSource,
|
development && transformReactJSXSource,
|
||||||
|
|||||||
@ -1,10 +1,42 @@
|
|||||||
import { declare } from "@babel/helper-plugin-utils";
|
import { declare } from "@babel/helper-plugin-utils";
|
||||||
import transformTypeScript from "@babel/plugin-transform-typescript";
|
import transformTypeScript from "@babel/plugin-transform-typescript";
|
||||||
|
|
||||||
export default declare((api, { jsxPragma }) => {
|
export default declare(
|
||||||
api.assertVersion(7);
|
(api, { jsxPragma, allExtensions = false, isTSX = false }) => {
|
||||||
|
api.assertVersion(7);
|
||||||
|
|
||||||
return {
|
if (typeof allExtensions !== "boolean") {
|
||||||
plugins: [[transformTypeScript, { jsxPragma }]],
|
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", () => {
|
it("handles the typescript preset", () => {
|
||||||
const output = Babel.transform("var a: string;", {
|
const output = Babel.transform("var a: string;", {
|
||||||
presets: ["typescript"],
|
presets: [["typescript", { allExtensions: true }]],
|
||||||
}).code;
|
}).code;
|
||||||
expect(output).toBe("var a;");
|
expect(output).toBe("var a;");
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user