Debian Codegraph @

packages/babel-preset-react/src/normalize-options.ts

raw
1 import {
2 OptionValidator,
3 findSuggestion,
4 } from "@babel/helper-validator-option";
5 const v = new OptionValidator("@babel/preset-react");
6
7 export default function normalizeOptions(options: any = {}) {
8 if (process.env.BABEL_8_BREAKING) {
9 if ("useSpread" in options) {
10 throw new Error(
11 '@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.',
12 );
13 }
14
15 if ("useBuiltIns" in options) {
16 const useBuiltInsFormatted = JSON.stringify(options.useBuiltIns);
17 throw new Error(
18 `@babel/preset-react: Since "useBuiltIns" is removed in Babel 8, you can remove it from the config.
19 - Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with
20 \`useBuiltIns: ${useBuiltInsFormatted}\`, you can use the following config
21 {
22 "plugins": [
23 ["@babel/plugin-proposal-object-rest-spread", { "loose": true, "useBuiltIns": ${useBuiltInsFormatted} }]
24 ],
25 "presets": ["@babel/preset-react"]
26 }`,
27 );
28 }
29
30 const TopLevelOptions = {
31 development: "development",
32 importSource: "importSource",
33 pragma: "pragma",
34 pragmaFrag: "pragmaFrag",
35 pure: "pure",
36 runtime: "runtime",
37 throwIfNamespace: "throwIfNamespace",
38 };
39 v.validateTopLevelOptions(options, TopLevelOptions);
40 const development = v.validateBooleanOption(
41 TopLevelOptions.development,
42 options.development,
43 false,
44 );
45 let importSource = v.validateStringOption(
46 TopLevelOptions.importSource,
47 options.importSource,
48 );
49 let pragma = v.validateStringOption(TopLevelOptions.pragma, options.pragma);
50 let pragmaFrag = v.validateStringOption(
51 TopLevelOptions.pragmaFrag,
52 options.pragmaFrag,
53 );
54 const pure = v.validateBooleanOption(TopLevelOptions.pure, options.pure);
55 const runtime = v.validateStringOption(
56 TopLevelOptions.runtime,
57 options.runtime,
58 "automatic",
59 );
60 const throwIfNamespace = v.validateBooleanOption(
61 TopLevelOptions.throwIfNamespace,
62 options.throwIfNamespace,
63 true,
64 );
65
66 const validRuntime = ["classic", "automatic"];
67
68 if (runtime === "classic") {
69 pragma = pragma || "React.createElement";
70 pragmaFrag = pragmaFrag || "React.Fragment";
71 } else if (runtime === "automatic") {
72 importSource = importSource || "react";
73 } else {
74 throw new Error(
75 `@babel/preset-react: 'runtime' must be one of ['automatic', 'classic'] but we have '${runtime}'\n` +
76 `- Did you mean '${findSuggestion(runtime, validRuntime)}'?`,
77 );
78 }
79
80 return {
81 development,
82 importSource,
83 pragma,
84 pragmaFrag,
85 pure,
86 runtime,
87 throwIfNamespace,
88 };
89 } else {
90 let { pragma, pragmaFrag } = options;
91
92 const {
93 pure,
94 throwIfNamespace = true,
95 runtime = "classic",
96 importSource,
97 useBuiltIns,
98 useSpread,
99 } = options;
100
101 if (runtime === "classic") {
102 pragma = pragma || "React.createElement";
103 pragmaFrag = pragmaFrag || "React.Fragment";
104 }
105
106 const development = !!options.development;
107
108 return {
109 development,
110 importSource,
111 pragma,
112 pragmaFrag,
113 pure,
114 runtime,
115 throwIfNamespace,
116 useBuiltIns,
117 useSpread,
118 };
119 }
120 }