-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbabel.config.cjs
124 lines (107 loc) · 3.31 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
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
'use strict';
const pkg = require('./package.json');
// Replaces import.meta.url with a CommonJS equivalent, and import.meta.* with `undefined`.
function importMetaToCommonJs() {
return {
visitor: {
MetaProperty(path) {
if (path.node.property.name !== 'meta') {
return;
}
const parent = path.parentPath;
if (!parent.isMemberExpression()) {
return;
}
if (parent.node.property.name === 'url') {
parent.replaceWithSourceString('require("url").pathToFileURL(__filename)');
} else {
parent.replaceWithSourceString('undefined');
}
},
},
};
}
module.exports = (api, envOverride) => {
const env = envOverride || process.env.BABEL_ENV || process.env.NODE_ENV || 'development';
// Command-line version override.
const browsers = process.env.BROWSERSLIST;
api.cache(() => `${env}${browsers || ''}`);
// When the caller is @babel/register, we expect to immediately run the output, in the current
// Node.js version.
const callerIsNode = api.caller((caller) => caller && (caller.name === '@babel/register' || caller.name === 'babel-jest'));
// When the target is `node`, we're doing a webpack build for server-side code. The output will
// run in any Node.js version supported by our public API.
const targetIsNode = api.caller((caller) => caller && caller.target === 'node');
// Check if our output should support older browsers.
const targetIsLegacy = api.caller((caller) => caller && caller.compiler === 'app-legacy');
let browserslistEnv = 'production';
let targets;
if (callerIsNode) {
targets = { node: 'current', browsers: '' };
} else if (targetIsNode) {
targets = { node: '12.0.0', browsers: '' };
}
if (targetIsLegacy) {
browserslistEnv = 'legacy';
}
const reactConfig = {
test: /\.jsx$/,
presets: [
['@babel/preset-react', {
development: env === 'development',
runtime: 'automatic',
}],
],
plugins: [],
};
const config = {
browserslistEnv,
assumptions: {
constantSuper: true,
noClassCalls: true,
noDocumentAll: true,
noNewArrows: true,
privateFieldsAsProperties: true,
},
presets: [
['@babel/preset-env', {
modules: false,
bugfixes: true,
}],
],
plugins: [
['@babel/plugin-transform-runtime', {
version: pkg.dependencies['@babel/runtime'],
// When targeting Node.js for any reason, dependencies are external to the webpack bundle,
// and must be `require()`-able.
useESModules: !callerIsNode && !targetIsNode,
corejs: false,
}],
],
overrides: [reactConfig],
};
if (targets) {
config.targets = targets;
}
if (callerIsNode) {
config.plugins.push(
'@babel/plugin-transform-modules-commonjs',
);
}
if (env === 'production') {
reactConfig.plugins.push(
'@babel/plugin-transform-react-constant-elements',
'@babel/plugin-transform-react-inline-elements',
['transform-react-remove-prop-types', { mode: 'remove' }],
);
}
if (env === 'development' && !targetIsNode && !callerIsNode) {
reactConfig.plugins.push(
'module:react-refresh/babel',
);
}
if (callerIsNode) {
config.plugins.push(importMetaToCommonJs);
}
return config;
};