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