You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dependencies.js 4.5 KiB

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // Do not transform modules to CJS
  68. modules: false,
  69. // Exclude transforms that make all code slower
  70. exclude: ['transform-typeof-symbol'],
  71. },
  72. ],
  73. (isEnvProduction || isEnvDevelopment) && [
  74. // Latest stable ECMAScript features
  75. require('@babel/preset-env').default,
  76. {
  77. // We want Create React App to be IE 9 compatible until React itself
  78. // no longer works with IE 9
  79. targets: {
  80. ie: 9,
  81. },
  82. // Users cannot override this behavior because this Babel
  83. // configuration is highly tuned for ES5 support
  84. ignoreBrowserslistConfig: true,
  85. // If users import all core-js they're probably not concerned with
  86. // bundle size. We shouldn't rely on magic to try and shrink it.
  87. useBuiltIns: false,
  88. // Do not transform modules to CJS
  89. modules: false,
  90. // Exclude transforms that make all code slower
  91. exclude: ['transform-typeof-symbol'],
  92. },
  93. ],
  94. ].filter(Boolean),
  95. plugins: [
  96. // Polyfills the runtime needed for async/await, generators, and friends
  97. // https://babeljs.io/docs/en/babel-plugin-transform-runtime
  98. [
  99. require('@babel/plugin-transform-runtime').default,
  100. {
  101. corejs: false,
  102. helpers: areHelpersEnabled,
  103. regenerator: true,
  104. // https://babeljs.io/docs/en/babel-plugin-transform-runtime#useesmodules
  105. // We should turn this on once the lowest version of Node LTS
  106. // supports ES Modules.
  107. useESModules: isEnvDevelopment || isEnvProduction,
  108. // Undocumented option that lets us encapsulate our runtime, ensuring
  109. // the correct version is used
  110. // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42
  111. absoluteRuntime: absoluteRuntimePath,
  112. },
  113. ],
  114. // Adds syntax support for import()
  115. require('@babel/plugin-syntax-dynamic-import').default,
  116. isEnvTest &&
  117. // Transform dynamic import to require
  118. require('babel-plugin-transform-dynamic-import').default,
  119. ].filter(Boolean),
  120. };
  121. };