forked from MetaMask/metamask-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabel.config.js
More file actions
163 lines (161 loc) · 5.52 KB
/
Copy pathbabel.config.js
File metadata and controls
163 lines (161 loc) · 5.52 KB
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
const ReactCompilerConfig = {
target: '18',
};
// Hermes (RN's bytecode compiler) does not accept dynamic `import()` syntax —
// even inside dead code branches — and aborts with "Invalid expression
// encountered", producing a `.app` with no JS bundle.
//
// Some shared controllers (e.g. PerpsController) and third-party packages
// (e.g. lilconfig) intentionally use `import(/* webpackIgnore: true */ x)` for
// webpack code splitting. Those constructs only make sense for webpack-based
// builds (extension); on Metro/Hermes they're parser-fatal.
//
// This visitor rewrites every `import(x)` call to
// `Promise.resolve().then(() => require(x))`, mirroring what
// babel-plugin-dynamic-import-node does, so Hermes never sees raw `import()`.
// Inlined to avoid pulling in another dependency.
//
// eslint-disable-next-line import-x/no-commonjs
const dynamicImportToRequire = ({ types: t }) => ({
name: 'transform-dynamic-import-to-require',
visitor: {
Import(path) {
const callExpr = path.parentPath;
if (!callExpr.isCallExpression()) {
return;
}
const arg = callExpr.node.arguments[0];
if (!arg) {
return;
}
const requireCall = t.callExpression(t.identifier('require'), [arg]);
const arrowFn = t.arrowFunctionExpression([], requireCall);
const replacement = t.callExpression(
t.memberExpression(
t.callExpression(
t.memberExpression(
t.identifier('Promise'),
t.identifier('resolve'),
),
[],
),
t.identifier('then'),
),
[arrowFn],
);
callExpr.replaceWith(replacement);
},
},
});
// eslint-disable-next-line import-x/no-commonjs
module.exports = {
ignore: [
(filename) =>
!!filename &&
(/\/ses\.cjs$/.test(filename) ||
/\/ses-hermes\.cjs$/.test(filename) ||
/\/react-native-lockdown\/src\/repair\.js$/.test(filename) ||
// expo/virtual/streams.js is a Metro polyfill — no require() available at that stage
// Babel must not transform it or it injects require("@babel/runtime/helpers/...")
/\/expo\/virtual\/streams\.js$/.test(filename)),
],
presets: ['babel-preset-expo'],
// Babel can find the plugin without the `babel-plugin-` prefix. Ex. `babel-plugin-react-compiler` -> `react-compiler`
plugins: [
[
'react-compiler',
{
target: '18',
sources: (filename) => {
// Match file paths or directories to include in the React Compiler.
const pathsToInclude = [
'app/components/Nav',
'app/components/UI/DeepLinkModal',
];
return pathsToInclude.some((path) => filename.includes(path));
},
},
],
'transform-inline-environment-variables',
dynamicImportToRequire,
// NOTE: react-native-reanimated/plugin must be listed LAST.
// Required by reanimated v3 to compile `'worklet'` directives; without it,
// gesture-handler worklets silently no-op on iOS Fabric and GestureDetector
// children (e.g. WebView) render at 0x0 (white screen).
'react-native-reanimated/plugin',
],
overrides: [
{
test: (f) => !!f?.includes('/node_modules/marked'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) =>
!!f?.includes('/node_modules/@metamask/profile-sync-controller'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) =>
!!f?.includes(
'/node_modules/@metamask/notification-services-controller',
),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) => !!f?.includes('/node_modules/@metamask/bridge-controller'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) => !!f?.includes('/node_modules/@nktkas/hyperliquid'),
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{ allowTopLevelThis: true },
],
],
},
{
test: (f) => !!f?.includes('/node_modules/@noble/secp256k1'),
plugins: [
[
'@babel/plugin-transform-modules-commonjs',
{ allowTopLevelThis: true },
],
],
},
{
test: (f) => !!f?.includes('/node_modules/@metamask/rpc-errors'),
plugins: [['@babel/plugin-transform-classes', { loose: true }]],
},
{
test: (f) => !!f?.includes('/app/lib/snaps/SnapsExecutionWebView.tsx'),
plugins: [['babel-plugin-inline-import', { extensions: ['.html'] }]],
},
// TODO: Remove this once we have a fix for the private methods
// Do not apply this plugin globally since it breaks FlatList props.getItem
{
test: (f) => !!f?.includes('/app/core/redux/ReduxService.ts'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) => !!f?.includes('/app/core/Engine/Engine.ts'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) =>
!!f?.includes('/app/core/NavigationService/NavigationService.ts'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
{
test: (f) => !!f?.includes('/app/core/OAuthService/OAuthLoginHandlers'),
plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
},
],
env: {
production: {
plugins: ['transform-remove-console'],
},
},
comments: false,
compact: true,
};