-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbabel.config.js
143 lines (136 loc) · 4.32 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
// * Every now and then, we adopt best practices from CRA
// * https://tinyurl.com/yakv4ggx
'use strict';
// ? https://nodejs.org/en/about/releases
const NODE_LTS = 'maintained node versions';
const packageName = require('./package.json').name;
const debug = require('debug')(`${packageName}:babel-config`);
const generateProductionEsmConfigObject = (mutateConfigFunction) => {
const config = {
presets: [
[
'@babel/preset-env',
{
// ? https://babeljs.io/docs/en/babel-preset-env#modules
modules: false
}
],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? We don't care about minification
],
plugins: [
[
'transform-rewrite-imports',
{
// ? Ensure all local imports without extensions now end in .mjs
appendExtension: '.mjs',
replaceExtensions: {
// ? Ensure built distributables can locate the package.json file
'^../package.json$': '../../package.json',
// ? Replace pkgverse imports with their runtime equivalents
'^pkgverse/([^/]+)/src/index$': '$1'
}
}
]
]
};
if (mutateConfigFunction) {
mutateConfigFunction(config);
}
return config;
};
debug('NODE_ENV: %O', process.env.NODE_ENV);
/**
* @type {import('@babel/core').TransformOptions}
*/
module.exports = {
comments: false,
parserOpts: { strictMode: true },
assumptions: {
constantReexports: true
},
plugins: [
'@babel/plugin-proposal-export-default-from',
[
'module-resolver',
{
root: '.',
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
// ! If changed, also update these aliases in tsconfig.json,
// ! webpack.config.js, next.config.ts, eslintrc.js, and jest.config.js
alias: {
'^universe/(.*)$': './src/\\1',
'^multiverse/(.*)$': './lib/\\1',
'^testverse/(.*)$': './test/\\1',
'^externals/(.*)$': './external-scripts/\\1',
'^types/(.*)$': './types/\\1',
'^package$': `./package.json`
}
}
]
],
// ? Sub-keys under the "env" config key will augment the above
// ? configuration depending on the value of NODE_ENV and friends. Default
// ? is: development
env: {
// * Used by Jest and `npm test`
test: {
comments: true,
sourceMaps: 'both',
presets: [
['@babel/preset-env', { targets: { node: true } }],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? We don't care about minification
],
plugins: [
// ? Only active when testing, the plugin solves the following problem:
// ? https://stackoverflow.com/q/40771520/1367414
'explicit-exports-references'
]
},
// * Used when NODE_ENV == production (usually for generating types w/ tsc)
production: {
presets: [
[
'@babel/preset-env',
{
// ? https://babeljs.io/docs/en/babel-preset-env#modules
modules: 'auto',
targets: NODE_LTS,
exclude: ['proposal-dynamic-import']
}
],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? Minification is handled externally (e.g. by webpack)
]
},
// * Used by `npm run build` for compiling ESM to code output in ./dist
'production-esm': generateProductionEsmConfigObject((config) => {
config.presets[0][1].targets = NODE_LTS;
}),
// * Used by `npm run build` for fixing declaration file imports in ./dist
'production-types': {
comments: true,
plugins: [
['@babel/plugin-syntax-typescript', { dts: true }],
[
'transform-rewrite-imports',
{
replaceExtensions: {
// ? Ensure deep package.json imports resolve properly
'^../../../package.json$': '../../package.json',
// ? Ensure deep imports resolve properly
'^../../../(.*)$': '../$1'
}
}
]
]
},
// * Used by `npm run build:externals` for compiling to ESM code output in
// * ./external-scripts/bin
'production-external': generateProductionEsmConfigObject((config) => {
config.presets[0][1].targets = { node: true };
})
}
};
debug('exports: %O', module.exports);