-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
web-preset.js
77 lines (77 loc) · 3.16 KB
/
web-preset.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
"use strict";
/**
* Copyright © 2024 650 Industries.
* 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.
*
* A fork of `@react-native/babel-preset` but with everything unrelated to web/ssr removed.
* https://github.com/facebook/react-native/blob/2af1da42ff517232f1309efed7565fe9ddbbac77/packages/react-native-babel-preset/src/configs/main.js#L1
*/
Object.defineProperty(exports, "__esModule", { value: true });
// use `this.foo = bar` instead of `this.defineProperty('foo', ...)`
const loose = true;
const defaultPlugins = [
[require('@babel/plugin-syntax-flow')],
[require('babel-plugin-transform-flow-enums')],
[require('@babel/plugin-proposal-class-properties'), { loose }],
[require('@babel/plugin-transform-private-methods'), { loose }],
[require('@babel/plugin-transform-private-property-in-object'), { loose }],
[require('@babel/plugin-syntax-dynamic-import')],
[require('@babel/plugin-syntax-export-default-from')],
[require('@babel/plugin-syntax-nullish-coalescing-operator')],
[require('@babel/plugin-syntax-optional-chaining')],
// [require('@babel/plugin-transform-unicode-regex')],
];
module.exports = function (babel, options) {
const extraPlugins = [];
// NOTE: We also remove `@react-native/babel-plugin-codegen` since it doesn't seem needed on web.
if (!options || !options.disableImportExportTransform) {
extraPlugins.push([require('@babel/plugin-proposal-export-default-from')], [
require('@babel/plugin-transform-modules-commonjs'),
{
strict: false,
strictMode: false,
lazy: options.lazyImportExportTransform,
allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
},
]);
}
// 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 (!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: false,
...(isVersion && { version: options.enableBabelRuntime }),
},
]);
}
return {
comments: false,
compact: true,
presets: [
// TypeScript support
[require('@babel/preset-typescript'), { allowNamespaces: 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,
},
{
plugins: extraPlugins,
},
],
};
};