Skip to content

πŸŽ₯ Make videos programmatically with React

Remove Turbopack's duplicate copies of the compiler assets from the Vibe Code build

JonnyBurger50e2dc03f660
diff --git a/packages/template-vibe-code/README.md b/packages/template-vibe-code/README.md
index 5af1f16f60..61c3053300 100644
--- a/packages/template-vibe-code/README.md
+++ b/packages/template-vibe-code/README.md
@@ -106,7 +106,7 @@ npx remotion upgrade
`npm run build` creates a production build. Saving to disk is disabled outside of development β€” the editor still works as an in-memory playground.
-The compiler's Web Worker and WebAssembly binary are copied to `public/compiler` by `scripts/build-preview.mjs` and loaded from there (see `workerUrl` in `src/editor/hooks/use-compiler.ts`), so they do not go through the Next.js bundler and both Turbopack and webpack work.
+The compiler's Web Worker and WebAssembly binary are copied to `public/compiler` by `scripts/build-preview.mjs` and loaded from there (see `workerUrl` in `src/editor/hooks/use-compiler.ts`), so they do not go through the Next.js bundler and both Turbopack and webpack work. Turbopack still emits a second copy of them into `.next/static/media` because `@remotion/browser-bundler` references them as a fallback; `scripts/prune-compiler-duplicates.mjs` removes those 32 MB after `next build`.
### Static export
diff --git a/packages/template-vibe-code/package.json b/packages/template-vibe-code/package.json
index 3c31cffccc..82703c986f 100644
--- a/packages/template-vibe-code/package.json
+++ b/packages/template-vibe-code/package.json
@@ -9,7 +9,7 @@
"homepage": "https://www.remotion.dev",
"scripts": {
"dev": "node scripts/build-preview.mjs && next dev",
- "build": "node scripts/build-preview.mjs && next build",
+ "build": "node scripts/build-preview.mjs && next build && node scripts/prune-compiler-duplicates.mjs",
"start": "next start",
"build-preview": "node scripts/build-preview.mjs",
"lint": "eslint . && tsc --noEmit",
diff --git a/packages/template-vibe-code/scripts/build-preview.mjs b/packages/template-vibe-code/scripts/build-preview.mjs
index f7589d1389..4328055fcb 100644
--- a/packages/template-vibe-code/scripts/build-preview.mjs
+++ b/packages/template-vibe-code/scripts/build-preview.mjs
@@ -15,6 +15,7 @@ import { copyFile, mkdir } from "node:fs/promises";
import { createRequire } from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";
+import { compilerAssets } from "./compiler-assets.mjs";
const root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const require = createRequire(import.meta.url);
@@ -42,11 +43,7 @@ const compilerDist = path.join(
);
const compilerDir = path.join(root, "public", "compiler");
await mkdir(compilerDir, { recursive: true });
-for (const asset of [
- "browser-bundler-worker.js",
- "rspack.wasm32-wasi.wasm",
- "wasi-worker-browser.mjs",
-]) {
+for (const asset of compilerAssets) {
await copyFile(path.join(compilerDist, asset), path.join(compilerDir, asset));
}
diff --git a/packages/template-vibe-code/scripts/compiler-assets.mjs b/packages/template-vibe-code/scripts/compiler-assets.mjs
new file mode 100644
index 0000000000..fbab070e6c
--- /dev/null
+++ b/packages/template-vibe-code/scripts/compiler-assets.mjs
@@ -0,0 +1,8 @@
+// The files of the browser bundler's Web Worker: the worker itself, Rspack
+// compiled to WebAssembly and the WASI worker it runs in. They are served from
+// public/compiler, see build-preview.mjs and use-compiler.ts.
+export const compilerAssets = [
+ "browser-bundler-worker.js",
+ "rspack.wasm32-wasi.wasm",
+ "wasi-worker-browser.mjs",
+];
diff --git a/packages/template-vibe-code/scripts/prune-compiler-duplicates.mjs b/packages/template-vibe-code/scripts/prune-compiler-duplicates.mjs
new file mode 100644
index 0000000000..b00093a3de
--- /dev/null
+++ b/packages/template-vibe-code/scripts/prune-compiler-duplicates.mjs
@@ -0,0 +1,40 @@
+// Removes the copies of the compiler assets that Turbopack emits into
+// .next/static/media (and the static export of it) after `next build`.
+//
+// @remotion/browser-bundler refers to its worker with
+// `new URL("./browser-bundler-worker.js", import.meta.url)` as a fallback for
+// apps that do not pass `workerUrl`, and the worker refers to the WebAssembly
+// binary and the WASI worker the same way. Turbopack bundles everything such a
+// URL points to, which adds a second 32 MB copy of the files to the build.
+// This app always passes `workerUrl` (see use-compiler.ts) and loads them from
+// public/compiler instead, so the copies are never requested.
+
+import { readdir, rm } from "node:fs/promises";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { compilerAssets } from "./compiler-assets.mjs";
+
+const root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
+
+// Turbopack inserts a hash: browser-bundler-worker.1s-aib4z2khvm.js
+const isCompilerAsset = (fileName) =>
+ compilerAssets.some((asset) => {
+ const extension = path.extname(asset);
+ return (
+ fileName.startsWith(`${asset.slice(0, -extension.length)}.`) &&
+ fileName.endsWith(extension)
+ );
+ });
+
+let removed = 0;
+for (const mediaDir of [".next/static/media", "out/_next/static/media"]) {
+ const dir = path.join(root, mediaDir);
+ for (const fileName of await readdir(dir).catch(() => [])) {
+ if (isCompilerAsset(fileName)) {
+ await rm(path.join(dir, fileName));
+ removed++;
+ }
+ }
+}
+
+console.log(`Removed ${removed} duplicate compiler assets from the build`);