🎥 Make videos programmatically with React
start with static server
Jonny Burgerb5f47fc842ae
diff --git a/packages/bundler/src/bundler.ts b/packages/bundler/src/bundler.tsindex f46fb504a9..0a141681ce 100644--- a/packages/bundler/src/bundler.ts+++ b/packages/bundler/src/bundler.ts@@ -5,6 +5,7 @@ import path from 'path';import {Internals, WebpackOverrideFn} from 'remotion';import {promisify} from 'util';import webpack from 'webpack';+import {indexHtml} from './static-preview';import {webpackConfig} from './webpack-config';const entry = require.resolve('./renderEntry');@@ -53,12 +54,10 @@ export const bundle = async (throw new Error(errors[0].message + '\n' + errors[0].details);}- const indexHtmlDir = path.join(__dirname, '..', 'web', 'index.html');- if (process.platform === 'win32') {- await execa('copy', [indexHtmlDir, outDir]);- } else {- await execa('cp', [indexHtmlDir, outDir]);- }+ // TODO: Make this better in Lambda+ // TODO: Copy /public folder+ const html = indexHtml(`/`);+ fs.writeFileSync(path.join(outDir, 'index.html'), html);return outDir;};diff --git a/packages/bundler/src/start-server.ts b/packages/bundler/src/start-server.tsindex cc59d28ede..c0c5998c4f 100644--- a/packages/bundler/src/start-server.ts+++ b/packages/bundler/src/start-server.ts@@ -12,6 +12,8 @@ import {getDesiredPort} from './get-port';import {getProjectInfo} from './project-info';import {isUpdateAvailableWithTimeout} from './update-available';import {webpackConfig} from './webpack-config';+import crypto from 'crypto';+import {indexHtml} from './static-preview';export const startServer = async (entry: string,@@ -42,7 +44,12 @@ export const startServer = async (});const compiler = webpack(config);- app.use(express.static(path.join(__dirname, '..', 'web')));+ const hash = crypto.randomBytes(6).toString('hex');++ app.use(+ `/static-${hash}`,+ express.static(path.join(process.cwd(), 'public'))+ );app.use(webpackDevMiddleware(compiler));app.use(webpackHotMiddleware(compiler, {@@ -79,8 +86,9 @@ export const startServer = async (res.sendFile(path.join(__dirname, '..', 'web', 'favicon.png'));});- app.use('*', (req, res) => {- res.sendFile(path.join(__dirname, '..', 'web', 'index.html'));+ app.use('*', (_, res) => {+ res.set('content-type', 'text/html');+ res.end(indexHtml(hash));});const desiredPort = options?.port ?? Internals.getServerPort();diff --git a/packages/bundler/src/static-preview.ts b/packages/bundler/src/static-preview.tsnew file mode 100644index 0000000000..d3daea256c--- /dev/null+++ b/packages/bundler/src/static-preview.ts@@ -0,0 +1,24 @@+export const indexHtml = (staticHash: string) =>+ `+<!DOCTYPE html>+<html lang="en">+ <head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <link rel="preconnect" href="https://fonts.gstatic.com" />+ <link rel="icon" type="image/png" href="/favicon.png" />+ <title>Remotion Preview</title>+ </head>+ <body>+ <script>window.remotion_staticBase = "${staticHash}";</script>+ <div id="container"></div>+ <div id="menuportal-0"></div>+ <div id="menuportal-1"></div>+ <div id="menuportal-2"></div>+ <div id="menuportal-3"></div>+ <div id="menuportal-4"></div>+ <div id="menuportal-5"></div>+ <script src="/bundle.js"></script>+ </body>+</html>+`.trim();diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 030a017629..a52ff06afb 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -6,6 +6,7 @@ declare global {interface Window {ready: boolean;getStaticCompositions: () => TCompMetadata[];+ remotion_staticBase: string;remotion_setFrame: (frame: number) => void;remotion_initialFrame: number;remotion_collectAssets: () => TAsset[];@@ -42,3 +43,4 @@ export * from './use-frame';export * from './use-video-config';export * from './video';export * from './video-config';+export {staticFile} from './static-file';diff --git a/packages/core/src/static-file.ts b/packages/core/src/static-file.tsnew file mode 100644index 0000000000..7eba8cd30f--- /dev/null+++ b/packages/core/src/static-file.ts@@ -0,0 +1,24 @@+const trimLeadingSlash = (path: string) => {+ if (path.startsWith('/')) {+ return path.substr(1);+ }++ return path;+};++const inner = (path: string): string => {+ if (window.remotion_staticBase) {+ return `/static-${window.remotion_staticBase}/${trimLeadingSlash(path)}`;+ }++ return `/${trimLeadingSlash(path)}`;+};++export const staticFile = (path: string) => {+ const preparsed = inner(path);+ if (!preparsed.startsWith('/')) {+ return `/${preparsed}`;+ }++ return preparsed;+};diff --git a/packages/eslint-plugin/src/rules/no-string-assets.ts b/packages/eslint-plugin/src/rules/no-string-assets.tsindex 428b2ce6f1..e89f175df9 100644--- a/packages/eslint-plugin/src/rules/no-string-assets.ts+++ b/packages/eslint-plugin/src/rules/no-string-assets.ts@@ -9,7 +9,7 @@ type Options = [];type MessageIds = "NoStringAssets";const NoStringAssets = [- "Don't reference local assets by string, use an import statement instead.",+ "Don't reference local assets by string, use an import statement or staticFile() instead.","See: https://www.remotion.dev/docs/assets",].join("\n");diff --git a/packages/example/public/logo.png b/packages/example/public/logo.pngnew file mode 100644index 0000000000..a35f38446aBinary files /dev/null and b/packages/example/public/logo.png differdiff --git a/packages/example/src/StaticServer/index.tsx b/packages/example/src/StaticServer/index.tsxnew file mode 100644index 0000000000..39fa752a7f--- /dev/null+++ b/packages/example/src/StaticServer/index.tsx@@ -0,0 +1,11 @@+import React from 'react';+import {Img, staticFile} from 'remotion';++export const StaticDemo: React.FC = () => {+ return (+ <>+ <Img src={staticFile('logo.png')} />+ <Img src="/logo.png" />+ </>+ );+};diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex 08d2bb365b..08ac70c4b8 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -8,6 +8,7 @@ import {OrbScene} from './Orb';import RemoteVideo from './RemoteVideo';import {SkipZeroFrame} from './SkipZeroFrame';import {SeriesTesting} from './StaggerTesting';+import {StaticDemo} from './StaticServer';import {TenFrameTester} from './TenFrameTester';import ThreeBasic from './ThreeBasic';import {VideoSpeed} from './VideoSpeed';@@ -283,6 +284,12 @@ export const Index: React.FC = () => {durationInFrames={100}/><Still id="Orb" component={OrbScene} width={2000} height={2000} />+ <Still+ id="static-file"+ component={StaticDemo}+ width={2000}+ height={2000}+ /></>);};