Setup JSX runtime even if the file doesn't contain JSX (#12479)

This commit is contained in:
Nicolò Ribaudo 2020-12-10 19:27:22 +01:00 committed by GitHub
parent e5b2680756
commit bf417186bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 158 additions and 130 deletions

View File

@ -138,7 +138,6 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
Program: {
enter(path, state) {
if (hasJSX(path)) {
const { file } = state;
let runtime = RUNTIME_DEFAULT;
@ -268,7 +267,6 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
if (options.development) {
path.traverse(injectMetaPropertiesVisitor, state);
}
}
},
// TODO (Babel 8): Decide if this should be removed or brought back.
@ -353,18 +351,6 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
return imports;
}
function hasJSX(parentPath) {
let fileHasJSX = false;
parentPath.traverse({
"JSXElement|JSXFragment"(path) {
fileHasJSX = true;
path.stop();
},
});
return fileHasJSX;
}
function getSource(source, importName) {
switch (importName) {
case "Fragment":

View File

@ -0,0 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "automatic" }],
"./plugin.js"
]
}

View File

@ -0,0 +1 @@
const foo = /*#__PURE__*/undefined.jsx("p", {});

View File

@ -0,0 +1,13 @@
module.exports = ({ types: t }) => ({
visitor: {
NumericLiteral(path) {
path.replaceWith(
t.jsxElement(
t.jsxOpeningElement(t.jsxIdentifier("p"), []),
t.jsxClosingElement(t.jsxIdentifier("p")),
[]
)
);
}
}
});

View File

@ -0,0 +1 @@
const foo = 2;

View File

@ -0,0 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "classic" }],
"./plugin.js"
]
}

View File

@ -0,0 +1 @@
const foo = /*#__PURE__*/React.createElement("p", null);

View File

@ -0,0 +1,13 @@
module.exports = ({ types: t }) => ({
visitor: {
NumericLiteral(path) {
path.replaceWith(
t.jsxElement(
t.jsxOpeningElement(t.jsxIdentifier("p"), []),
t.jsxClosingElement(t.jsxIdentifier("p")),
[]
)
);
}
}
});