Skip to content

🎥 Make videos programmatically with React

`@remotion/bundler`: Avoid double-initializing `remotion_delayRenderTimeouts`

JonnyBurger85e0f446c72c
diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.ts
index 330d82e17a..f69def711f 100644
--- a/packages/bundler/src/index-html.ts
+++ b/packages/bundler/src/index-html.ts
@@ -112,6 +112,14 @@ export const indexHtml = ({
)};</script>`
: ''
}
+ ${
+ mode === 'dev'
+ ? `
+ <script>window.remotion_isStudio = true;</script>
+ <script>window.remotion_isReadOnlyStudio = false;</script>`
+ : ''
+ }
+
<script>window.remotion_staticFiles = ${JSON.stringify(publicFiles)}</script>
<script>window.remotion_installedPackages = ${JSON.stringify(installedDependencies)}</script>
<script>window.remotion_packageManager = ${JSON.stringify(packageManager)}</script>
diff --git a/packages/bundler/src/setup-environment.ts b/packages/bundler/src/setup-environment.ts
index 5b7a027163..d414fcfa61 100644
--- a/packages/bundler/src/setup-environment.ts
+++ b/packages/bundler/src/setup-environment.ts
@@ -1,8 +1,70 @@
-import {Internals} from 'remotion';
+// https://github.com/remotion-dev/remotion/issues/3412#issuecomment-1910120552
-Internals.setupEnvVariables();
+function getEnvVar() {
+ const parts = ['proc', 'ess', '.', 'en', 'v', '.', 'NOD', 'E_EN', 'V'];
+ return parts.join('');
+}
-Internals.CSSUtils.injectCSS(`
+const getEnvVariables = (): Record<string, string> => {
+ if (window.remotion_isStudio) {
+ // For the Studio, we already set the environment variables in index-html.ts.
+ // We just add NODE_ENV here.
+ if (!process.env.NODE_ENV) {
+ throw new Error(`${getEnvVar()} is not set`);
+ }
+
+ return {
+ NODE_ENV: process.env.NODE_ENV,
+ };
+ }
+
+ const param = window.remotion_envVariables;
+ if (!param) {
+ return {};
+ }
+
+ return {...JSON.parse(param), NODE_ENV: process.env.NODE_ENV};
+};
+
+const setupEnvVariables = () => {
+ const env = getEnvVariables();
+ if (!window.process) {
+ window.process = {} as NodeJS.Process;
+ }
+
+ if (!window.process.env) {
+ window.process.env = {};
+ }
+
+ Object.keys(env).forEach((key) => {
+ window.process.env[key] = env[key];
+ });
+};
+
+setupEnvVariables();
+
+const injected: {[key: string]: boolean} = {};
+
+export const injectCSS = (css: string) => {
+ // Skip in node
+ if (typeof document === 'undefined') {
+ return;
+ }
+
+ if (injected[css]) {
+ return;
+ }
+
+ const head = document.head || document.getElementsByTagName('head')[0];
+ const style = document.createElement('style');
+
+ style.appendChild(document.createTextNode(css));
+
+ head.prepend(style);
+ injected[css] = true;
+};
+
+injectCSS(`
.css-reset, .css-reset * {
font-size: 16px;
line-height: 1.5;
@@ -89,7 +151,4 @@ Internals.CSSUtils.injectCSS(`
background-color: #ffffff;
appearance: none;
}
-
-
-
`);
diff --git a/packages/core/src/delay-render.ts b/packages/core/src/delay-render.ts
index dc23c5cedf..4a2cf976cf 100644
--- a/packages/core/src/delay-render.ts
+++ b/packages/core/src/delay-render.ts
@@ -4,13 +4,12 @@ import {Log} from './log.js';
import type {RemotionEnvironment} from './remotion-environment-context.js';
import {truthy} from './truthy.js';
-if (typeof window !== 'undefined') {
- window.remotion_renderReady = false;
-}
-
let handles: number[] = [];
if (typeof window !== 'undefined') {
- window.remotion_delayRenderTimeouts = {};
+ window.remotion_renderReady = false;
+ if (!window.remotion_delayRenderTimeouts) {
+ window.remotion_delayRenderTimeouts = {};
+ }
}
export const DELAY_RENDER_CALLSTACK_TOKEN = 'The delayRender was called:';
diff --git a/packages/studio/src/previewEntry.tsx b/packages/studio/src/previewEntry.tsx
index 255590139e..c255091982 100644
--- a/packages/studio/src/previewEntry.tsx
+++ b/packages/studio/src/previewEntry.tsx
@@ -45,8 +45,6 @@ const renderToDOM = (content: React.ReactElement) => {
renderToDOM(<NoRegisterRoot />);
Internals.waitForRoot((NewRoot) => {
- window.remotion_isStudio = true;
- window.remotion_isReadOnlyStudio = false;
Internals.enableSequenceStackTraces();
renderToDOM(<Studio readOnly={false} rootComponent={NewRoot} />);