-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
104 lines (99 loc) · 3.17 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const envPreset = [
'@babel/preset-env',
{
// 只导入需要的 polyfill
useBuiltIns: 'usage',
// 指定 corjs 版本
corejs: {
version: 3,
proposals: true, // 使用尚在“提议”阶段特性的 polyfill
},
modules: false,
},
'@babel/typescript',
];
function useNodeConfig() {
envPreset[1].targets = { node: 'current' };
envPreset[1].modules = 'auto';
return {
presets: [envPreset, '@babel/preset-typescript'],
plugins: [
[
('import',
{
libraryName: 'lodash',
libraryDirectory: '',
camel2DashComponentName: false,
},
'lodash'),
],
'@babel/plugin-transform-runtime',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
// eq ts-loader set 'emitDecoratorMetadata': true
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties', { loose: true }],
],
};
}
function useWebConfig() {
// 禁用模块化方案转换
envPreset[1].modules = false;
return {
presets: ['@babel/preset-typescript', envPreset],
plugins: [
[
'import',
{
libraryName: 'antd',
libraryDirectory: 'es',
style: true,
},
'antd',
],
[
'import',
{
libraryName: 'lodash',
libraryDirectory: '',
camel2DashComponentName: false,
},
'lodash',
],
'@babel/plugin-transform-runtime',
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-optional-chaining',
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
],
env: {
development: {
presets: [['@babel/preset-react', { development: true }]],
plugins: ['react-hot-loader/babel'],
},
production: {
presets: ['@babel/preset-react'],
plugins: [
'babel-plugin-dev-expression',
'@babel/plugin-transform-react-constant-elements',
'@babel/plugin-transform-react-inline-elements',
],
},
},
};
}
module.exports = function (api) {
let target;
api.caller((caller) => caller && (target = caller.target));
api.cache(true);
switch (target) {
case 'node': {
return useNodeConfig();
}
default: {
return useWebConfig();
}
}
};