-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
190 lines (163 loc) · 5.22 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const {licenseBanner} = require('./defines/license-banner.js');
const {
engines,
browserslist,
dependencies,
devDependencies,
peerDependencies
} = require('./package.json');
const findVersion = (name) => {
const sources = [dependencies, devDependencies, peerDependencies];
for (const source of sources) {
if (!source) {
continue;
}
const keys = Object.keys(source);
for (const key of keys) {
if (name instanceof RegExp) {
if (name.test(key)) {
return source[key];
}
} else if (key === name) {
return source[key];
}
}
}
return null;
};
const toExactVersion = (ver) => {
return ver.replace(/[^0-9.]/g, '');
};
const getVersion = (name, exact) => {
const ver = findVersion(name);
if (!ver) {
return undefined;
}
return exact ? toExactVersion(ver) : ver;
};
module.exports = function (api, options = {}) {
const env = typeof options.env === 'string' ? options.env : api.env();
const esm =
typeof options.esm === 'boolean'
? options.esm
: process.env.NPM_CONFIG_ESM !== 'false';
const licenseNotice =
env === 'production' &&
process.env.NPM_CONFIG_SKIP_LICENSE_NOTICE !== 'true';
const targets = {
browsers: browserslist[env] || browserslist.development,
node: toExactVersion(engines.node)
};
const transformRuntimeVersion = getVersion('@babel/runtime', false);
const coreJSVersion = getVersion('core-js-pure', false);
const signature = JSON.stringify({
env,
esm,
licenseNotice,
targets,
transformRuntimeVersion,
coreJSVersion
});
api.cache.using(() => signature);
console.log('\n');
console.log('BABEL CONFIG:');
console.log('- ENV: ', env);
console.log('- ESM: ', esm);
console.log('- License Notice: ', licenseNotice);
console.log('- Transform Runtime Version: ', transformRuntimeVersion);
console.log('- CoreJS Version: ', coreJSVersion);
console.log('\n');
const presets = [
[
'@babel/preset-env',
{
spec: env === 'production',
modules: esm && env !== 'test' ? false : 'auto',
ignoreBrowserslistConfig: true
}
]
];
const plugins = [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-private-methods',
'@babel/plugin-proposal-private-property-in-object'
];
if (env === 'production') {
plugins.push([
'@babel/plugin-transform-runtime',
{
version: transformRuntimeVersion // https://babeljs.io/docs/en/babel-plugin-transform-runtime#version
}
]);
plugins.push([
'polyfill-corejs3',
{
method: 'usage-pure',
version: coreJSVersion
}
]);
}
const ignore = [];
if (env !== 'test') {
ignore.push(/.+\.test\.(js|ts)x?$/);
ignore.push(/.+jest\.setup\.(js|ts)x?$/);
}
if (env !== 'development') {
ignore.push(/.+\.stories\.(js|ts)x?$/);
}
/*
* USEFUL RESOURCES:
* https://babeljs.io/docs/en/options#matchpattern
*/
return {
targets,
browserslistConfigFile: false,
sourceMaps: true, // https://github.com/babel/babel/issues/5261
sourceType: 'module',
ignore,
presets,
plugins,
overrides: [
{
test: ['**/*.jsx', '**/*.tsx'],
presets: [
[
'@babel/preset-react',
{
development: env === 'development',
runtime: 'automatic' // https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#manual-babel-setup
}
]
],
plugins:
env === 'production'
? ['babel-plugin-jsx-remove-data-test-id']
: []
},
{
// THIS PRESET WILL JUST STRIP/TRANSFORM TYPESCRIPT SYNTAX WITHOUT PERFORMING ANY CHECK (AS EXPECTED). TYPECHECKING SHOULD BE PERFORMED WHEN BUILDING TYPE DECLARATIONS
test: ['**/*.ts', '**/*.tsx'],
presets: ['@babel/preset-typescript']
},
{
// https://babeljs.io/docs/en/options#sourcetype
test: [`${__dirname}/.yarn/**/*`, '**/node_modules/**/*'],
sourceType: 'unambiguous'
},
{
// https://github.com/babel/babel/issues/15363
test: [`${__dirname}/src/**/*`],
plugins: licenseNotice
? [
[
'./.babel/plugins/babel-plugin-license-banner/index.js',
{
licenseBanner
}
]
]
: []
}
]
};
};