-
Notifications
You must be signed in to change notification settings - Fork 529
/
babel.config.js
149 lines (138 loc) · 4.23 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
144
145
146
147
148
149
module.exports = (api) => {
const clean = (x) => x.filter(Boolean);
const env = api.env().split(',');
const isTest = env.includes('test');
const isCJS = env.includes('cjs');
const isES = env.includes('es');
const isUMD = env.includes('umd');
const isRollup = env.includes('rollup');
const isParcel = env.includes('parcel');
const disableHoisting = env.includes('disableHoisting');
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',
'./scripts/babel/wrap-warning-with-dev-check',
];
const buildPlugins = clean([
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-private-methods',
'@babel/plugin-proposal-private-property-in-object',
(isCJS || isES || isUMD || isRollup) &&
!disableHoisting &&
'@babel/plugin-transform-react-constant-elements',
'babel-plugin-transform-react-pure-class-to-function',
'./scripts/babel/wrap-warning-with-dev-check',
isRollup && 'babel-plugin-transform-react-remove-prop-types',
(isCJS || isES || isParcel) && [
'inline-replace-variables',
{
__DEV__: {
type: 'node',
replacement: "process.env.NODE_ENV === 'development'",
},
},
],
isES && [
'./scripts/babel/extension-resolver',
{
// For verification, see test/module/packages-are-es-modules.mjs
modulesToResolve: [
// InstantSearch.js/es is an ES Module, so needs complete paths,
'instantsearch.js',
// React-DOM also fails if the paths are incomplete
'react-dom',
// `use-sync-external-store` also fails if the paths are incomplete
'use-sync-external-store',
// `next` imports as peer dependencies fail if paths are incomplete
'next',
],
},
],
// 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',
// false positive (spread)
'Object.getOwnPropertyDescriptors',
];
if (defaultShouldInject && !exclude.includes(name)) {
throw new Error(
`Usage of a builtin which isn't allowed to be polyfilled: ${name}`
);
}
return false;
},
},
],
]);
return {
presets: !isParcel
? [
'@babel/preset-typescript',
'@babel/preset-react',
[
'@babel/preset-env',
{
modules,
targets,
},
],
]
: [],
plugins: isTest ? testPlugins : buildPlugins,
overrides: [
{
test: 'packages/react-*',
plugins: [
[
'@babel/plugin-transform-runtime',
{
corejs: false,
helpers: true,
regenerator: false,
useESModules: isES || isRollup,
},
],
],
},
{
test: 'packages/instantsearch-ui-components',
plugins: [
[
'@babel/plugin-transform-runtime',
{
corejs: false,
helpers: true,
regenerator: false,
},
],
],
},
],
// jsx is transpiled, so the comment should no longer be present in the final files
shouldPrintComment: (value) => !value.startsWith('* @jsx'),
};
};