2020-10-27 09:41:16 +01:00
|
|
|
module.exports = function (api) {
|
|
|
|
const validEnv = ['development', 'test', 'production'];
|
|
|
|
const currentEnv = api.env();
|
|
|
|
const isDevelopmentEnv = api.env('development');
|
|
|
|
const isProductionEnv = api.env('production');
|
|
|
|
const isTestEnv = api.env('test');
|
|
|
|
const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;
|
2020-09-07 15:52:05 +02:00
|
|
|
|
|
|
|
if (!validEnv.includes(currentEnv)) {
|
|
|
|
throw new Error(
|
|
|
|
'Please specify a valid `NODE_ENV` or ' +
|
|
|
|
'`BABEL_ENV` environment variables. Valid values are "development", ' +
|
|
|
|
'"test", and "production". Instead, received: ' +
|
|
|
|
JSON.stringify(currentEnv) +
|
|
|
|
'.'
|
2020-10-27 09:41:16 +01:00
|
|
|
);
|
2020-09-07 15:52:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
presets: [
|
|
|
|
isTestEnv && [
|
|
|
|
'@babel/preset-env',
|
|
|
|
{
|
|
|
|
targets: {
|
|
|
|
node: 'current'
|
2020-10-21 15:07:01 +02:00
|
|
|
},
|
|
|
|
modules: 'commonjs'
|
|
|
|
},
|
|
|
|
'@babel/preset-react'
|
2020-09-07 15:52:05 +02:00
|
|
|
],
|
|
|
|
(isProductionEnv || isDevelopmentEnv) && [
|
|
|
|
'@babel/preset-env',
|
|
|
|
{
|
|
|
|
forceAllTransforms: true,
|
|
|
|
useBuiltIns: 'entry',
|
|
|
|
corejs: 3,
|
|
|
|
modules: false,
|
|
|
|
exclude: ['transform-typeof-symbol']
|
|
|
|
}
|
2020-10-21 15:07:01 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'@babel/preset-react',
|
|
|
|
{
|
|
|
|
development: isDevelopmentEnv || isTestEnv,
|
|
|
|
useBuiltIns: true
|
|
|
|
}
|
2020-10-27 09:41:16 +01:00
|
|
|
],
|
|
|
|
['@babel/preset-typescript', { allExtensions: true, isTSX: true }]
|
2020-09-07 15:52:05 +02:00
|
|
|
].filter(Boolean),
|
|
|
|
plugins: [
|
2020-10-27 09:41:16 +01:00
|
|
|
isWebpackDevServer && 'react-refresh/babel',
|
2020-09-07 15:52:05 +02:00
|
|
|
'babel-plugin-macros',
|
|
|
|
'@babel/plugin-syntax-dynamic-import',
|
|
|
|
isTestEnv && 'babel-plugin-dynamic-import-node',
|
|
|
|
'@babel/plugin-transform-destructuring',
|
|
|
|
[
|
|
|
|
'@babel/plugin-proposal-class-properties',
|
|
|
|
{
|
2021-05-25 11:24:22 +02:00
|
|
|
loose: false
|
2020-09-07 15:52:05 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'@babel/plugin-proposal-object-rest-spread',
|
|
|
|
{
|
|
|
|
useBuiltIns: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'@babel/plugin-transform-runtime',
|
|
|
|
{
|
2020-10-21 15:07:01 +02:00
|
|
|
helpers: false,
|
|
|
|
regenerator: true,
|
|
|
|
corejs: false
|
2020-09-07 15:52:05 +02:00
|
|
|
}
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'@babel/plugin-transform-regenerator',
|
|
|
|
{
|
|
|
|
async: false
|
|
|
|
}
|
2020-10-21 15:07:01 +02:00
|
|
|
],
|
|
|
|
isProductionEnv && [
|
|
|
|
'babel-plugin-transform-react-remove-prop-types',
|
|
|
|
{
|
|
|
|
removeImport: true
|
|
|
|
}
|
2020-09-07 15:52:05 +02:00
|
|
|
]
|
|
|
|
].filter(Boolean)
|
2020-10-27 09:41:16 +01:00
|
|
|
};
|
|
|
|
};
|