-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
83 lines (71 loc) · 2.13 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
function checkAppEnv(env) {
return (
process.env.REACT_APP_ENV &&
process.env.REACT_APP_ENV.toLowerCase().indexOf(env) !== -1
);
}
module.exports = function (api) {
// 若build依赖于env,就不要再指定api.cache为forever或never了
// api.cache(true);
const env = api.env();
// const isProd = api.env('production');
// 用在react应用开发调试阶段,会启用@babel/preset-react、react-refresh/babel
const isEnvReactHot = checkAppEnv('reacthot');
// 用在react项目打包阶段,会启用@babel/preset-react,而不会启用react-refresh/babel
const isEnvReact = checkAppEnv('react');
console.log(';;process.env.REACT_APP_ENV, ', process.env.REACT_APP_ENV);
console.log(';;isEnvReact, ', isEnvReact);
// Plugins run before Presets. Plugin ordering is first to last.
const plugins = [
['@babel/plugin-proposal-class-properties', { loose: false }],
'@babel/proposal-object-rest-spread',
isEnvReactHot && 'react-refresh/babel',
].filter(Boolean);
function configModule() {
if (env === 'esm' || env === 'es6') {
return false;
}
// 默认会编译成node自身的commonjs
return 'auto';
}
// Preset ordering is reversed (last to first).
const presets = [
[
'@babel/preset-env',
{
// modules: env === 'esm' ? false : 'auto',
modules: configModule(),
targets: 'defaults',
// targets: '> 0.25%, not dead',
useBuiltIns: 'usage',
corejs: { version: 3, proposals: true },
debug: false,
},
],
[
'@babel/preset-typescript',
{
// later: 支持其他框架的jsx
isTSX: !!isEnvReact,
allExtensions: true,
onlyRemoveTypeImports: true,
allowNamespaces: true,
allowDeclareFields: true,
},
],
isEnvReact && [
'@babel/preset-react',
{
// runtime: 'automatic',
development: env !== 'production',
},
],
].filter(Boolean);
// console.log('babel-presets, ', JSON.stringify(presets));
const ignore = ['node_modules'];
return {
plugins,
presets,
ignore,
};
};