/
babel.config.js
86 lines (81 loc) · 2.57 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
// * Every now and then, we adopt best practices from CRA
// * https://tinyurl.com/yakv4ggx
// ? https://nodejs.org/en/about/releases
const NODE_LTS = 'maintained node versions';
const debug = require('debug')(`${require('./package.json').name}:babel-config`);
// ? Fix relative local imports referencing package.json (.dist/esm/...)
const transformRenameImport = [
'transform-rename-import',
{
replacements: [{ original: '../package.json', replacement: '../../package.json' }]
}
];
module.exports = {
parserOpts: { strictMode: true },
plugins: [
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-function-bind',
'@babel/plugin-transform-typescript',
// ? Interoperable named CJS imports for free
'transform-default-named-imports'
],
// ? 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: {
sourceMaps: 'both',
presets: [
['@babel/preset-env', { targets: { node: true } }],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? We don't care about minification
]
},
// * Used by `npm run build`
production: {
presets: [
[
'@babel/preset-env',
{
// ? https://github.com/babel/babel-loader/issues/521#issuecomment-441466991
//modules: false,
targets: NODE_LTS
}
],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? Webpack will handle minification
]
},
// * Used by `npm run build-externals`
external: {
presets: [
['@babel/preset-env', { targets: { node: true } }],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? Webpack will handle minification
],
plugins: [transformRenameImport]
},
// * Used for compiling ESM code output somewhere in ./dist
esm: {
presets: [
[
'@babel/preset-env',
{
// ? https://babeljs.io/docs/en/babel-preset-env#modules
modules: false,
targets: NODE_LTS
}
],
['@babel/preset-typescript', { allowDeclareFields: true }]
// ? The end user will handle minification
],
plugins: [
// ? Ensure all local imports without extensions now end in .mjs
['add-import-extension', { extension: 'mjs' }],
transformRenameImport
]
}
}
};
debug('exports: %O', module.exports);