Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Pin tsconfigRaw so Remotion never resolves ambient TypeScript (#101)

Nika Siradze8801f22afffa
diff --git a/remotion/bundle-cache.mjs b/remotion/bundle-cache.mjs
new file mode 100644
index 0000000..d908f37
--- /dev/null
+++ b/remotion/bundle-cache.mjs
@@ -0,0 +1,54 @@
+import { bundle } from "@remotion/bundler";
+import path from "path";
+import fs from "fs";
+import crypto from "crypto";
+import { fileURLToPath } from "url";
+import { webpackOverride } from "./webpack-override.mjs";
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const PROJECT_ROOT = path.resolve(__dirname, "..");
+const CACHE_ROOT = process.env.PODCLI_CACHE_DIR
+ ? path.resolve(process.env.PODCLI_CACHE_DIR)
+ : path.join(PROJECT_ROOT, "data", "cache");
+
+export const CACHE_DIR = path.join(CACHE_ROOT, "remotion-bundle");
+const HASH_FILE = path.join(CACHE_DIR, ".hash");
+const ENTRY_POINT = path.join(__dirname, "src", "index.ts");
+
+/**
+ * A bundle is a product of the compositions and the webpack config, so both
+ * feed the cache key. Keying on either alone silently serves a bundle built
+ * from inputs that no longer exist.
+ */
+function currentHash() {
+ const hash = crypto.createHash("md5");
+
+ function walk(dir) {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) walk(full);
+ else if (entry.isFile()) hash.update(fs.readFileSync(full));
+ }
+ }
+
+ walk(path.join(__dirname, "src"));
+ hash.update(fs.readFileSync(path.join(__dirname, "webpack-override.mjs")));
+ return hash.digest("hex");
+}
+
+export async function getCachedBundle({ onBundle } = {}) {
+ const want = currentHash();
+ if (fs.existsSync(HASH_FILE) && fs.existsSync(path.join(CACHE_DIR, "index.html"))) {
+ if (fs.readFileSync(HASH_FILE, "utf-8").trim() === want) return CACHE_DIR;
+ }
+
+ onBundle?.();
+ fs.mkdirSync(CACHE_DIR, { recursive: true });
+ const location = await bundle({
+ entryPoint: ENTRY_POINT,
+ outDir: CACHE_DIR,
+ webpackOverride,
+ });
+ fs.writeFileSync(HASH_FILE, want);
+ return location;
+}
diff --git a/remotion/remotion.config.ts b/remotion/remotion.config.ts
index e8d4dba..c46219f 100644
--- a/remotion/remotion.config.ts
+++ b/remotion/remotion.config.ts
@@ -1,4 +1,6 @@
import { Config } from "@remotion/cli/config";
+import { webpackOverride } from "./webpack-override.mjs";
Config.setVideoImageFormat("jpeg");
Config.setOverwriteOutput(true);
+Config.overrideWebpackConfig(webpackOverride);
diff --git a/remotion/render-bookend.mjs b/remotion/render-bookend.mjs
index 2688079..dd5ed57 100644
--- a/remotion/render-bookend.mjs
+++ b/remotion/render-bookend.mjs
@@ -14,25 +14,14 @@
* [--bg "#0B0B0F"] [--accent "#FFE000"] [--fps 30] [--width 1080] [--height 1920]
*/
-import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
+import { getCachedBundle } from "./bundle-cache.mjs";
import path from "path";
import fs from "fs";
import os from "os";
import crypto from "crypto";
import { spawnSync } from "child_process";
-import { fileURLToPath } from "url";
-const __dirname = path.dirname(fileURLToPath(import.meta.url));
-const PROJECT_ROOT = path.resolve(__dirname, "..");
-// Share the (project-independent) composition bundle with render.mjs via
-// PODCLI_CACHE_DIR so bookends reuse the prewarmed bundle instead of rebuilding
-// into the runtime dir.
-const CACHE_ROOT = process.env.PODCLI_CACHE_DIR
- ? path.resolve(process.env.PODCLI_CACHE_DIR)
- : path.join(PROJECT_ROOT, "data", "cache");
-const CACHE_DIR = path.join(CACHE_ROOT, "remotion-bundle");
-const ENTRY_POINT = path.join(__dirname, "src", "index.ts");
function parseArgs() {
const args = process.argv.slice(2);
@@ -46,11 +35,6 @@ function parseArgs() {
return opts;
}
-async function getBundle() {
- // Reuse podcli's cached bundle if present; otherwise build fresh.
- if (fs.existsSync(path.join(CACHE_DIR, "index.html"))) return CACHE_DIR;
- return await bundle({ entryPoint: ENTRY_POINT, outDir: CACHE_DIR });
-}
async function main() {
const opts = parseArgs();
@@ -76,7 +60,9 @@ async function main() {
bookendAccent: opts.accent || "#FFE000",
};
- const bundleLocation = await getBundle();
+ const bundleLocation = await getCachedBundle({
+ onBundle: () => console.log(" Remotion: bundling (first run, or src/config changed)..."),
+ });
const composition = await selectComposition({ serveUrl: bundleLocation, id: "Bookend", inputProps });
const seed = `${path.resolve(opts.output)}:${process.pid}`;
diff --git a/remotion/render.mjs b/remotion/render.mjs
index 536b047..b93adbc 100644
--- a/remotion/render.mjs
+++ b/remotion/render.mjs
@@ -17,22 +17,13 @@
* node remotion/render.mjs --prebundle # Bundle only, no render
*/
-import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
+import { getCachedBundle } from "./bundle-cache.mjs";
import path from "path";
import fs from "fs";
import os from "os";
import crypto from "crypto";
-import { fileURLToPath } from "url";
-const __dirname = path.dirname(fileURLToPath(import.meta.url));
-const PROJECT_ROOT = path.resolve(__dirname, "..");
-const CACHE_ROOT = process.env.PODCLI_CACHE_DIR
- ? path.resolve(process.env.PODCLI_CACHE_DIR)
- : path.join(PROJECT_ROOT, "data", "cache");
-const CACHE_DIR = path.join(CACHE_ROOT, "remotion-bundle");
-const BUNDLE_HASH_FILE = path.join(CACHE_DIR, ".hash");
-const ENTRY_POINT = path.join(__dirname, "src", "index.ts");
const BOOLEAN_FLAGS = new Set(["prebundle", "keep-overlay"]);
@@ -53,56 +44,6 @@ function parseArgs() {
return opts;
}
-/**
- * Hash the remotion/src/ directory to detect changes.
- */
-function hashSrcDir() {
- const srcDir = path.join(__dirname, "src");
- const hash = crypto.createHash("md5");
-
- function walk(dir) {
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- walk(full);
- } else if (entry.isFile()) {
- hash.update(fs.readFileSync(full));
- }
- }
- }
-
- walk(srcDir);
- return hash.digest("hex");
-}
-
-/**
- * Get or create a cached bundle. Only re-bundles when src/ changes.
- */
-async function getCachedBundle() {
- const currentHash = hashSrcDir();
-
- // Check if cached bundle is still valid
- if (fs.existsSync(BUNDLE_HASH_FILE)) {
- const cachedHash = fs.readFileSync(BUNDLE_HASH_FILE, "utf-8").trim();
- const bundleIndex = path.join(CACHE_DIR, "index.html");
- if (cachedHash === currentHash && fs.existsSync(bundleIndex)) {
- return CACHE_DIR;
- }
- }
-
- // Bundle fresh
- console.log(" Remotion: bundling (first run or src changed)...");
- fs.mkdirSync(CACHE_DIR, { recursive: true });
-
- const bundleLocation = await bundle({
- entryPoint: ENTRY_POINT,
- outDir: CACHE_DIR,
- });
-
- // Save hash
- fs.writeFileSync(BUNDLE_HASH_FILE, currentHash);
- return bundleLocation;
-}
async function main() {
const opts = parseArgs();
@@ -110,7 +51,7 @@ async function main() {
// Prebundle mode — just bundle and exit
if (opts.prebundle) {
const t0 = Date.now();
- const loc = await getCachedBundle();
+ const loc = await getCachedBundle({ onBundle: () => console.log(" Remotion: bundling (first run, or src/config changed)...") });
console.log(`Remotion bundle ready at ${loc} (${Date.now() - t0}ms)`);
return;
}
@@ -131,7 +72,7 @@ async function main() {
// Get cached bundle first (needed to copy assets into it)
const t0 = Date.now();
- const bundleLocation = await getCachedBundle();
+ const bundleLocation = await getCachedBundle({ onBundle: () => console.log(" Remotion: bundling (first run, or src/config changed)...") });
const bundleMs = Date.now() - t0;
if (bundleMs > 1000) {
console.log(` bundled in ${(bundleMs / 1000).toFixed(1)}s`);
diff --git a/remotion/test-render.mjs b/remotion/test-render.mjs
index 2167746..6e321b0 100644
--- a/remotion/test-render.mjs
+++ b/remotion/test-render.mjs
@@ -10,53 +10,15 @@
* node remotion/test-render.mjs subtle
*/
-import { bundle } from "@remotion/bundler";
import { renderMedia, selectComposition } from "@remotion/renderer";
+import { getCachedBundle } from "./bundle-cache.mjs";
import path from "path";
import fs from "fs";
-import crypto from "crypto";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PROJECT_ROOT = path.resolve(__dirname, "..");
-const CACHE_ROOT = process.env.PODCLI_CACHE_DIR
- ? path.resolve(process.env.PODCLI_CACHE_DIR)
- : path.join(PROJECT_ROOT, "data", "cache");
-const CACHE_DIR = path.join(CACHE_ROOT, "remotion-bundle");
-const BUNDLE_HASH_FILE = path.join(CACHE_DIR, ".hash");
-const ENTRY_POINT = path.join(__dirname, "src", "index.ts");
-function hashSrcDir() {
- const srcDir = path.join(__dirname, "src");
- const hash = crypto.createHash("md5");
- function walk(dir) {
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
- const full = path.join(dir, entry.name);
- if (entry.isDirectory()) walk(full);
- else if (entry.isFile()) hash.update(fs.readFileSync(full));
- }
- }
- walk(srcDir);
- return hash.digest("hex");
-}
-
-async function getCachedBundle() {
- const currentHash = hashSrcDir();
- if (fs.existsSync(BUNDLE_HASH_FILE)) {
- const cachedHash = fs.readFileSync(BUNDLE_HASH_FILE, "utf-8").trim();
- if (
- cachedHash === currentHash &&
- fs.existsSync(path.join(CACHE_DIR, "index.html"))
- ) {
- return CACHE_DIR;
- }
- }
- console.log("Bundling (first run or src changed)...");
- fs.mkdirSync(CACHE_DIR, { recursive: true });
- const loc = await bundle({ entryPoint: ENTRY_POINT, outDir: CACHE_DIR });
- fs.writeFileSync(BUNDLE_HASH_FILE, currentHash);
- return loc;
-}
const styleName = process.argv[2] || "branded";
@@ -111,7 +73,7 @@ const inputProps = {
async function main() {
const t0 = Date.now();
- const bundleLocation = await getCachedBundle();
+ const bundleLocation = await getCachedBundle({ onBundle: () => console.log("Bundling (first run, or src/config changed)...") });
const bundleMs = Date.now() - t0;
console.log(`Bundle: ${bundleMs}ms (${bundleMs < 100 ? "cached" : "fresh"})`);
diff --git a/remotion/webpack-override.mjs b/remotion/webpack-override.mjs
new file mode 100644
index 0000000..4c05915
--- /dev/null
+++ b/remotion/webpack-override.mjs
@@ -0,0 +1,28 @@
+/**
+ * Remotion's esbuild-loader reads a tsconfig via `require('typescript')` unless
+ * `tsconfigRaw` is already set. The runtime lives under the user's home dir, so
+ * Node walks up and resolves whatever TypeScript it finds there — a TypeScript 7
+ * install has no `ts.sys` and hard-crashes the bundle. Pinning tsconfigRaw skips
+ * that lookup. Empty matches the shipped default: the runtime bundle has no
+ * typescript, and every composition imports React, so esbuild's classic JSX
+ * transform is correct.
+ */
+const TSCONFIG_RAW = { compilerOptions: {} };
+
+const pinTsconfig = (entry) =>
+ entry &&
+ typeof entry === "object" &&
+ typeof entry.loader === "string" &&
+ entry.loader.includes("esbuild-loader")
+ ? { ...entry, options: { ...entry.options, tsconfigRaw: TSCONFIG_RAW } }
+ : entry;
+
+export const webpackOverride = (config) => ({
+ ...config,
+ module: {
+ ...config.module,
+ rules: (config.module?.rules ?? []).map((rule) =>
+ Array.isArray(rule?.use) ? { ...rule, use: rule.use.map(pinTsconfig) } : rule,
+ ),
+ },
+});