-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel.config.mjs
112 lines (105 loc) · 3.13 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
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
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
/** @type {import('@babel/core').TransformOptions} */
const config = {
assumptions: {
constantReexports: true,
constantSuper: true,
enumerableModuleMeta: true,
ignoreFunctionLength: true,
noClassCalls: true,
noDocumentAll: true,
noIncompleteNsImportDetection: true,
noNewArrows: true,
privateFieldsAsSymbols: true,
setClassMethods: true,
setComputedProperties: true,
setPublicClassFields: true,
superIsCallableConstructor: true,
},
presets: [
[
'@babel/preset-env',
{
bugfixes: true,
},
],
'@babel/typescript',
],
plugins: [
// cf. https://babeljs.io/blog/2023/05/26/7.22.0#import-attributes-15536-15620
'@babel/plugin-syntax-import-attributes',
// cf. https://babeljs.io/blog/2023/05/26/7.22.0#explicit-resource-management-15633-15520
'@babel/plugin-proposal-explicit-resource-management',
// cf. https://babeljs.io/blog/2024/02/28/7.24.0#decorators-updates-16242
[
'@babel/plugin-proposal-decorators',
{
version: '2023-11',
},
],
...(process.env.BUILDTS_USE_BABLE_RUNTIME ? ['@babel/plugin-transform-runtime'] : []),
],
env: {
production: {
plugins: [
[
'transform-remove-console',
{
exclude: ['error', 'info', 'warn'],
},
],
],
},
test: {
plugins: [
[
'transform-remove-console',
{
exclude: ['error', 'info', 'warn', 'debug'],
},
],
],
presets: [
[
'@babel/preset-env',
{
modules: 'auto',
},
],
],
},
},
};
if (process.env.BUILD_TS_COREJS || process.env.BUILD_TS_COREJS_WITH_PROPOSALS) {
const rootPath = url.fileURLToPath(path.dirname(import.meta.url));
const packageJson = JSON.parse(fs.readFileSync(path.join(rootPath, 'package.json'), 'utf8'));
const [major, minor] = packageJson.dependencies['core-js'].split('.');
const proposals = process.env.BUILD_TS_COREJS_WITH_PROPOSALS ? { proposals: true } : {};
if (process.env.BUILD_TS_TARGET_CATEGORY === 'app') {
/** @type {import('@babel/core').PluginItem} */
const presetEnvConfig = config.presets[0][1];
presetEnvConfig.useBuiltIns = 'usage';
presetEnvConfig.corejs = { version: `${major}.${minor}`, ...proposals };
} else if (process.env.BUILD_TS_TARGET_CATEGORY === 'lib') {
// cf. https://github.com/babel/babel-polyfills#injection-methods
config.plugins.push(['polyfill-corejs3', { method: 'usage-pure', version: `${major}.${minor}`, ...proposals }]);
}
}
if (process.env.BUILD_TS_TARGET_DETAIL === 'lib-react') {
config.presets.push([
'@babel/preset-react',
{
runtime: 'automatic',
},
]);
/** @type {import('@babel/core').PluginItem} */
const presetEnvConfig = config.presets[0][1];
presetEnvConfig.targets = { esmodules: true };
presetEnvConfig.modules = false;
}
if (process.env.BUILD_TS_VERBOSE) {
console.info('Babel config:', JSON.stringify(config, undefined, 2));
}
export default config;