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