Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

187 строки
6.3 KiB

  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const path = require('path');
  9. const validateBoolOption = (name, value, defaultValue) => {
  10. if (typeof value === 'undefined') {
  11. value = defaultValue;
  12. }
  13. if (typeof value !== 'boolean') {
  14. throw new Error(`Preset react-app: '${name}' option must be a boolean.`);
  15. }
  16. return value;
  17. };
  18. module.exports = function(api, opts, env) {
  19. if (!opts) {
  20. opts = {};
  21. }
  22. var isEnvDevelopment = env === 'development';
  23. var isEnvProduction = env === 'production';
  24. var isEnvTest = env === 'test';
  25. var isFlowEnabled = validateBoolOption('flow', opts.flow, true);
  26. var isTypeScriptEnabled = validateBoolOption(
  27. 'typescript',
  28. opts.typescript,
  29. true
  30. );
  31. var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, true);
  32. var useAbsoluteRuntime = validateBoolOption(
  33. 'absoluteRuntime',
  34. opts.absoluteRuntime,
  35. true
  36. );
  37. var absoluteRuntimePath = undefined;
  38. if (useAbsoluteRuntime) {
  39. absoluteRuntimePath = path.dirname(
  40. require.resolve('@babel/runtime/package.json')
  41. );
  42. }
  43. if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
  44. throw new Error(
  45. 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
  46. '`BABEL_ENV` environment variables. Valid values are "development", ' +
  47. '"test", and "production". Instead, received: ' +
  48. JSON.stringify(env) +
  49. '.'
  50. );
  51. }
  52. return {
  53. presets: [
  54. isEnvTest && [
  55. // ES features necessary for user's Node version
  56. require('@babel/preset-env').default,
  57. {
  58. targets: {
  59. node: 'current',
  60. },
  61. },
  62. ],
  63. (isEnvProduction || isEnvDevelopment) && [
  64. // Latest stable ECMAScript features
  65. require('@babel/preset-env').default,
  66. {
  67. // We want Create React App to be IE 9 compatible until React itself
  68. // no longer works with IE 9
  69. targets: {
  70. ie: 9,
  71. },
  72. // Users cannot override this behavior because this Babel
  73. // configuration is highly tuned for ES5 support
  74. ignoreBrowserslistConfig: true,
  75. // If users import all core-js they're probably not concerned with
  76. // bundle size. We shouldn't rely on magic to try and shrink it.
  77. useBuiltIns: false,
  78. // Do not transform modules to CJS
  79. modules: false,
  80. // Exclude transforms that make all code slower
  81. exclude: ['transform-typeof-symbol'],
  82. },
  83. ],
  84. [
  85. require('@babel/preset-react').default,
  86. {
  87. // Adds component stack to warning messages
  88. // Adds __self attribute to JSX which React will use for some warnings
  89. development: isEnvDevelopment || isEnvTest,
  90. // Will use the native built-in instead of trying to polyfill
  91. // behavior for any plugins that require one.
  92. useBuiltIns: true,
  93. },
  94. ],
  95. isTypeScriptEnabled && [require('@babel/preset-typescript').default],
  96. ].filter(Boolean),
  97. plugins: [
  98. // Strip flow types before any other transform, emulating the behavior
  99. // order as-if the browser supported all of the succeeding features
  100. // https://github.com/facebook/create-react-app/pull/5182
  101. isFlowEnabled &&
  102. require('@babel/plugin-transform-flow-strip-types').default,
  103. // Experimental macros support. Will be documented after it's had some time
  104. // in the wild.
  105. require('babel-plugin-macros'),
  106. // Necessary to include regardless of the environment because
  107. // in practice some other transforms (such as object-rest-spread)
  108. // don't work without it: https://github.com/babel/babel/issues/7215
  109. require('@babel/plugin-transform-destructuring').default,
  110. // Turn on legacy decorators for TypeScript files
  111. isTypeScriptEnabled && [
  112. require('@babel/plugin-proposal-decorators').default,
  113. false,
  114. ],
  115. // class { handleClick = () => { } }
  116. // Enable loose mode to use assignment instead of defineProperty
  117. // See discussion in https://github.com/facebook/create-react-app/issues/4263
  118. [
  119. require('@babel/plugin-proposal-class-properties').default,
  120. {
  121. loose: true,
  122. },
  123. ],
  124. // The following two plugins use Object.assign directly, instead of Babel's
  125. // extends helper. Note that this assumes `Object.assign` is available.
  126. // { ...todo, completed: true }
  127. [
  128. require('@babel/plugin-proposal-object-rest-spread').default,
  129. {
  130. useBuiltIns: true,
  131. },
  132. ],
  133. // Polyfills the runtime needed for async/await, generators, and friends
  134. // https://babeljs.io/docs/en/babel-plugin-transform-runtime
  135. [
  136. require('@babel/plugin-transform-runtime').default,
  137. {
  138. corejs: false,
  139. helpers: areHelpersEnabled,
  140. regenerator: true,
  141. // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
  142. // We should turn this on once the lowest version of Node LTS
  143. // supports ES Modules.
  144. useESModules: isEnvDevelopment || isEnvProduction,
  145. // Undocumented option that lets us encapsulate our runtime, ensuring
  146. // the correct version is used
  147. // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
  148. absoluteRuntime: absoluteRuntimePath,
  149. },
  150. ],
  151. isEnvProduction && [
  152. // Remove PropTypes from production build
  153. require('babel-plugin-transform-react-remove-prop-types').default,
  154. {
  155. removeImport: true,
  156. },
  157. ],
  158. // Adds syntax support for import()
  159. require('@babel/plugin-syntax-dynamic-import').default,
  160. isEnvTest &&
  161. // Transform dynamic import to require
  162. require('babel-plugin-dynamic-import-node'),
  163. ].filter(Boolean),
  164. overrides: [
  165. isTypeScriptEnabled && {
  166. test: /\.tsx?$/,
  167. plugins: [
  168. [
  169. require('@babel/plugin-proposal-decorators').default,
  170. { legacy: true },
  171. ],
  172. ],
  173. },
  174. ].filter(Boolean),
  175. };
  176. };