-
Notifications
You must be signed in to change notification settings - Fork 97
/
.babelrc.js
77 lines (64 loc) · 2.24 KB
/
.babelrc.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
// About this file:
// Babel 7 introduces .babelrc.js files. The .babelrc file can be removed when Babel 7 is released. (https://github.com/babel/babel/pull/4892)
// Babel 7 will also remove the 'env' option --> https://github.com/babel/babel/issues/4539#issuecomment-284870486
const env = process.env.BABEL_ENV || process.env.NODE_ENV
const defaultBabelPresetEnvConfig = {
// No module transformation, webpack will take care of this if necessary.
modules: false,
}
// Latest browsers (via package.json browserslists)
const browserBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig)
// Node
const nodeBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: {
node: '18',
},
})
// Combined node and browser environment for es6 modules version and tests
const modulesBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: Object.assign(nodeBabelPresetEnvConfig.targets),
})
const testBabelPresetEnvConfig = Object.assign({}, modulesBabelPresetEnvConfig, {
// Tests need to transform modules
modules: 'commonjs',
})
const plugins = [
'@babel/proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-export-namespace-from',
'lodash',
[
'inline-replace-variables',
{
// Inject version number into code
__VERSION__: require('./package.json').version,
},
],
]
let babelConfig = {
plugins,
}
if (env === 'browser') {
babelConfig = Object.assign(babelConfig, {
presets: [['@babel/preset-env', browserBabelPresetEnvConfig], '@babel/typescript'],
})
}
if (env === 'modules') {
babelConfig = Object.assign(babelConfig, {
presets: [['@babel/preset-env', modulesBabelPresetEnvConfig], '@babel/typescript'],
})
}
if (env === 'node') {
babelConfig = Object.assign(babelConfig, {
presets: [['@babel/preset-env', nodeBabelPresetEnvConfig], '@babel/typescript'],
})
}
if (env === 'test') {
babelConfig = Object.assign(babelConfig, {
presets: [['@babel/preset-env', testBabelPresetEnvConfig], '@babel/typescript'],
plugins: babelConfig.plugins.concat(['rewire-ts']),
})
}
module.exports = babelConfig