|
| 1 | +module.exports = function(api) { |
| 2 | + var validEnv = ['development', 'test', 'production'] |
| 3 | + var currentEnv = api.env() |
| 4 | + var isDevelopmentEnv = api.env('development') |
| 5 | + var isProductionEnv = api.env('production') |
| 6 | + var isTestEnv = api.env('test') |
| 7 | + |
| 8 | + if (!validEnv.includes(currentEnv)) { |
| 9 | + throw new Error( |
| 10 | + 'Please specify a valid `NODE_ENV` or ' + |
| 11 | + '`BABEL_ENV` environment variables. Valid values are "development", ' + |
| 12 | + '"test", and "production". Instead, received: ' + |
| 13 | + JSON.stringify(currentEnv) + |
| 14 | + '.' |
| 15 | + ) |
| 16 | + } |
| 17 | + |
| 18 | + return { |
| 19 | + presets: [ |
| 20 | + isTestEnv && [ |
| 21 | + '@babel/preset-env', |
| 22 | + { |
| 23 | + targets: { |
| 24 | + node: 'current' |
| 25 | + }, |
| 26 | + modules: 'commonjs' |
| 27 | + }, |
| 28 | + '@babel/preset-react' |
| 29 | + ], |
| 30 | + (isProductionEnv || isDevelopmentEnv) && [ |
| 31 | + '@babel/preset-env', |
| 32 | + { |
| 33 | + forceAllTransforms: true, |
| 34 | + useBuiltIns: 'entry', |
| 35 | + corejs: 3, |
| 36 | + modules: false, |
| 37 | + exclude: ['transform-typeof-symbol'] |
| 38 | + } |
| 39 | + ], |
| 40 | + [ |
| 41 | + '@babel/preset-react', |
| 42 | + { |
| 43 | + development: isDevelopmentEnv || isTestEnv, |
| 44 | + useBuiltIns: true |
| 45 | + } |
| 46 | + ] |
| 47 | + ].filter(Boolean), |
| 48 | + plugins: [ |
| 49 | + 'babel-plugin-macros', |
| 50 | + '@babel/plugin-syntax-dynamic-import', |
| 51 | + isTestEnv && 'babel-plugin-dynamic-import-node', |
| 52 | + '@babel/plugin-transform-destructuring', |
| 53 | + [ |
| 54 | + '@babel/plugin-proposal-class-properties', |
| 55 | + { |
| 56 | + loose: true |
| 57 | + } |
| 58 | + ], |
| 59 | + [ |
| 60 | + '@babel/plugin-proposal-object-rest-spread', |
| 61 | + { |
| 62 | + useBuiltIns: true |
| 63 | + } |
| 64 | + ], |
| 65 | + [ |
| 66 | + '@babel/plugin-proposal-private-methods', |
| 67 | + { |
| 68 | + loose: true |
| 69 | + } |
| 70 | + ], |
| 71 | + [ |
| 72 | + '@babel/plugin-proposal-private-property-in-object', |
| 73 | + { |
| 74 | + loose: true |
| 75 | + } |
| 76 | + ], |
| 77 | + [ |
| 78 | + '@babel/plugin-transform-runtime', |
| 79 | + { |
| 80 | + helpers: false, |
| 81 | + regenerator: true, |
| 82 | + corejs: false |
| 83 | + } |
| 84 | + ], |
| 85 | + [ |
| 86 | + '@babel/plugin-transform-regenerator', |
| 87 | + { |
| 88 | + async: false |
| 89 | + } |
| 90 | + ], |
| 91 | + isProductionEnv && [ |
| 92 | + 'babel-plugin-transform-react-remove-prop-types', |
| 93 | + { |
| 94 | + removeImport: true |
| 95 | + } |
| 96 | + ] |
| 97 | + ].filter(Boolean) |
| 98 | + } |
| 99 | +} |
0 commit comments