-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathbabel.config.js
More file actions
181 lines (165 loc) · 7.62 KB
/
Copy pathbabel.config.js
File metadata and controls
181 lines (165 loc) · 7.62 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require('dotenv').config();
/**
* Expo SDK 56 replaces global fetch with expo/fetch unless this is set at transform time.
* metro.config.js sets it for the Metro process; babel needs it here so release bundles inline the value.
*/
process.env.EXPO_PUBLIC_USE_RN_FETCH = process.env.EXPO_PUBLIC_USE_RN_FETCH ?? '1';
const BaseReactCompilerConfig = require('./config/babel/reactCompilerConfig');
const {expoInlineEnvVars} = require('babel-preset-expo/build/plugins/inline-env-vars');
const ReactCompilerConfig = {
...BaseReactCompilerConfig,
sources: (filename) => !filename.includes('tests/') && !filename.includes('node_modules/'),
};
/**
* Custom plugin that prints a file name when it's being processed by babel.
* Disabled by default. To enable, set DEBUG_BABEL_TRACE=true in the environment.
*/
function traceTransformer() {
return {
visitor: {
Program(path, state) {
console.log('🔧 Transforming file:', state.filename);
},
},
};
}
const isTestEnv = process.env.BABEL_ENV === 'test' || process.env.NODE_ENV === 'test';
const metro = {
presets: [require('@react-native/babel-preset')],
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig], // must run first!
// This is needed due to a react-native bug: https://github.com/facebook/react-native/issues/29084#issuecomment-1030732709
// It is included in metro-react-native-babel-preset but needs to be before plugin-proposal-class-properties or FlatList will break
'@babel/plugin-transform-flow-strip-types',
// Inline EXPO_PUBLIC_* env vars into release bundles (e.g. EXPO_PUBLIC_USE_RN_FETCH to keep RN fetch).
expoInlineEnvVars,
['@babel/plugin-proposal-class-properties', {loose: true}],
['@babel/plugin-proposal-private-methods', {loose: true}],
['@babel/plugin-proposal-private-property-in-object', {loose: true}],
/* Fullstory */
'@fullstory/react-native',
[
'@fullstory/babel-plugin-annotate-react',
{
native: true,
},
],
// Import alias for native devices
[
'module-resolver',
{
extensions: [
'.native.js',
'.native.jsx',
'.native.ts',
'.native.tsx',
'.js',
'.jsx',
'.ts',
'.tsx',
'.ios.js',
'.ios.jsx',
'.ios.ts',
'.ios.tsx',
'.android.js',
'.android.jsx',
'.android.ts',
'.android.tx',
],
alias: {
'@assets': './assets',
'@components': './src/components',
'@hooks': './src/hooks',
'@libs': './src/libs',
'@navigation': './src/libs/Navigation',
'@pages': './src/pages',
'@prompts': './prompts',
'@styles': './src/styles',
// This path is provide alias for files like `ONYXKEYS` and `CONST`.
'@src': './src',
'@userActions': './src/libs/actions',
'@github': './.github',
'@selectors': './src/selectors',
},
},
],
'@babel/plugin-transform-export-namespace-from',
// The worklets babel plugin needs to be last, as stated here: https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/getting-started/
// Skipped under Jest (isTestEnv): reanimated and worklets are fully mocked in jest/setup.ts, so the
// plugin's worklet transform is wasted work - and its output is not cached, so a heavy nested
// Babel transform re-runs on every test run.
...(isTestEnv
? []
: [
[
'react-native-worklets/plugin',
{
bundleMode: true,
// In Bundle Mode, an import that a worklet captures in its closure becomes a "remote function"
// and throws when called synchronously on a Worklet Runtime. The `react-native-live-markdown`
// ExpensiMark parser runs on the LiveMarkdownRuntime and directly imports `ExpensiMark`,
// `unescapeText` (from `expensify-common/utils`) and `decode` (from `html-entities`) into its
// worklets, so those imports must be forwarded to avoid the "Tried to synchronously call a
// Remote Function" crash. Their transitive dependencies (expensify-common's `Str`, `punycode`,
// `awesome-phonenumber`, html-entities' internals, ...) do NOT need forwarding: metro bundles
// the whole `require()` graph onto the Worklet Runtime, so they resolve locally there.
// `relativePaths` only needs to cover live-markdown's own `./rangeUtils` (expensify-common and
// html-entities resolve to CommonJS, where `relativePaths` forwarding never fires).
// See https://docs.swmansion.com/react-native-worklets/docs/bundleMode/importForwarding
importForwarding: {
moduleNames: ['expensify-common', 'expensify-common/utils', 'html-entities'],
relativePaths: ['@expensify/react-native-live-markdown'],
},
},
],
]),
],
env: {
production: {
plugins: [['transform-remove-console', {exclude: ['error', 'warn']}]],
},
test: {
plugins: ['@babel/plugin-transform-dynamic-import'],
},
},
};
if (process.env.DEBUG_BABEL_TRACE) {
metro.plugins.push(traceTransformer);
}
/*
* We use <React.Profiler> and react-native-performance to capture/monitor stats
* By default <React.Profiler> is disabled in production as it adds small overhead
* When CAPTURE_METRICS is set we're explicitly saying that we want to capture metrics
* To enable the <Profiler> for release builds we add these aliases */
if (process.env.CAPTURE_METRICS === 'true') {
const path = require('path');
const profilingRenderer = path.resolve(__dirname, './node_modules/react-native/Libraries/Renderer/implementations/ReactNativeRenderer-profiling');
metro.plugins.push([
'module-resolver',
{
root: ['./'],
alias: {
'ReactNativeRenderer-prod': profilingRenderer,
'scheduler/tracing': 'scheduler/tracing-profiling',
},
},
'extra-alias',
]);
}
module.exports = (api) => {
if (!process.env.KNIP) {
console.debug('babel.config.js');
console.debug(' - api.version:', api.version);
console.debug(' - api.env:', api.env());
console.debug(' - process.env.NODE_ENV:', process.env.NODE_ENV);
console.debug(' - process.env.BABEL_ENV:', process.env.BABEL_ENV);
}
// For `react-native` (iOS/Android) caller will be "metro"
// For jest, it will be babel-jest
// The web build and Storybook (Rsbuild) don't call into this file at all
const runningIn = api.caller((args = {}) => args.name);
if (!process.env.KNIP) {
console.debug(' - running in: ', runningIn);
}
return ['metro', 'babel-jest'].includes(runningIn) ? metro : {};
};