-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
76 lines (67 loc) 路 2.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { declare } from "@babel/helper-plugin-utils";
import transformReactJSX from "@babel/plugin-transform-react-jsx";
import transformReactJSXDevelopment from "@babel/plugin-transform-react-jsx-development";
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";
import transformReactPure from "@babel/plugin-transform-react-pure-annotations";
export default declare((api, opts) => {
api.assertVersion(7);
const {
pure,
pragma,
pragmaFrag,
throwIfNamespace = true,
runtime = "automatic",
importSource,
} = opts;
// TODO: (Babel 8) Don't cast these options but validate it
const development = !!opts.development;
if ("useSpread" in opts) {
throw new Error(
'@babel/preset-react: 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.',
);
}
if ("useBuiltIns" in opts) {
const useBuiltInsFormatted = JSON.stringify(opts.useBuiltIns);
throw new Error(
`@babel/preset-react: Since "useBuiltIns" is removed in Babel 8, you can remove it from the config.
- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with
\`useBuiltIns: ${useBuiltInsFormatted}\`, you can use the following config
{
"plugins": [
["@babel/plugin-proposal-object-rest-spread", { "loose": true, "useBuiltIns": ${useBuiltInsFormatted} }]
],
"presets": ["@babel/preset-react"]
}`,
);
}
if (typeof development !== "boolean") {
throw new Error(
"@babel/preset-react 'development' option must be a boolean.",
);
}
const transformReactJSXPlugin =
runtime === "automatic" && development
? transformReactJSXDevelopment
: transformReactJSX;
return {
plugins: [
[
transformReactJSXPlugin,
{
importSource,
pragma,
pragmaFrag,
runtime,
throwIfNamespace,
pure,
},
],
transformReactDisplayName,
pure !== false && transformReactPure,
development && runtime === "classic" && transformReactJSXSource,
development && runtime === "classic" && transformReactJSXSelf,
].filter(Boolean),
};
});