forked from u-wave/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel.config.js
102 lines (89 loc) · 2.9 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
'use strict';
const pkg = require('./package.json');
function importMetaToCommonJs() {
return {
visitor: {
MetaProperty(path) {
if (path.node.property.name !== 'meta') {
return;
}
const parent = path.parentPath;
if (!parent.isMemberExpression() || parent.node.property.name !== 'url') {
return;
}
parent.replaceWithSourceString('require("url").pathToFileURL(__filename)');
},
},
};
}
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');
// 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');
const targetIsModern = !targetIsLegacy;
let targets = {};
let bugfixes = false;
if (callerIsNode) {
targets = { node: 'current', browsers: '' };
} else if (targetIsNode) {
targets = { node: '10.0.0', browsers: '' };
}
if (targetIsModern) {
targets = { esmodules: true, browsers: '' };
bugfixes = true;
}
const preset = {
presets: [
['@babel/preset-env', {
modules: false,
targets,
bugfixes,
}],
['@babel/preset-react', {
development: env === 'development',
runtime: 'automatic',
}],
],
plugins: [
'@babel/plugin-proposal-class-properties',
['@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,
}],
],
};
if (callerIsNode) {
preset.plugins.push(
'@babel/plugin-transform-modules-commonjs',
'module:babel-plugin-dynamic-import-node',
);
}
if (env === 'production') {
preset.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) {
preset.plugins.push(
'module:react-refresh/babel',
);
}
if (callerIsNode) {
preset.plugins.push(importMetaToCommonJs);
}
return preset;
};