-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.mjs
69 lines (66 loc) · 1.2 KB
/
babel.config.mjs
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
import {readFileSync} from 'node:fs';
const {name, version, engines} = JSON.parse(readFileSync('./package.json'));
const node = engines.node
.split(/[^\d.]+/)
.filter(s => s.length)
.map(s => [...s.split('.').map(s => +s || 0), 0, 0].slice(0, 3))
.sort((a, b) => a[2] - b[2])
.sort((a, b) => a[1] - b[1])
.sort((a, b) => a[0] - b[0])[0]
.join('.');
export default api => {
const env = api.env();
api.cache(() => env);
const modules = env === 'esm' ? false : 'commonjs';
const ext = modules ? '.js' : '.mjs';
const presets = [];
const plugins = [];
presets.push(
[
'@babel/preset-env',
{
modules,
exclude: ['proposal-dynamic-import'],
targets: {
node
}
}
],
['@babel/preset-typescript']
);
plugins.push(
[
'module-replace',
{
replace: [[/^(\.\.?\/.+)\.(m|c)?tsx?$/i, `$1${ext}`]]
}
],
[
'search-and-replace',
{
rules: [
{
search: '#{NAME}',
replace: name
},
{
search: '#{VERSION}',
replace: version
}
]
}
]
);
if (modules === 'commonjs') {
plugins.push([
'@babel/plugin-transform-modules-commonjs',
{
importInterop: 'node'
}
]);
}
return {
presets,
plugins
};
};