Skip to content

Commit 13c5323

Browse files
authored
fix(metro-serializer-esbuild): eliminate prelude overhead (#3638)
1 parent 823c587 commit 13c5323

File tree

6 files changed

+438
-93
lines changed

6 files changed

+438
-93
lines changed

.changeset/chilly-pillows-drum.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rnx-kit/metro-serializer-esbuild": patch
3+
---
4+
5+
Reduce bundle size by manually injecting polyfills/preludes

.changeset/sour-carrots-change.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rnx-kit/third-party-notices": patch
3+
---
4+
5+
Handle absolute paths

packages/metro-serializer-esbuild/src/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ export function MetroSerializer(
179179
if (args.path === prelude) {
180180
return {
181181
/**
182-
* Add all the polyfills in this file. See the `inject` option
183-
* below for more details.
182+
* Add all the polyfills in this file. See the `entryPoints`
183+
* option below for more details.
184184
*
185185
* We must ensure that the content is ES5-friendly so esbuild
186186
* doesn't blow up when targeting ES5, e.g. use `var` instead of
@@ -198,7 +198,7 @@ export function MetroSerializer(
198198
*
199199
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function#description
200200
*/
201-
`export var global = new Function("return this;")();`,
201+
'global = typeof globalThis !== "undefined" ? globalThis : typeof global !== "undefined" ? global : typeof window !== "undefined" ? window : new Function("return this;")();',
202202

203203
/** Polyfills */
204204
...preModules
@@ -213,6 +213,11 @@ export function MetroSerializer(
213213
...options.runBeforeMainModule
214214
.filter((value) => dependencies.has(value))
215215
.map((value) => `require("${escapePath(value)}");`),
216+
217+
/**
218+
* Finally, import the entry point. This must always be last.
219+
*/
220+
`require("${escapePath(entryPoint)}");`,
216221
].join("\n"),
217222
};
218223
}
@@ -247,19 +252,18 @@ export function MetroSerializer(
247252
global: "global",
248253
},
249254
drop: buildOptions?.drop,
250-
entryPoints: [entryPoint],
251-
inject: [
252-
/**
253-
* A require call is generated and prepended to _all_ modules for each
254-
* injected file. This can increase the bundle size significantly if
255-
* there are many polyfills and modules. For just four polyfills (e.g.
256-
* `console.js`, `error-guard.js`, `Object.es7.js`, and
257-
* `InitializeCore.js`), we've seen an increase of ~180 KB in a small
258-
* to medium sized app. We can work around this issue by adding all
259-
* the polyfills in a single file that we inject here.
260-
*/
261-
prelude,
262-
],
255+
/**
256+
* We no longer use `inject` for polyfills and `runBeforeMainModule`
257+
* modules. A require call is generated and prepended to _all_ modules
258+
* for each injected file. This can increase the bundle size
259+
* significantly if there are many polyfills and modules. For just four
260+
* polyfills (e.g. `console.js`, `error-guard.js`, `Object.es7.js`, and
261+
* `InitializeCore.js`), we've seen an increase of ~180 KB in a small to
262+
* medium sized app. We work around this issue by adding all the
263+
* polyfills in a virtual file and make it the entry point. The virtual
264+
* file then imports the actual entry point.
265+
*/
266+
entryPoints: [prelude],
263267
legalComments: "none",
264268
logLevel: buildOptions?.logLevel ?? "error",
265269
metafile: Boolean(buildOptions?.analyze || buildOptions?.metafile),

0 commit comments

Comments
 (0)