-
Notifications
You must be signed in to change notification settings - Fork 108
/
babel.config.cjs
73 lines (63 loc) · 1.82 KB
/
babel.config.cjs
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
// Babel can't transpile to ES on-the-fly. So, we need to transform `import.meta.dirname` to old `__dirname`.
function transformImportMetaDirname() {
return {
visitor: {
MemberExpression(path) {
if (path.node.object.type === 'MetaProperty' && path.node.property.name === 'dirname') {
path.replaceWithSourceString('__dirname')
}
},
},
}
}
const babel = (api) => {
api.cache(true)
const browserTarget = '> 0.25%, not dead'
const nodeTarget = { node: '18.0' }
const buildMixin = {
ignore: ['./__tests__', './**/*.d.ts'],
sourceMaps: 'inline',
}
return {
presets: [
['@babel/preset-env', { modules: 'commonjs', targets: nodeTarget }],
'@babel/preset-typescript',
],
ignore: ['./**/*.d.ts'],
sourceMaps: true,
env: {
// ESM build
ESM: {
presets: [['@babel/preset-env', { modules: false, targets: nodeTarget }]],
...buildMixin,
},
// CommonJS build
CJS: {
presets: [['@babel/preset-env', { modules: 'commonjs', targets: nodeTarget }]],
plugins: [
// Node.js CJS needs extensions in require statements
['babel-plugin-add-import-extension', { extension: 'cjs' }],
],
...buildMixin,
},
// Bundled ESM
ESM_ROLLUP: {
presets: [['@babel/preset-env', { targets: browserTarget }]],
...buildMixin,
},
// Gulp
GULP: {
presets: [['@babel/preset-env', { modules: false, targets: nodeTarget }]],
plugins: [transformImportMetaDirname],
sourceMaps: false,
},
// Jest
JEST: {
presets: [['@babel/preset-env', { modules: 'commonjs', targets: nodeTarget }]],
plugins: [transformImportMetaDirname],
sourceMaps: false,
},
},
}
}
module.exports = babel