|
6 | 6 | */
|
7 | 7 | 'use strict';
|
8 | 8 |
|
9 |
| -const plugins = [ |
10 |
| - // Experimental macros support. Will be documented after it's had some time |
11 |
| - // in the wild. |
12 |
| - require.resolve('babel-plugin-macros'), |
13 |
| - // Necessary to include regardless of the environment because |
14 |
| - // in practice some other transforms (such as object-rest-spread) |
15 |
| - // don't work without it: https://github.com/babel/babel/issues/7215 |
16 |
| - require.resolve('babel-plugin-transform-es2015-destructuring'), |
17 |
| - // class { handleClick = () => { } } |
18 |
| - require.resolve('babel-plugin-transform-class-properties'), |
19 |
| - // The following two plugins use Object.assign directly, instead of Babel's |
20 |
| - // extends helper. Note that this assumes `Object.assign` is available. |
21 |
| - // { ...todo, completed: true } |
22 |
| - [ |
23 |
| - require.resolve('babel-plugin-transform-object-rest-spread'), |
24 |
| - { |
25 |
| - useBuiltIns: true, |
26 |
| - }, |
27 |
| - ], |
28 |
| - // Transforms JSX |
29 |
| - [ |
30 |
| - require.resolve('babel-plugin-transform-react-jsx'), |
31 |
| - { |
32 |
| - useBuiltIns: true, |
33 |
| - }, |
34 |
| - ], |
35 |
| - // Polyfills the runtime needed for async/await and generators |
36 |
| - [ |
37 |
| - require.resolve('babel-plugin-transform-runtime'), |
38 |
| - { |
39 |
| - helpers: false, |
40 |
| - polyfill: false, |
41 |
| - regenerator: true, |
42 |
| - }, |
43 |
| - ], |
44 |
| -]; |
45 |
| - |
46 |
| -// This is similar to how `env` works in Babel: |
47 |
| -// https://babeljs.io/docs/usage/babelrc/#env-option |
48 |
| -// We are not using `env` because it’s ignored in versions > [email protected]: |
49 |
| -// https://github.com/babel/babel/issues/4539 |
50 |
| -// https://github.com/facebookincubator/create-react-app/issues/720 |
51 |
| -// It’s also nice that we can enforce `NODE_ENV` being specified. |
52 |
| -var env = process.env.BABEL_ENV || process.env.NODE_ENV; |
53 |
| -if (env !== 'development' && env !== 'test' && env !== 'production') { |
54 |
| - throw new Error( |
55 |
| - 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' + |
56 |
| - '`BABEL_ENV` environment variables. Valid values are "development", ' + |
57 |
| - '"test", and "production". Instead, received: ' + |
58 |
| - JSON.stringify(env) + |
59 |
| - '.' |
60 |
| - ); |
61 |
| -} |
| 9 | +module.exports = function(api, opts) { |
| 10 | + if (!opts) { |
| 11 | + opts = {}; |
| 12 | + } |
62 | 13 |
|
63 |
| -if (env === 'development' || env === 'test') { |
64 |
| - // The following two plugins are currently necessary to make React warnings |
65 |
| - // include more valuable information. They are included here because they are |
66 |
| - // currently not enabled in babel-preset-react. See the below threads for more info: |
67 |
| - // https://github.com/babel/babel/issues/4702 |
68 |
| - // https://github.com/babel/babel/pull/3540#issuecomment-228673661 |
69 |
| - // https://github.com/facebookincubator/create-react-app/issues/989 |
70 |
| - plugins.push.apply(plugins, [ |
71 |
| - // Adds component stack to warning messages |
72 |
| - require.resolve('babel-plugin-transform-react-jsx-source'), |
73 |
| - // Adds __self attribute to JSX which React will use for some warnings |
74 |
| - require.resolve('babel-plugin-transform-react-jsx-self'), |
75 |
| - ]); |
76 |
| -} |
| 14 | + // This is similar to how `env` works in Babel: |
| 15 | + // https://babeljs.io/docs/usage/babelrc/#env-option |
| 16 | + // We are not using `env` because it’s ignored in versions > [email protected]: |
| 17 | + // https://github.com/babel/babel/issues/4539 |
| 18 | + // https://github.com/facebookincubator/create-react-app/issues/720 |
| 19 | + // It’s also nice that we can enforce `NODE_ENV` being specified. |
| 20 | + var env = process.env.BABEL_ENV || process.env.NODE_ENV; |
| 21 | + var isEnvDevelopment = env === 'development'; |
| 22 | + var isEnvProduction = env === 'production'; |
| 23 | + var isEnvTest = env === 'test'; |
| 24 | + if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) { |
| 25 | + throw new Error( |
| 26 | + 'Using `babel-preset-react-app` requires that you specify `NODE_ENV` or ' + |
| 27 | + '`BABEL_ENV` environment variables. Valid values are "development", ' + |
| 28 | + '"test", and "production". Instead, received: ' + |
| 29 | + JSON.stringify(env) + |
| 30 | + '.' |
| 31 | + ); |
| 32 | + } |
77 | 33 |
|
78 |
| -if (env === 'test') { |
79 |
| - module.exports = { |
| 34 | + return { |
80 | 35 | presets: [
|
81 |
| - // ES features necessary for user's Node version |
82 |
| - [ |
83 |
| - require('babel-preset-env').default, |
| 36 | + isEnvTest && [ |
| 37 | + // ES features necessary for user's Node version |
| 38 | + require('@babel/preset-env').default, |
84 | 39 | {
|
85 | 40 | targets: {
|
86 |
| - node: 'current', |
| 41 | + node: '6.12', |
87 | 42 | },
|
88 | 43 | },
|
89 | 44 | ],
|
90 |
| - // JSX, Flow |
91 |
| - require.resolve('babel-preset-react'), |
92 |
| - ], |
93 |
| - plugins: plugins.concat([ |
94 |
| - // Compiles import() to a deferred require() |
95 |
| - require.resolve('babel-plugin-dynamic-import-node'), |
96 |
| - ]), |
97 |
| - }; |
98 |
| -} else { |
99 |
| - module.exports = { |
100 |
| - presets: [ |
101 |
| - // Latest stable ECMAScript features |
102 |
| - [ |
103 |
| - require.resolve('babel-preset-env'), |
| 45 | + (isEnvProduction || isEnvDevelopment) && [ |
| 46 | + // Latest stable ECMAScript features |
| 47 | + require('@babel/preset-env').default, |
104 | 48 | {
|
105 | 49 | targets: {
|
106 | 50 | // React parses on ie 9, so we should too
|
107 | 51 | ie: 9,
|
108 |
| - // We currently minify with uglify |
109 |
| - // Remove after https://github.com/mishoo/UglifyJS2/issues/448 |
110 |
| - uglify: true, |
111 | 52 | },
|
| 53 | + // We currently minify with uglify |
| 54 | + // Remove after https://github.com/mishoo/UglifyJS2/issues/448 |
| 55 | + forceAllTransforms: true, |
112 | 56 | // Disable polyfill transforms
|
113 | 57 | useBuiltIns: false,
|
114 | 58 | // Do not transform modules to CJS
|
115 | 59 | modules: false,
|
116 | 60 | },
|
117 | 61 | ],
|
118 |
| - // JSX, Flow |
119 |
| - require.resolve('babel-preset-react'), |
120 |
| - ], |
121 |
| - plugins: plugins.concat([ |
122 |
| - // function* () { yield 42; yield 43; } |
123 | 62 | [
|
124 |
| - require.resolve('babel-plugin-transform-regenerator'), |
| 63 | + require('@babel/preset-react').default, |
| 64 | + { |
| 65 | + // Adds component stack to warning messages |
| 66 | + // Adds __self attribute to JSX which React will use for some warnings |
| 67 | + development: isEnvDevelopment || isEnvTest, |
| 68 | + }, |
| 69 | + ], |
| 70 | + [require('@babel/preset-flow').default], |
| 71 | + ].filter(Boolean), |
| 72 | + plugins: [ |
| 73 | + // Experimental macros support. Will be documented after it's had some time |
| 74 | + // in the wild. |
| 75 | + require('babel-plugin-macros'), |
| 76 | + // Necessary to include regardless of the environment because |
| 77 | + // in practice some other transforms (such as object-rest-spread) |
| 78 | + // don't work without it: https://github.com/babel/babel/issues/7215 |
| 79 | + require('@babel/plugin-transform-destructuring').default, |
| 80 | + // class { handleClick = () => { } } |
| 81 | + require('@babel/plugin-proposal-class-properties').default, |
| 82 | + // The following two plugins use Object.assign directly, instead of Babel's |
| 83 | + // extends helper. Note that this assumes `Object.assign` is available. |
| 84 | + // { ...todo, completed: true } |
| 85 | + [ |
| 86 | + require('@babel/plugin-proposal-object-rest-spread').default, |
| 87 | + { |
| 88 | + useBuiltIns: true, |
| 89 | + }, |
| 90 | + ], |
| 91 | + // Transforms JSX |
| 92 | + [ |
| 93 | + require('@babel/plugin-transform-react-jsx').default, |
| 94 | + { |
| 95 | + useBuiltIns: true, |
| 96 | + }, |
| 97 | + ], |
| 98 | + // Polyfills the runtime needed for async/await and generators |
| 99 | + [ |
| 100 | + require('@babel/plugin-transform-runtime').default, |
| 101 | + { |
| 102 | + helpers: false, |
| 103 | + polyfill: false, |
| 104 | + regenerator: true, |
| 105 | + }, |
| 106 | + ], |
| 107 | + // function* () { yield 42; yield 43; } |
| 108 | + !isEnvTest && [ |
| 109 | + require('@babel/plugin-transform-regenerator').default, |
125 | 110 | {
|
126 | 111 | // Async functions are converted to generators by babel-preset-env
|
127 | 112 | async: false,
|
128 | 113 | },
|
129 | 114 | ],
|
130 | 115 | // Adds syntax support for import()
|
131 |
| - require.resolve('babel-plugin-syntax-dynamic-import'), |
132 |
| - ]), |
| 116 | + require('@babel/plugin-syntax-dynamic-import').default, |
| 117 | + isEnvTest && |
| 118 | + // Transform dynamic import to require |
| 119 | + require('babel-plugin-transform-dynamic-import').default, |
| 120 | + ].filter(Boolean), |
133 | 121 | };
|
134 |
| - |
135 |
| - if (env === 'production') { |
136 |
| - // Optimization: hoist JSX that never changes out of render() |
137 |
| - // Disabled because of issues: https://github.com/facebookincubator/create-react-app/issues/553 |
138 |
| - // TODO: Enable again when these issues are resolved. |
139 |
| - // plugins.push.apply(plugins, [ |
140 |
| - // require.resolve('babel-plugin-transform-react-constant-elements') |
141 |
| - // ]); |
142 |
| - } |
143 |
| -} |
| 122 | +}; |
0 commit comments