-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel.config.js
153 lines (151 loc) · 4.34 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// @see https://babeljs.io/docs/en/next/config-files#project-wide-configuration
module.exports = (api) => {
api.cache(() => process.env.NODE_ENV);
const isSite = api.env('site');
const isCDNBundle = api.env('bundle');
const isCommonJS = api.env('cjs');
const isESModule = api.env('esm');
const isTest = api.env('test');
if (isSite) {
return {
skipEnvCheck: true,
presets: [
'@babel/preset-env',
[
'@babel/preset-react',
{
development: isCommonJS,
},
],
// 'babel-preset-gatsby', {
// silence: true
// },
],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
'transform-inline-environment-variables',
[
'@babel/plugin-proposal-decorators',
{
legacy: true,
},
],
[
// import glsl as raw text
'babel-plugin-inline-import',
{
extensions: [
// 由于使用了 TS 的 resolveJsonModule 选项,JSON 可以直接引入,不需要当作纯文本
'.pbf',
'.glsl',
],
},
],
],
};
}
return {
presets: [
[
'@babel/preset-env',
{
// https://babeljs.io/docs/en/babel-preset-env#usebuiltins
// useBuiltIns: 'usage',
...(isCDNBundle ? { corejs: 3 } : {}),
useBuiltIns: isCDNBundle ? 'usage' : false,
// set `modules: false` when building CDN bundle, let rollup do commonjs works
// @see https://github.com/rollup/rollup-plugin-babel#modules
modules: isCDNBundle || isESModule ? false : 'auto',
targets: {
chrome: 58,
browsers: ['ie >= 11'],
},
},
],
[
'@babel/preset-react',
{
development: isCommonJS,
},
],
'@babel/preset-typescript',
],
plugins: [
isCDNBundle ? {} : '@babel/plugin-transform-runtime',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-syntax-async-generators',
// '@babel/plugin-transform-parameters',
'transform-node-env-inline',
[
'@babel/plugin-proposal-decorators',
{
legacy: true,
},
],
[
'@babel/plugin-proposal-class-properties',
{
// @see https://github.com/storybookjs/storybook/issues/6069#issuecomment-472544973
loose: false,
},
],
'@babel/plugin-syntax-dynamic-import',
// let rollup do commonjs works
// @see https://github.com/rollup/rollup-plugin-babel#modules
isCDNBundle || isESModule
? {}
: '@babel/plugin-transform-modules-commonjs',
// 开发模式下以原始文本引入,便于调试
isCDNBundle
? {}
: [
// import glsl as raw text
'babel-plugin-inline-import',
{
extensions: [
// 由于使用了 TS 的 resolveJsonModule 选项,JSON 可以直接引入,不需要当作纯文本
// '.json',
'.glsl',
'.worker.js',
],
},
],
isCDNBundle
? {}
: [
'transform-import-css-l7',
// 'transform-import-styles' // babel 编译将样式打包到js
],
[
// @see https://github.com/babel/babel/issues/8741#issuecomment-509041135
'const-enum',
{
transform: 'constObject',
},
],
// 按需引用 @see https://github.com/lodash/babel-plugin-lodash
'lodash',
// 内联 WebGL 常量 @see https://www.npmjs.com/package/babel-plugin-inline-webgl-constants
// isCDNBundle ? 'inline-webgl-constants' : {},
],
ignore: [
// /node_modules\/(?![kdbush|supercluster|async])/,
'node_modules',
...(!isTest
? [
'**/*.test.tsx',
'**/*.test.ts',
'**/*.story.tsx',
'__snapshots__',
'__tests__',
'__stories__',
'**/*/__snapshots__',
'**/*/__tests__',
]
: []),
],
};
};