Skip to content

🎥 Make videos programmatically with React

`@remotion/bundler`: Show composition source locations in read-only Studio

JonnyBurger6dfd88ca3ab7
diff --git a/packages/bundler/src/rspack-config.ts b/packages/bundler/src/rspack-config.ts
index 4f3b054656..15fc1fdd37 100644
--- a/packages/bundler/src/rspack-config.ts
+++ b/packages/bundler/src/rspack-config.ts
@@ -101,10 +101,7 @@ export const rspackConfig = async ({
: null,
reactScan: getReactScanEntryPoint(environment),
environmentSetup: require.resolve('./setup-environment'),
- sequenceStackTraces:
- environment === 'development'
- ? require.resolve('./setup-sequence-stack-traces')
- : null,
+ sequenceStackTraces: require.resolve('./setup-sequence-stack-traces'),
userDefinedComponent,
reactShim: require.resolve('../react-shim.js'),
studioRenderEntry: entry,
diff --git a/packages/bundler/src/setup-sequence-stack-traces.ts b/packages/bundler/src/setup-sequence-stack-traces.ts
index 78f7e56742..7e7fda1fca 100644
--- a/packages/bundler/src/setup-sequence-stack-traces.ts
+++ b/packages/bundler/src/setup-sequence-stack-traces.ts
@@ -1,9 +1,10 @@
import React from 'react';
import JsxRuntimeDev from 'react/jsx-dev-runtime';
import JsxRuntime from 'react/jsx-runtime';
-import {Internals} from 'remotion';
+import {Composition, Folder, Internals, Still} from 'remotion';
const componentsToAddStacksTo = Internals.getComponentsToAddStacksTo();
+const componentsToAddStacksToInReadOnlyStudio = [Composition, Folder, Still];
const sequenceComponent = Internals.getSequenceComponent();
const internalStackProp = Internals.REMOTION_INTERNAL_STACK_PROP;
const studioOriginalSourcePrefix = 'studio-original://';
@@ -50,7 +51,11 @@ const enableProxy = <
): T => {
return new Proxy(api, {
apply(target, thisArg, argArray) {
- if (componentsToAddStacksTo.includes(argArray[0])) {
+ const component = argArray[0];
+ const shouldAddStack =
+ process.env.NODE_ENV !== 'production' ||
+ componentsToAddStacksToInReadOnlyStudio.includes(component);
+ if (shouldAddStack && componentsToAddStacksTo.includes(component)) {
const [first, props, ...rest] = argArray;
const children = isCreateElement
? rest.length === 0
@@ -85,7 +90,18 @@ const enableProxy = <
});
};
-React.createElement = enableProxy(originalCreateElement, true, null);
-JsxRuntime.jsx = enableProxy(originalJsx, false, null);
-JsxRuntime.jsxs = enableProxy(originalJsxs, false, null);
-JsxRuntimeDev.jsxDEV = enableProxy(originalJsxDev, false, 4);
+// Renderer pages set this value before the bundle loads. Avoid proxying every
+// JSX call while rendering; production stack traces are only needed by Studio.
+const shouldEnableStackTraces =
+ process.env.NODE_ENV !== 'production' ||
+ (typeof window !== 'undefined' &&
+ typeof window.remotion_puppeteerTimeout === 'undefined');
+
+if (shouldEnableStackTraces) {
+ React.createElement = enableProxy(originalCreateElement, true, null);
+ JsxRuntime.jsx = enableProxy(originalJsx, false, null);
+ JsxRuntime.jsxs = enableProxy(originalJsxs, false, null);
+ if (originalJsxDev) {
+ JsxRuntimeDev.jsxDEV = enableProxy(originalJsxDev, false, 4);
+ }
+}
diff --git a/packages/bundler/src/test/override-order.test.ts b/packages/bundler/src/test/override-order.test.ts
index de2d20ea99..988b95af5b 100644
--- a/packages/bundler/src/test/override-order.test.ts
+++ b/packages/bundler/src/test/override-order.test.ts
@@ -102,6 +102,20 @@ test('includes React Scan only in an explicitly enabled development bundle', asy
).toBe(false);
}
+ for (const config of [
+ productionWebpackConfig,
+ productionRspackConfig,
+ developmentWebpackConfig,
+ developmentRspackConfig,
+ ]) {
+ expect(Array.isArray(config.entry)).toBe(true);
+ expect(
+ (config.entry as string[]).some((entry) =>
+ entry.includes('setup-sequence-stack-traces'),
+ ),
+ ).toBe(true);
+ }
+
for (const config of [developmentWebpackConfig, developmentRspackConfig]) {
expect(Array.isArray(config.entry)).toBe(true);
expect(
diff --git a/packages/bundler/src/test/setup-sequence-stack-traces.test.ts b/packages/bundler/src/test/setup-sequence-stack-traces.test.ts
index 71c577de8f..350033e42c 100644
--- a/packages/bundler/src/test/setup-sequence-stack-traces.test.ts
+++ b/packages/bundler/src/test/setup-sequence-stack-traces.test.ts
@@ -1,7 +1,7 @@
import {expect, test} from 'bun:test';
import React from 'react';
import JsxRuntimeDev from 'react/jsx-dev-runtime';
-import {Internals} from 'remotion';
+import {Composition, Internals} from 'remotion';
test('injects a namespaced source stack without conflicting with application props', async () => {
const Component: React.FC<{readonly stack: string}> = () => null;
@@ -28,15 +28,34 @@ test('injects a namespaced source stack without conflicting with application pro
};
expect(existingProps._remotionInternalStack).toBe('existing-source-stack');
+ const previousNodeEnv = process.env.NODE_ENV;
Object.defineProperty(globalThis, 'window', {
configurable: true,
- value: {remotion_cwd: '/project'},
+ value: {
+ remotion_cwd: '/project',
+ },
});
try {
+ process.env.NODE_ENV = 'production';
+ const renderingElement = React.createElement(Component, {
+ stack: 'application-stack',
+ });
+ expect(
+ (renderingElement.props as {_remotionInternalStack?: string})
+ ._remotionInternalStack,
+ ).toBeUndefined();
+
const sourceElement = JsxRuntimeDev.jsxDEV(
- Component,
- {stack: 'application-stack'},
+ Composition,
+ {
+ component: Component,
+ durationInFrames: 30,
+ fps: 30,
+ height: 1080,
+ id: 'read-only-stack-test',
+ width: 1920,
+ },
undefined,
false,
{
@@ -53,6 +72,12 @@ test('injects a namespaced source stack without conflicting with application pro
'Error\n at remotionOriginalSource (studio-original://.%2Fsrc%2FVideo.tsx:12:4)',
);
} finally {
+ if (previousNodeEnv === undefined) {
+ delete process.env.NODE_ENV;
+ } else {
+ process.env.NODE_ENV = previousNodeEnv;
+ }
+
Reflect.deleteProperty(globalThis, 'window');
}
});
diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.ts
index 459d76e31f..e88432bcd8 100644
--- a/packages/bundler/src/webpack-config.ts
+++ b/packages/bundler/src/webpack-config.ts
@@ -71,10 +71,7 @@ export const webpackConfig = async ({
: null,
reactScan: getReactScanEntryPoint(environment),
environmentSetup: require.resolve('./setup-environment'),
- sequenceStackTraces:
- environment === 'development'
- ? require.resolve('./setup-sequence-stack-traces')
- : null,
+ sequenceStackTraces: require.resolve('./setup-sequence-stack-traces'),
userDefinedComponent,
reactShim: require.resolve('../react-shim.js'),
studioRenderEntry: entry,
diff --git a/packages/it-tests/src/bundle/bundle-studio.test.ts b/packages/it-tests/src/bundle/bundle-studio.test.ts
index c8bcb61f64..0399f92111 100644
--- a/packages/it-tests/src/bundle/bundle-studio.test.ts
+++ b/packages/it-tests/src/bundle/bundle-studio.test.ts
@@ -1,7 +1,7 @@
import {expect, test} from 'bun:test';
import {existsSync, readFileSync} from 'fs';
import path from 'path';
-import {RenderInternals, openBrowser} from '@remotion/renderer';
+import {openBrowser} from '@remotion/renderer';
import {
getRemotionVersionFromIndexHtml,
VERSION,
@@ -48,22 +48,27 @@ test(
const version = getRemotionVersionFromIndexHtml(indexHtmlContent);
expect(version).toBe(VERSION);
- const {port, close} = await RenderInternals.serveStatic(
- path.join(process.cwd(), '..', 'example', 'build'),
- {
- port: null,
- offthreadVideoThreads: 1,
- downloadMap: RenderInternals.makeDownloadMap(48000),
- indent: false,
- logLevel: 'info',
- offthreadVideoCacheSizeInBytes: null,
- remotionRoot: path.join(process.cwd(), '..', 'example'),
- binariesDirectory: null,
- forceIPv4: false,
+ const server = Bun.serve({
+ port: 0,
+ fetch: async (request) => {
+ const url = new URL(request.url);
+ const requestedPath =
+ url.pathname === '/' ? 'index.html' : url.pathname.slice(1);
+ const filePath = path.resolve(folder, requestedPath);
+ if (!filePath.startsWith(`${folder}${path.sep}`)) {
+ return new Response('Forbidden', {status: 403});
+ }
+
+ const file = Bun.file(filePath);
+ if (!(await file.exists())) {
+ return new Response('Not found', {status: 404});
+ }
+
+ return new Response(file);
},
- );
+ });
await tab.goto({
- url: `http://localhost:${port}`,
+ url: `http://localhost:${server.port}`,
timeout: 10000,
options: {},
});
@@ -77,6 +82,36 @@ test(
});
expect(result.toString()).toBeGreaterThan(1);
+ const sourceLocation = await tab.evaluateHandle(async () => {
+ let openedInspector = false;
+ for (let attempt = 0; attempt < 100; attempt++) {
+ const inspectorSourceLocation = document.querySelector(
+ '[aria-label="Inspector source location"]',
+ );
+ if (inspectorSourceLocation?.textContent?.includes('Root.tsx:')) {
+ return inspectorSourceLocation.textContent;
+ }
+ if (inspectorSourceLocation) {
+ openedInspector = true;
+ }
+
+ if (!openedInspector) {
+ const inspectorToggle = document.querySelector<HTMLElement>(
+ '[data-sidebar-toggle="right"]',
+ );
+ if (inspectorToggle) {
+ openedInspector = true;
+ inspectorToggle.click();
+ }
+ }
+
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ }
+
+ return null;
+ });
+ expect(sourceLocation.toString()).toMatch(/Root\.tsx:\d+/);
+
const fontLoaded = await tab.evaluateHandle(() => {
let loaded = false;
document.fonts.forEach((font) => {
@@ -95,8 +130,15 @@ test(
});
expect(Number(orphanedFontTimeouts.toString())).toBe(0);
+ await Promise.all([
+ result.dispose(),
+ sourceLocation.dispose(),
+ fontLoaded.dispose(),
+ orphanedFontTimeouts.dispose(),
+ ]);
+ await tab.close();
await (await browser).close({silent: false});
- await close();
+ await server.stop(true);
},
- {timeout: 20000},
+ {timeout: 20_000},
);