Skip to content

🎥 Make videos programmatically with React

Defer production stack traces until Studio loads

JonnyBurgercf9ed1f33e6d
diff --git a/packages/bundler/src/setup-sequence-stack-traces.ts b/packages/bundler/src/setup-sequence-stack-traces.ts
index 2418d50f23..714c181606 100644
--- a/packages/bundler/src/setup-sequence-stack-traces.ts
+++ b/packages/bundler/src/setup-sequence-stack-traces.ts
@@ -86,18 +86,26 @@ const enableProxy = <
});
};
-// 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');
+let stackTracesEnabled = false;
-if (shouldEnableStackTraces) {
+const enableSequenceStackTraces = () => {
+ if (stackTracesEnabled) {
+ return;
+ }
+
+ stackTracesEnabled = true;
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);
}
+};
+
+if (typeof window !== 'undefined') {
+ window.remotion_enableSequenceStackTraces = enableSequenceStackTraces;
+}
+
+if (process.env.NODE_ENV !== 'production') {
+ enableSequenceStackTraces();
}
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 6378d8dd3f..b50e83ff40 100644
--- a/packages/bundler/src/test/setup-sequence-stack-traces.test.ts
+++ b/packages/bundler/src/test/setup-sequence-stack-traces.test.ts
@@ -3,31 +3,9 @@ import React from 'react';
import JsxRuntimeDev from 'react/jsx-dev-runtime';
import {Composition, Internals} from 'remotion';
-test('injects a namespaced source stack without conflicting with application props', async () => {
+test('defers and idempotently enables source stack injection in production', async () => {
const Component: React.FC<{readonly stack: string}> = () => null;
Internals.addSequenceStackTraces(Component);
- await import('../setup-sequence-stack-traces');
-
- const element = React.createElement(Component, {stack: 'application-stack'});
- const props = element.props as typeof element.props & {
- readonly _remotionInternalStack: string;
- };
-
- expect(props.stack).toBe('application-stack');
- expect(props._remotionInternalStack).toContain('Error');
-
- const existingStackElement = React.createElement(Component, {
- stack: 'application-stack',
- _remotionInternalStack: 'existing-source-stack',
- } as React.ComponentProps<typeof Component> & {
- readonly _remotionInternalStack: string;
- });
- const existingProps =
- existingStackElement.props as typeof existingStackElement.props & {
- readonly _remotionInternalStack: string;
- };
- expect(existingProps._remotionInternalStack).toBe('existing-source-stack');
-
const previousNodeEnv = process.env.NODE_ENV;
Object.defineProperty(globalThis, 'window', {
configurable: true,
@@ -38,6 +16,38 @@ test('injects a namespaced source stack without conflicting with application pro
try {
process.env.NODE_ENV = 'production';
+ const originalCreateElement = React.createElement;
+ await import('../setup-sequence-stack-traces');
+ expect(React.createElement).toBe(originalCreateElement);
+
+ window.remotion_enableSequenceStackTraces?.();
+ const proxiedCreateElement = React.createElement;
+ expect(proxiedCreateElement).not.toBe(originalCreateElement);
+ window.remotion_enableSequenceStackTraces?.();
+ expect(React.createElement).toBe(proxiedCreateElement);
+
+ const element = React.createElement(Component, {
+ stack: 'application-stack',
+ });
+ const props = element.props as typeof element.props & {
+ readonly _remotionInternalStack: string;
+ };
+
+ expect(props.stack).toBe('application-stack');
+ expect(props._remotionInternalStack).toContain('Error');
+
+ const existingStackElement = React.createElement(Component, {
+ stack: 'application-stack',
+ _remotionInternalStack: 'existing-source-stack',
+ } as React.ComponentProps<typeof Component> & {
+ readonly _remotionInternalStack: string;
+ });
+ const existingProps =
+ existingStackElement.props as typeof existingStackElement.props & {
+ readonly _remotionInternalStack: string;
+ };
+ expect(existingProps._remotionInternalStack).toBe('existing-source-stack');
+
const renderingElement = React.createElement(Component, {
stack: 'application-stack',
});
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 42e828dca5..7948e6e5ff 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -106,6 +106,7 @@ declare global {
remotion_isPlayer: boolean;
remotion_isStudio: boolean;
remotion_isReadOnlyStudio: boolean;
+ remotion_enableSequenceStackTraces: (() => void) | null;
remotion_isBuilding: undefined | (() => void);
remotion_finishedBuilding: undefined | (() => void);
siteVersion: '11';
diff --git a/packages/studio/src/renderEntry.tsx b/packages/studio/src/renderEntry.tsx
index 5eab9f1367..5b83da6fd2 100644
--- a/packages/studio/src/renderEntry.tsx
+++ b/packages/studio/src/renderEntry.tsx
@@ -308,6 +308,7 @@ const renderContent = (Root: React.FC) => {
window.remotion_isStudio = true;
window.remotion_isReadOnlyStudio = true;
window.remotion_inputProps = '{}';
+ window.remotion_enableSequenceStackTraces?.();
renderToDOM(<StudioInternals.Studio readOnly rootComponent={Root} />);
})