-
Notifications
You must be signed in to change notification settings - Fork 1
/
babel.config.js
78 lines (75 loc) · 2.61 KB
/
babel.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @type {import('@babel/core').ConfigFunction}
*/
module.exports = function getBabelPreset(api) {
if (api) {
// Cache the returned value forever and don't call this function again.
api.cache(true);
}
// This is similar to how `env` works in Babel:
// https://babeljs.io/docs/usage/babelrc/#env-option
// We are not using `env` because it’s ignored in versions > [email protected]:
// https://github.com/babel/babel/issues/4539
// https://github.com/facebook/create-react-app/issues/720
// It’s also nice that we can enforce `NODE_ENV` being specified.
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
const isEnvDevelopment = env === 'development';
const isEnvProduction = env === 'production';
const isEnvTest = env === 'test';
if (!isEnvDevelopment && !isEnvProduction && !isEnvTest) {
throw new Error(
'The babel preset of requires that you specify `NODE_ENV` or ' +
'`BABEL_ENV` environment variables. Valid values are "development", ' +
`"test", and "production". Instead, received: ${JSON.stringify(env)}.`
);
}
/**
* @type {import('@babel/core').TransformOptions}
*/
return {
presets: [
isEnvTest && [
// ES features necessary for user's Node version
require('@babel/preset-env').default,
{
targets: {
browsers: ['last 2 versions'],
node: 'current',
},
},
],
(isEnvProduction || isEnvDevelopment) && [
// Latest stable ECMAScript features
require('@babel/preset-env').default,
{
targets: {
browsers: ['last 2 versions'],
},
corejs: { version: 3, proposals: true },
// `entry` transforms `@babel/polyfill` into individual requires for
// the targeted browsers. This is safer than `usage` which performs
// static code analysis to determine what's required.
// This is probably a fine default to help trim down bundles when
// end-users inevitably import '@babel/polyfill'.
useBuiltIns: 'entry',
// Do not transform modules to CJS
modules: false,
include: ['transform-classes'],
},
],
[require('@babel/preset-typescript').default],
].filter(Boolean),
plugins: [
// Polyfills the runtime needed for async/await and generators
[
require('@babel/plugin-transform-runtime').default,
{
corejs: 3,
// To be able to use `runtime` in Rollup babel plugin
helpers: true,
regenerator: true,
},
],
],
};
};