dependencies.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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) {
  19. if (!opts) {
  20. opts = {};
  21. }
  22. // This is similar to how `env` works in Babel:
  23. // https://babeljs.io/docs/usage/babelrc/#env-option
  24. // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
  25. // https://github.com/babel/babel/issues/4539
  26. // https://github.com/facebook/create-react-app/issues/720
  27. // It’s also nice that we can enforce `NODE_ENV` being specified.
  28. var env = process.env.BABEL_ENV || process.env.NODE_ENV;
  29. var isEnvDevelopment = env === 'development';
  30. var isEnvProduction = env === 'production';
  31. var isEnvTest = env === 'test';
  32. var areHelpersEnabled = validateBoolOption('helpers', opts.helpers, false);
  33. var useAbsoluteRuntime = validateBoolOption(
  34. 'absoluteRuntime',
  35. opts.absoluteRuntime,
  36. true
  37. );
  38. var absoluteRuntimePath = undefined;
  39. if (useAbsoluteRuntime) {
  40. absoluteRuntimePath = path.dirname(
  41. require.resolve('@babel/runtime/package.json')
  42. );
  43. }
  44. if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
  45. throw new Error(
  46. 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' +
  47. '`BABEL_ENV` environment variables. Valid values are "development", ' +
  48. '"test", and "production". Instead, received: ' +
  49. JSON.stringify(env) +
  50. '.'
  51. );
  52. }
  53. return {
  54. // Babel assumes ES Modules, which isn't safe until CommonJS
  55. // dies. This changes the behavior to assume CommonJS unless
  56. // an `import` or `export` is present in the file.
  57. // https://github.com/webpack/webpack/issues/4039#issuecomment-419284940
  58. sourceType: 'unambiguous',
  59. presets: [
  60. isEnvTest && [
  61. // ES features necessary for user's Node version
  62. require('@babel/preset-env').default,
  63. {
  64. targets: {
  65. node: 'current',
  66. },
  67. // Exclude transforms that make all code slower
  68. exclude: ['transform-typeof-symbol'],
  69. },
  70. ],
  71. (isEnvProduction || isEnvDevelopment) && [
  72. // Latest stable ECMAScript features
  73. require('@babel/preset-env').default,
  74. {
  75. // Allow importing core-js in entrypoint and use browserlist to select polyfills
  76. useBuiltIns: 'entry',
  77. // Set the corejs version we are using to avoid warnings in console
  78. // This will need to change once we upgrade to corejs@3
  79. corejs: 3,
  80. // Exclude transforms that make all code slower
  81. exclude: ['transform-typeof-symbol'],
  82. },
  83. ],
  84. ].filter(Boolean),
  85. plugins: [
  86. // Disabled as it's handled automatically by preset-env, and `selectiveLoose` isn't
  87. // yet merged into babel: https://github.com/babel/babel/pull/9486
  88. // Related: https://github.com/facebook/create-react-app/pull/8215
  89. // [
  90. // require('@babel/plugin-transform-destructuring').default,
  91. // {
  92. // // Use loose mode for performance:
  93. // // https://github.com/facebook/create-react-app/issues/5602
  94. // loose: false,
  95. // selectiveLoose: [
  96. // 'useState',
  97. // 'useEffect',
  98. // 'useContext',
  99. // 'useReducer',
  100. // 'useCallback',
  101. // 'useMemo',
  102. // 'useRef',
  103. // 'useImperativeHandle',
  104. // 'useLayoutEffect',
  105. // 'useDebugValue',
  106. // ],
  107. // },
  108. // ],
  109. // Polyfills the runtime needed for async/await, generators, and friends
  110. // https://babeljs.io/docs/en/babel-plugin-transform-runtime
  111. [
  112. require('@babel/plugin-transform-runtime').default,
  113. {
  114. corejs: false,
  115. helpers: areHelpersEnabled,
  116. // By default, babel assumes babel/runtime version 7.0.0-beta.0,
  117. // explicitly resolving to match the provided helper functions.
  118. // https://github.com/babel/babel/issues/10261
  119. version: require('@babel/runtime/package.json').version,
  120. regenerator: true,
  121. // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
  122. // We should turn this on once the lowest version of Node LTS
  123. // supports ES Modules.
  124. useESModules: isEnvDevelopment || isEnvProduction,
  125. // Undocumented option that lets us encapsulate our runtime, ensuring
  126. // the correct version is used
  127. // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
  128. absoluteRuntime: absoluteRuntimePath,
  129. },
  130. ],
  131. ].filter(Boolean),
  132. };
  133. };