forked from contentful/contentful-management.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.babelrc.js
94 lines (79 loc) · 2.67 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// 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
const browserBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: {
browsers: ['last 2 versions', 'not ie < 13', 'not android < 50'],
},
})
// Legacy browsers
const legacyBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: {
browsers: ['last 5 versions', 'not ie < 10'],
},
})
// Node
const nodeBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: {
node: '6',
},
})
// Combined node and browser environment for es6 modules version and tests
const modulesBabelPresetEnvConfig = Object.assign({}, defaultBabelPresetEnvConfig, {
targets: Object.assign(legacyBabelPresetEnvConfig.targets, 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 === 'legacy') {
babelConfig = Object.assign(babelConfig, {
presets: [['@babel/preset-env', legacyBabelPresetEnvConfig], '@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