Skip to content

Commit

Permalink
Make automatic runtime the default for JSX compilation (#11436)
Browse files Browse the repository at this point in the history
  • Loading branch information
arku authored and nicolo-ribaudo committed Jul 5, 2020
1 parent b2607ba commit 0ee5335
Show file tree
Hide file tree
Showing 78 changed files with 164 additions and 59 deletions.
2 changes: 1 addition & 1 deletion packages/babel-node/test/config.json
@@ -1,7 +1,7 @@
{
"presets": [
"../../babel-preset-env",
["../../babel-preset-react"]
["../../babel-preset-react", { "runtime": "classic" }]
],
"plugins": [
"../../babel-plugin-transform-strict-mode",
Expand Down
Expand Up @@ -2,7 +2,7 @@
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-react-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-react-constant-elements"
]
}
Expand Up @@ -2,7 +2,7 @@
"plugins": [
"transform-block-scoping",
"syntax-jsx",
"transform-react-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-block-scoped-functions",
"transform-arrow-functions"
]
Expand Down
@@ -1,4 +1,7 @@
{
"plugins": ["external-helpers", "proposal-class-properties"],
"presets": ["env", "react"]
"presets": [
"env",
["react", { "runtime": "classic" }]
]
}
@@ -1,3 +1,6 @@
{
"presets": ["env", "react"]
"presets": [
"env",
["react", { "runtime": "classic" }]
]
}
@@ -1,3 +1,6 @@
{
"plugins": ["transform-react-jsx", "transform-react-constant-elements"]
"plugins": [
["transform-react-jsx", { "runtime": "classic" }],
"transform-react-constant-elements"
]
}
@@ -1,3 +1,6 @@
{
"plugins": ["transform-react-jsx", "transform-react-constant-elements"]
"plugins": [
["transform-react-jsx", { "runtime": "classic" }],
"transform-react-constant-elements"
]
}
Expand Up @@ -3,7 +3,7 @@
"external-helpers",
"syntax-jsx",
"transform-react-inline-elements",
"transform-react-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-modules-commonjs"
]
}
Expand Up @@ -3,7 +3,7 @@
"external-helpers",
"syntax-jsx",
"transform-react-inline-elements",
"transform-react-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-modules-commonjs"
]
}
Expand Up @@ -3,6 +3,6 @@
"external-helpers",
"syntax-jsx",
"transform-react-inline-elements",
"transform-react-jsx"
["transform-react-jsx", { "runtime": "classic" }]
]
}
6 changes: 3 additions & 3 deletions packages/babel-plugin-transform-react-jsx/src/index.js
Expand Up @@ -5,14 +5,14 @@ import transformAutomatic from "./transform-automatic";
import { declare } from "@babel/helper-plugin-utils";

export default declare((api, options) => {
const { runtime = "classic" } = options;
const { runtime = "automatic" } = options;

// we throw a warning in helper-builder-react-jsx-experimental if runtime
// is neither automatic or classic because we will remove this file
// in v8.0.0
if (runtime === "classic") {
return transformClassic(api, options);
} else {
return transformAutomatic(api, options);
}

return transformAutomatic(api, options);
});
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pure": false, "runtime": "classic" }]
]
}
@@ -0,0 +1 @@
React.createElement("div", null);
@@ -1 +1,3 @@
React.createElement("div", null);
var _reactJsxRuntime = require("react/jsx-runtime");

_reactJsxRuntime.jsx("div", {});
@@ -0,0 +1,3 @@
/* @jsx h */

<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pure": false, "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "pure": false }]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h", "pure": false, "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h", "pure": false }]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pure": true, "runtime": "classic" }]
]
}
@@ -0,0 +1,2 @@
/*#__PURE__*/
React.createElement("div", null);
@@ -1,2 +1,4 @@
var _reactJsxRuntime = require("react/jsx-runtime");

/*#__PURE__*/
React.createElement("div", null);
_reactJsxRuntime.jsx("div", {});
@@ -0,0 +1,3 @@
/* @jsx h */

<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pure": true, "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "pure": true }]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h", "pure": true, "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h", "pure": true }]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "classic" }]
]
}
@@ -0,0 +1,2 @@
/*#__PURE__*/
React.createElement("div", null);
@@ -1,2 +1,4 @@
var _reactJsxRuntime = require("react/jsx-runtime");

/*#__PURE__*/
React.createElement("div", null);
_reactJsxRuntime.jsx("div", {});
@@ -0,0 +1,3 @@
/* @jsx h */

<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", {}]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -0,0 +1 @@
<div />;
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h", "runtime": "classic" }]
]
}
@@ -1,5 +1,6 @@
{
"plugins": [
["transform-react-jsx", { "pragma": "h" }]
]
],
"throws": "pragma and pragmaFrag cannot be set when runtime is automatic."
}
@@ -1,3 +1,3 @@
{
"plugins": [["transform-react-jsx", { "pragma": "foo.bar" }]]
"plugins": [["transform-react-jsx", { "pragma": "foo.bar", "runtime": "classic" }]]
}
@@ -1,3 +1,3 @@
{
"plugins": [["transform-react-jsx", { "pragma": "dom" }]]
"plugins": [["transform-react-jsx", { "pragma": "dom", "runtime": "classic" }]]
}
Expand Up @@ -2,7 +2,7 @@
"plugins": [
"external-helpers",
"syntax-jsx",
"transform-react-jsx",
["transform-react-jsx", { "runtime": "classic" }],
"transform-react-display-name",
"transform-arrow-functions"
]
Expand Down
@@ -1,3 +1,6 @@
{
"plugins": ["transform-react-jsx", "transform-property-literals"]
"plugins": [
["transform-react-jsx", { "runtime": "classic" }],
"transform-property-literals"
]
}
Expand Up @@ -4,6 +4,7 @@
"transform-react-jsx",
{
"pragma": "h",
"runtime": "classic",
"throwIfNamespace": false
}
]
Expand Down
Expand Up @@ -4,6 +4,7 @@
"transform-react-jsx",
{
"pragma": "h",
"runtime": "classic",
"throwIfNamespace": true
}
]
Expand Down
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-react-jsx", { "runtime": "classic" }]
]
}

This file was deleted.

@@ -0,0 +1,5 @@
var _reactJsxRuntime = require("react/jsx-runtime");

var x = /*#__PURE__*/_reactJsxRuntime.jsx("div", {
children: /*#__PURE__*/_reactJsxRuntime.jsx("span", {})
});

This file was deleted.

@@ -1,3 +1,5 @@
var div = /*#__PURE__*/React.createElement(Component, babelHelpers.extends({}, props, {
var _reactJsxRuntime = require("react/jsx-runtime");

var div = /*#__PURE__*/_reactJsxRuntime.jsx(Component, babelHelpers.extends({}, props, {
foo: "bar"
}));
@@ -1,4 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useBuiltIns": false }]],
"plugins": [["transform-react-jsx", { "runtime": "classic", "useBuiltIns": false }]],
"rejects": "transform-react-jsx: Since \"useBuiltIns\" is removed in Babel 8, you must remove it from your config.\n- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with\n`useBuiltIns: false`, you can use the following config\n{\n \"plugins\": [\n \"@babel/plugin-transform-react-jsx\",\n [\"@babel/plugin-proposal-object-rest-spread\", { \"loose\": true, \"useBuiltIns\": false }]\n ]\n}"
}
@@ -1,3 +1,5 @@
var div = /*#__PURE__*/React.createElement(Component, Object.assign({}, props, {
var _reactJsxRuntime = require("react/jsx-runtime");

var div = /*#__PURE__*/_reactJsxRuntime.jsx(Component, Object.assign({}, props, {
foo: "bar"
}));
@@ -1,4 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useBuiltIns": true }]],
"plugins": [["transform-react-jsx", { "runtime": "classic", "useBuiltIns": true }]],
"rejects": "transform-react-jsx: Since \"useBuiltIns\" is removed in Babel 8, you must remove it from your config.\n- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with\n`useBuiltIns: true`, you can use the following config\n{\n \"plugins\": [\n \"@babel/plugin-transform-react-jsx\",\n [\"@babel/plugin-proposal-object-rest-spread\", { \"loose\": true, \"useBuiltIns\": true }]\n ]\n}"
}
@@ -1,4 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useSpread": false }]],
"plugins": [["transform-react-jsx", { "runtime": "classic", "useSpread": false }]],
"rejects": "transform-react-jsx: Since Babel 8, an inline object with spread elements is always used, and the \"useSpread\" option is no longer available. Please remove it from your config."
}
@@ -1,4 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useSpread": true }]],
"plugins": [["transform-react-jsx", { "runtime": "classic", "useSpread": true }]],
"rejects": "transform-react-jsx: Since Babel 8, an inline object with spread elements is always used, and the \"useSpread\" option is no longer available. Please remove it from your config."
}
@@ -1,4 +1,6 @@
{
"presets": ["react"],
"presets": [
["react", { "runtime": "classic" }]
],
"plugins": ["transform-regenerator"]
}
12 changes: 3 additions & 9 deletions packages/babel-preset-react/src/index.js
Expand Up @@ -9,21 +9,15 @@ import transformReactPure from "@babel/plugin-transform-react-pure-annotations";
export default declare((api, opts) => {
api.assertVersion(7);

let { pragma, pragmaFrag } = opts;

const {
pure,
pragma,
pragmaFrag,
throwIfNamespace = true,
runtime = "classic",
runtime = "automatic",
importSource,
} = opts;

// TODO: (Babel 8) Remove setting these defaults
if (runtime === "classic") {
pragma = pragma || "React.createElement";
pragmaFrag = pragmaFrag || "React.Fragment";
}

// TODO: (Babel 8) Don't cast these options but validate it
const development = !!opts.development;

Expand Down
@@ -1,4 +1,4 @@
{
"presets": [["react", { "development": true }]],
"presets": [["react", { "development": true, "runtime": "classic" }]],
"os": ["win32"]
}

0 comments on commit 0ee5335

Please sign in to comment.