-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathmetro.config.js
More file actions
269 lines (232 loc) · 8.71 KB
/
Copy pathmetro.config.js
File metadata and controls
269 lines (232 loc) · 8.71 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const fs = require('fs');
const path = require('path');
const { getDefaultConfig } = require('expo/metro-config');
const { withNativeWind } = require('nativewind/metro');
// ---------------------------------------------------------------------------
// Auto code-splitting: route size threshold
// Routes whose synchronous dependency chunk exceeds this value trigger a
// warning and are recorded in .metro-route-sizes.json for CI inspection.
// ---------------------------------------------------------------------------
const ROUTE_SIZE_THRESHOLD = 100 * 1024; // 100 KB
/**
* Walks the synchronous dependency sub-graph rooted at `entryPath` and
* returns the total compiled byte size of every non-dynamic module.
*
* Metro marks dynamic import() edges with asyncType !== null, so those
* subtrees (i.e. React.lazy chunks) are correctly excluded from the count.
*
* @param {string} entryPath
* @param {Map<string, import('metro').Module>} allModules
* @returns {{ bytes: number, moduleCount: number }}
*/
function computeRouteSyncChunkSize(entryPath, allModules) {
const visited = new Set();
let bytes = 0;
function visit(modulePath) {
if (visited.has(modulePath)) return;
visited.add(modulePath);
const mod = allModules.get(modulePath);
if (!mod) return;
for (const output of mod.output ?? []) {
if (output.data?.code) {
bytes += Buffer.byteLength(output.data.code, 'utf8');
}
}
for (const dep of mod.dependencies?.values() ?? []) {
// asyncType: null → synchronous import (count it)
// asyncType: 'async' | 'prefetch' → dynamic import() boundary (skip)
if (dep.data?.data?.asyncType == null) {
visit(dep.absolutePath);
}
}
}
visit(entryPath);
return { bytes, moduleCount: visited.size };
}
/**
* Iterates the Metro module graph, identifies Expo Router route files
* (any .ts/.tsx/.js/.jsx under app/), and logs a warning for every route
* whose synchronous chunk exceeds ROUTE_SIZE_THRESHOLD.
*
* Results are written to .metro-route-sizes.json so CI and bundle auditing
* tools can consume them without re-parsing console output.
*
* @param {import('metro/src/DeltaBundler').ReadOnlyGraph} graph
*/
function analyzeRouteChunkSizes(graph) {
const projectRoot = __dirname;
const appPrefix = path.join(projectRoot, 'app') + path.sep;
const results = [];
for (const modulePath of graph.dependencies.keys()) {
if (!modulePath.startsWith(appPrefix)) continue;
if (modulePath.includes('node_modules')) continue;
if (!/\.(tsx?|jsx?)$/.test(modulePath)) continue;
const { bytes, moduleCount } = computeRouteSyncChunkSize(
modulePath,
graph.dependencies,
);
const route = path.relative(projectRoot, modulePath);
const exceeds = bytes > ROUTE_SIZE_THRESHOLD;
results.push({ route, bytes, moduleCount, exceeds });
if (exceeds) {
const kb = (bytes / 1024).toFixed(1);
console.warn(
`\n⚠️ [auto-split] Route chunk exceeds 100 KB threshold\n` +
` Route : ${route}\n` +
` Sync size : ${kb} KB (${moduleCount} modules)\n` +
` Fix : use React.lazy() for heavy component imports\n`,
);
}
}
if (results.length === 0) return;
try {
fs.writeFileSync(
path.join(projectRoot, '.metro-route-sizes.json'),
JSON.stringify(
{
threshold: ROUTE_SIZE_THRESHOLD,
generatedAt: new Date().toISOString(),
routes: results,
},
null,
2,
),
);
} catch {
// Non-fatal: report write failure must not break the build
}
}
/**
* Wraps an existing serializer (or Metro's default) with the route-size
* analysis pass. The analysis is a pure side-effect observer — the bundle
* output is not modified.
*
* @param {Function|undefined} existingSerializer
* @returns {Function}
*/
function wrapWithRouteSizeAnalyzer(existingSerializer) {
return async function routeSizeAnalyzerSerializer(
entryPoint,
preModules,
graph,
options,
) {
try {
analyzeRouteChunkSizes(graph);
} catch (err) {
// Analysis errors must never break the bundle
console.warn('[auto-split] Route size analysis failed:', err.message);
}
// Delegate to the upstream serializer if one exists (e.g. Expo's or NativeWind's).
if (typeof existingSerializer === 'function') {
return existingSerializer(entryPoint, preModules, graph, options);
}
// No upstream serializer — call Metro's built-in bundler directly.
// These module paths are stable across Metro 0.76–0.82 (Expo SDK 50–54).
const { default: baseJSBundle } = require('metro/src/DeltaBundler/Serializers/baseJSBundle');
const { default: bundleToString } = require('metro/src/DeltaBundler/Serializers/bundleToString');
const { code } = bundleToString(
baseJSBundle(entryPoint, preModules, graph, options),
);
return code;
};
}
// ---------------------------------------------------------------------------
// Compose the final Metro config
// ---------------------------------------------------------------------------
const projectRoot = __dirname;
const config = getDefaultConfig(projectRoot);
// ---------------------------------------------------------------------------
// Tree-shaking configuration for bundle size optimization (Issue #217)
// ---------------------------------------------------------------------------
// Enable tree-shaking optimizations for better bundle size
config.transformer.minifierConfig = {
...config.transformer.minifierConfig,
keep_classnames: true,
keep_fnames: true,
mangle: {
...config.transformer.minifierConfig?.mangle,
keep_classnames: true,
keep_fnames: true,
},
};
// Enable inline requires for better dead code elimination
config.transformer.inlineRequires = true;
// Enable additional optimization in production
if (process.env.NODE_ENV === 'production') {
config.transformer.minifierConfig = {
...config.transformer.minifierConfig,
compress: {
...config.transformer.minifierConfig?.compress,
dead_code: true,
unused: true,
conditionals: true,
evaluate: true,
booleans: true,
loops: true,
if_return: true,
join_vars: true,
drop_console: true,
},
};
}
const defaultResolveRequest = config.resolver.resolveRequest;
const tryResolve = (context, candidate, platform) => {
try {
return context.resolveRequest(context, candidate, platform);
} catch {
return null;
}
};
/**
* @/* maps to src/* in tsconfig, but Expo template files live at repo root
* (components/, hooks/, constants/). Resolve both locations.
*/
config.resolver.resolveRequest = (context, moduleName, platform) => {
if (moduleName.startsWith('@/src/')) {
const subpath = moduleName.slice('@/src/'.length);
const fromSrc = tryResolve(context, path.join(projectRoot, 'src', subpath), platform);
if (fromSrc) return fromSrc;
}
if (moduleName.startsWith('@/hooks/')) {
const subpath = moduleName.slice('@/hooks/'.length);
const fromRoot = tryResolve(context, path.join(projectRoot, 'hooks', subpath), platform);
if (fromRoot) return fromRoot;
const fromSrc = tryResolve(context, path.join(projectRoot, 'src/hooks', subpath), platform);
if (fromSrc) return fromSrc;
}
if (moduleName.startsWith('@/constants/')) {
const subpath = moduleName.slice('@/constants/'.length);
const fromRoot = tryResolve(context, path.join(projectRoot, 'constants', subpath), platform);
if (fromRoot) return fromRoot;
}
if (moduleName.startsWith('@/components/')) {
const subpath = moduleName.slice('@/components/'.length);
const fromSrc = tryResolve(
context,
path.join(projectRoot, 'src/components', subpath),
platform
);
if (fromSrc) return fromSrc;
const fromRoot = tryResolve(context, path.join(projectRoot, 'components', subpath), platform);
if (fromRoot) return fromRoot;
}
if (defaultResolveRequest) {
return defaultResolveRequest(context, moduleName, platform);
}
return context.resolveRequest(context, moduleName, platform);
};
// Apply NativeWind first so we capture its serializer (if any) before wrapping.
const nativewindConfig = withNativeWind(config, { input: './global.css' });
// Inject the route size analysis observer into the serializer chain.
nativewindConfig.serializer ??= {};
nativewindConfig.serializer.customSerializer = wrapWithRouteSizeAnalyzer(
nativewindConfig.serializer.customSerializer,
);
// Register Metro asset inlining plugin for Issue #369
nativewindConfig.transformer ??= {};
nativewindConfig.transformer.assetPlugins = [
...(nativewindConfig.transformer.assetPlugins || []),
require.resolve('./tools/metro-plugins/imageInlinePlugin.js'),
];
module.exports = nativewindConfig;