forked from algolia/instantsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel.config.js
89 lines (80 loc) · 2.4 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
/* eslint-disable import/no-commonjs */
const wrapWarningWithDevCheck = require('./scripts/babel/wrap-warning-with-dev-check');
const isCJS = process.env.BABEL_ENV === 'cjs';
const isES = process.env.BABEL_ENV === 'es';
const isUMD = process.env.BABEL_ENV === 'umd';
const clean = x => x.filter(Boolean);
module.exports = api => {
const isTest = api.env('test');
const modules = isTest || isCJS ? 'commonjs' : false;
const targets = {};
if (isTest) {
targets.node = true;
} else {
targets.browsers = ['last 2 versions', 'ie >= 9'];
}
const testPlugins = [
'@babel/plugin-proposal-class-properties',
wrapWarningWithDevCheck,
];
const buildPlugins = clean([
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-react-constant-elements',
'babel-plugin-transform-react-pure-class-to-function',
wrapWarningWithDevCheck,
(isCJS || isES) && [
'inline-replace-variables',
{
__DEV__: {
type: 'node',
replacement: "process.env.NODE_ENV === 'development'",
},
},
],
// this plugin is used to test if we need polyfills, not to actually insert them
// only UMD, since cjs & esm have false positives due to imports
isUMD && [
'polyfill-es-shims',
{
method: 'usage-global',
targets: {
ie: 11,
},
shouldInjectPolyfill(name, defaultShouldInject) {
const exclude = [
// false positives (we access these from objects only)
'Array.prototype.item',
'String.prototype.item',
'Array.prototype.values',
'Function.prototype.name',
// we require polyfills for this already
'Array.prototype.includes',
// false positive (babel doesn't know types)
// this is actually only called on arrays
'String.prototype.includes',
];
if (defaultShouldInject && !exclude.includes(name)) {
throw new Error(
`Usage of a builtin which isn't allowed to be polyfilled: ${name}`
);
}
return false;
},
},
],
]);
return {
presets: [
'@babel/preset-typescript',
'@babel/preset-react',
[
'@babel/preset-env',
{
modules,
targets,
},
],
],
plugins: isTest ? testPlugins : buildPlugins,
};
};