UNPKG

@babel/preset-react

Version:

Babel preset for all React plugins.

95 lines (91 loc) 3.87 kB
import { declarePreset } 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 transformReactPure from '@babel/plugin-transform-react-pure-annotations'; import { OptionValidator, findSuggestion } from '@babel/helper-validator-option'; const v = new OptionValidator("@babel/preset-react"); function normalizeOptions(options = {}) { if ("useSpread" in options) { 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 options) { const useBuiltInsFormatted = JSON.stringify(options.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-transform-object-rest-spread", { "loose": true, "useBuiltIns": ${useBuiltInsFormatted} }] ], "presets": ["@babel/preset-react"] }`); } const TopLevelOptions = { development: "development", developmentSourceSelf: "developmentSourceSelf", importSource: "importSource", pragma: "pragma", pragmaFrag: "pragmaFrag", pure: "pure", runtime: "runtime", throwIfNamespace: "throwIfNamespace" }; v.validateTopLevelOptions(options, TopLevelOptions); const development = v.validateBooleanOption(TopLevelOptions.development, options.development); const developmentSourceSelf = v.validateBooleanOption(TopLevelOptions.developmentSourceSelf, options.developmentSourceSelf, false); let importSource = v.validateStringOption(TopLevelOptions.importSource, options.importSource); let pragma = v.validateStringOption(TopLevelOptions.pragma, options.pragma); let pragmaFrag = v.validateStringOption(TopLevelOptions.pragmaFrag, options.pragmaFrag); const pure = v.validateBooleanOption(TopLevelOptions.pure, options.pure); const runtime = v.validateStringOption(TopLevelOptions.runtime, options.runtime, "automatic"); const throwIfNamespace = v.validateBooleanOption(TopLevelOptions.throwIfNamespace, options.throwIfNamespace, true); const validRuntime = ["classic", "automatic"]; if (runtime === "classic") { pragma = pragma || "React.createElement"; pragmaFrag = pragmaFrag || "React.Fragment"; } else if (runtime === "automatic") { importSource = importSource || "react"; } else { throw new Error(`@babel/preset-react: 'runtime' must be one of ['automatic', 'classic'] but we have '${runtime}'\n` + `- Did you mean '${findSuggestion(runtime, validRuntime)}'?`); } return { development, developmentSourceSelf, importSource, pragma, pragmaFrag, pure, runtime, throwIfNamespace }; } const index = declarePreset((api, opts) => { api.assertVersion("^7.0.0-0 || ^8.0.0"); const { development = api.env(env => env === "development"), developmentSourceSelf, importSource, pragma, pragmaFrag, pure, runtime, throwIfNamespace } = normalizeOptions(opts); const pluginOptios = { importSource, pragma, pragmaFrag, runtime, throwIfNamespace, pure }; return { plugins: [development ? [transformReactJSXDevelopment, { ...pluginOptios, sourceSelf: developmentSourceSelf }] : [transformReactJSX, pluginOptios], transformReactDisplayName, pure !== false && transformReactPure].filter(Boolean) }; }); export { index as default }; //# sourceMappingURL=index.js.map