/node_modules/babel-preset-react-app/index.js

https://bitbucket.org/delainetech/transporter_react · JavaScript · 136 lines · 86 code · 5 blank · 45 comment · 9 complexity · f7d6e3a5fce7a2594bfa8af0d8bc2d39 MD5 · raw file

  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 plugins = [
  9. // class { handleClick = () => { } }
  10. require.resolve('babel-plugin-transform-class-properties'),
  11. // The following two plugins use Object.assign directly, instead of Babel's
  12. // extends helper. Note that this assumes `Object.assign` is available.
  13. // { ...todo, completed: true }
  14. [
  15. require.resolve('babel-plugin-transform-object-rest-spread'),
  16. {
  17. useBuiltIns: true,
  18. },
  19. ],
  20. // Transforms JSX
  21. [
  22. require.resolve('babel-plugin-transform-react-jsx'),
  23. {
  24. useBuiltIns: true,
  25. },
  26. ],
  27. // Polyfills the runtime needed for async/await and generators
  28. [
  29. require.resolve('babel-plugin-transform-runtime'),
  30. {
  31. helpers: false,
  32. polyfill: false,
  33. regenerator: true,
  34. },
  35. ],
  36. ];
  37. // This is similar to how `env` works in Babel:
  38. // https://babeljs.io/docs/usage/babelrc/#env-option
  39. // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
  40. // https://github.com/babel/babel/issues/4539
  41. // https://github.com/facebookincubator/create-react-app/issues/720
  42. // It’s also nice that we can enforce `NODE_ENV` being specified.
  43. var env = process.env.BABEL_ENV || process.env.NODE_ENV;
  44. if (env !== 'development' && env !== 'test' && env !== 'production') {
  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. if (env === 'development' || env === 'test') {
  54. // The following two plugins are currently necessary to make React warnings
  55. // include more valuable information. They are included here because they are
  56. // currently not enabled in babel-preset-react. See the below threads for more info:
  57. // https://github.com/babel/babel/issues/4702
  58. // https://github.com/babel/babel/pull/3540#issuecomment-228673661
  59. // https://github.com/facebookincubator/create-react-app/issues/989
  60. plugins.push.apply(plugins, [
  61. // Adds component stack to warning messages
  62. require.resolve('babel-plugin-transform-react-jsx-source'),
  63. // Adds __self attribute to JSX which React will use for some warnings
  64. require.resolve('babel-plugin-transform-react-jsx-self'),
  65. ]);
  66. }
  67. if (env === 'test') {
  68. module.exports = {
  69. presets: [
  70. // ES features necessary for user's Node version
  71. [
  72. require('babel-preset-env').default,
  73. {
  74. targets: {
  75. node: 'current',
  76. },
  77. },
  78. ],
  79. // JSX, Flow
  80. require.resolve('babel-preset-react'),
  81. ],
  82. plugins: plugins.concat([
  83. // Compiles import() to a deferred require()
  84. require.resolve('babel-plugin-dynamic-import-node'),
  85. ]),
  86. };
  87. } else {
  88. module.exports = {
  89. presets: [
  90. // Latest stable ECMAScript features
  91. [
  92. require.resolve('babel-preset-env'),
  93. {
  94. targets: {
  95. // React parses on ie 9, so we should too
  96. ie: 9,
  97. // We currently minify with uglify
  98. // Remove after https://github.com/mishoo/UglifyJS2/issues/448
  99. uglify: true,
  100. },
  101. // Disable polyfill transforms
  102. useBuiltIns: false,
  103. // Do not transform modules to CJS
  104. modules: false,
  105. },
  106. ],
  107. // JSX, Flow
  108. require.resolve('babel-preset-react'),
  109. ],
  110. plugins: plugins.concat([
  111. // function* () { yield 42; yield 43; }
  112. [
  113. require.resolve('babel-plugin-transform-regenerator'),
  114. {
  115. // Async functions are converted to generators by babel-preset-env
  116. async: false,
  117. },
  118. ],
  119. // Adds syntax support for import()
  120. require.resolve('babel-plugin-syntax-dynamic-import'),
  121. ]),
  122. };
  123. if (env === 'production') {
  124. // Optimization: hoist JSX that never changes out of render()
  125. // Disabled because of issues: https://github.com/facebookincubator/create-react-app/issues/553
  126. // TODO: Enable again when these issues are resolved.
  127. // plugins.push.apply(plugins, [
  128. // require.resolve('babel-plugin-transform-react-constant-elements')
  129. // ]);
  130. }
  131. }