/
main.js
218 lines (200 loc) Β· 6.48 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const passthroughSyntaxPlugins = require('../passthrough-syntax-plugins');
const lazyImports = require('./lazy-imports');
function isTypeScriptSource(fileName) {
return !!fileName && fileName.endsWith('.ts');
}
function isTSXSource(fileName) {
return !!fileName && fileName.endsWith('.tsx');
}
const defaultPlugins = [
[require('@babel/plugin-syntax-flow')],
[require('@babel/plugin-transform-block-scoping')],
[
require('@babel/plugin-proposal-class-properties'),
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
{loose: true},
],
[require('@babel/plugin-syntax-dynamic-import')],
[require('@babel/plugin-syntax-export-default-from')],
...passthroughSyntaxPlugins,
[require('@babel/plugin-transform-unicode-regex')],
];
const getPreset = (src, options) => {
const transformProfile =
(options && options.unstable_transformProfile) || 'default';
const isHermesStable = transformProfile === 'hermes-stable';
const isHermesCanary = transformProfile === 'hermes-canary';
const isHermes = isHermesStable || isHermesCanary;
const isNull = src == null;
const hasClass = isNull || src.indexOf('class') !== -1;
const extraPlugins = [];
if (!options.useTransformReactJSXExperimental) {
extraPlugins.push([
require('@babel/plugin-transform-react-jsx'),
{runtime: 'automatic'},
]);
}
if (!options || !options.disableImportExportTransform) {
extraPlugins.push(
[require('@babel/plugin-proposal-export-default-from')],
[
require('@babel/plugin-transform-modules-commonjs'),
{
strict: false,
strictMode: false, // prevent "use strict" injections
lazy:
options && options.lazyImportExportTransform != null
? options.lazyImportExportTransform
: importSpecifier => lazyImports.has(importSpecifier),
allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
},
],
);
}
if (hasClass) {
extraPlugins.push([require('@babel/plugin-transform-classes')]);
}
// TODO(gaearon): put this back into '=>' indexOf bailout
// and patch react-refresh to not depend on this transform.
extraPlugins.push([require('@babel/plugin-transform-arrow-functions')]);
if (!isHermes) {
extraPlugins.push([require('@babel/plugin-transform-computed-properties')]);
extraPlugins.push([require('@babel/plugin-transform-parameters')]);
extraPlugins.push([
require('@babel/plugin-transform-shorthand-properties'),
]);
extraPlugins.push([
require('@babel/plugin-proposal-optional-catch-binding'),
]);
extraPlugins.push([require('@babel/plugin-transform-function-name')]);
extraPlugins.push([require('@babel/plugin-transform-literals')]);
extraPlugins.push([require('@babel/plugin-transform-sticky-regex')]);
} else {
extraPlugins.push([
require('@babel/plugin-transform-named-capturing-groups-regex'),
]);
}
if (!isHermesCanary) {
extraPlugins.push([
require('@babel/plugin-transform-destructuring'),
{useBuiltIns: true},
]);
}
if (!isHermes && (isNull || hasClass || src.indexOf('...') !== -1)) {
extraPlugins.push(
[require('@babel/plugin-transform-spread')],
[
require('@babel/plugin-proposal-object-rest-spread'),
// Assume no dependence on getters or evaluation order. See https://github.com/babel/babel/pull/11520
{loose: true, useBuiltIns: true},
],
);
}
if (!isHermes && (isNull || src.indexOf('`') !== -1)) {
extraPlugins.push([
require('@babel/plugin-transform-template-literals'),
{loose: true}, // dont 'a'.concat('b'), just use 'a'+'b'
]);
}
if (isNull || src.indexOf('async') !== -1) {
extraPlugins.push([
require('@babel/plugin-proposal-async-generator-functions'),
]);
extraPlugins.push([require('@babel/plugin-transform-async-to-generator')]);
}
if (
isNull ||
src.indexOf('React.createClass') !== -1 ||
src.indexOf('createReactClass') !== -1
) {
extraPlugins.push([require('@babel/plugin-transform-react-display-name')]);
}
if (!isHermes && (isNull || src.indexOf('?.') !== -1)) {
extraPlugins.push([
require('@babel/plugin-proposal-optional-chaining'),
{loose: true},
]);
}
if (!isHermes && (isNull || src.indexOf('??') !== -1)) {
extraPlugins.push([
require('@babel/plugin-proposal-nullish-coalescing-operator'),
{loose: true},
]);
}
if (options && options.dev && !options.useTransformReactJSXExperimental) {
extraPlugins.push([require('@babel/plugin-transform-react-jsx-source')]);
extraPlugins.push([require('@babel/plugin-transform-react-jsx-self')]);
}
if (!options || options.enableBabelRuntime !== false) {
// Allows configuring a specific runtime version to optimize output
const isVersion = typeof options?.enableBabelRuntime === 'string';
extraPlugins.push([
require('@babel/plugin-transform-runtime'),
{
helpers: true,
regenerator: !isHermes,
...(isVersion && {version: options.enableBabelRuntime}),
},
]);
}
return {
comments: false,
compact: true,
overrides: [
// the flow strip types plugin must go BEFORE class properties!
// there'll be a test case that fails if you don't.
{
plugins: [require('@babel/plugin-transform-flow-strip-types')],
},
{
plugins: defaultPlugins,
},
{
test: isTypeScriptSource,
plugins: [
[
require('@babel/plugin-transform-typescript'),
{
isTSX: false,
allowNamespaces: true,
},
],
],
},
{
test: isTSXSource,
plugins: [
[
require('@babel/plugin-transform-typescript'),
{
isTSX: true,
allowNamespaces: true,
},
],
],
},
{
plugins: extraPlugins,
},
],
};
};
module.exports = options => {
if (options.withDevTools == null) {
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
if (!env || env === 'development') {
return getPreset(null, {...options, dev: true});
}
}
return getPreset(null, options);
};
module.exports.getPreset = getPreset;