1 | /**
|
2 | * Copyright (c) 2015-present, Facebook, Inc.
|
3 | * All rights reserved.
|
4 | *
|
5 | * This source code is licensed under the BSD-style license found in the
|
6 | * LICENSE file in the root directory of this source tree. An additional grant
|
7 | * of patent rights can be found in the PATENTS file in the same directory.
|
8 | */
|
9 | ;
|
10 |
|
11 | var path = require('path');
|
12 |
|
13 | const plugins = [
|
14 | // class { handleClick = () => { } }
|
15 | require.resolve('babel-plugin-transform-class-properties'),
|
16 | // The following two plugins use Object.assign directly, instead of Babel's
|
17 | // extends helper. Note that this assumes `Object.assign` is available.
|
18 | // { ...todo, completed: true }
|
19 | [require.resolve('babel-plugin-transform-object-rest-spread'), {
|
20 | useBuiltIns: true
|
21 | }],
|
22 | // Transforms JSX
|
23 | [require.resolve('babel-plugin-transform-react-jsx'), {
|
24 | useBuiltIns: true
|
25 | }],
|
26 | // Polyfills the runtime needed for async/await and generators
|
27 | [require.resolve('babel-plugin-transform-runtime'), {
|
28 | helpers: false,
|
29 | polyfill: false,
|
30 | regenerator: true,
|
31 | // Resolve the Babel runtime relative to the config.
|
32 | moduleName: path.dirname(require.resolve('babel-runtime/package'))
|
33 | }]
|
34 | ];
|
35 |
|
36 | // This is similar to how `env` works in Babel:
|
37 | // https://babeljs.io/docs/usage/babelrc/#env-option
|
38 | // We are not using `env` because it’s ignored in versions > babel-core@6.10.4:
|
39 | // https://github.com/babel/babel/issues/4539
|
40 | // https://github.com/facebookincubator/create-react-app/issues/720
|
41 | // It’s also nice that we can enforce `NODE_ENV` being specified.
|
42 | var env = process.env.BABEL_ENV || process.env.NODE_ENV;
|
43 | if (env !== 'development' && env !== 'test' && env !== 'production') {
|
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: ' + JSON.stringify(env) + '.'
|
48 | );
|
49 | }
|
50 |
|
51 | if (env === 'development' || env === 'test') {
|
52 | // The following two plugins are currently necessary to make React warnings
|
53 | // include more valuable information. They are included here because they are
|
54 | // currently not enabled in babel-preset-react. See the below threads for more info:
|
55 | // https://github.com/babel/babel/issues/4702
|
56 | // https://github.com/babel/babel/pull/3540#issuecomment-228673661
|
57 | // https://github.com/facebookincubator/create-react-app/issues/989
|
58 | plugins.push.apply(plugins, [
|
59 | // Adds component stack to warning messages
|
60 | require.resolve('babel-plugin-transform-react-jsx-source'),
|
61 | // Adds __self attribute to JSX which React will use for some warnings
|
62 | require.resolve('babel-plugin-transform-react-jsx-self')
|
63 | ]);
|
64 | }
|
65 |
|
66 | if (env === 'test') {
|
67 | plugins.push.apply(plugins, [
|
68 | // We always include this plugin regardless of environment
|
69 | // because of a Babel bug that breaks object rest/spread without it:
|
70 | // https://github.com/babel/babel/issues/4851
|
71 | require.resolve('babel-plugin-transform-es2015-parameters')
|
72 | ]);
|
73 |
|
74 | module.exports = {
|
75 | presets: [
|
76 | // ES features necessary for user's Node version
|
77 | [require('babel-preset-env').default, {
|
78 | targets: {
|
79 | node: 'current',
|
80 | },
|
81 | }],
|
82 | // JSX, Flow
|
83 | require.resolve('babel-preset-react')
|
84 | ],
|
85 | plugins: plugins
|
86 | };
|
87 | } else {
|
88 | module.exports = {
|
89 | presets: [
|
90 | // Latest stable ECMAScript features
|
91 | require.resolve('babel-preset-latest'),
|
92 | // JSX, Flow
|
93 | require.resolve('babel-preset-react')
|
94 | ],
|
95 | plugins: plugins.concat([
|
96 | // function* () { yield 42; yield 43; }
|
97 | [require.resolve('babel-plugin-transform-regenerator'), {
|
98 | // Async functions are converted to generators by babel-preset-latest
|
99 | async: false
|
100 | }],
|
101 | ])
|
102 | };
|
103 |
|
104 | if (env === 'production') {
|
105 | // Optimization: hoist JSX that never changes out of render()
|
106 | // Disabled because of issues:
|
107 | // * https://github.com/facebookincubator/create-react-app/issues/525
|
108 | // * https://phabricator.babeljs.io/search/query/pCNlnC2xzwzx/
|
109 | // * https://github.com/babel/babel/issues/4516
|
110 | // TODO: Enable again when these issues are resolved.
|
111 | // plugins.push.apply(plugins, [
|
112 | // require.resolve('babel-plugin-transform-react-constant-elements')
|
113 | // ]);
|
114 | }
|
115 | }
|