Skip to content

🎥 Make videos programmatically with React

revamp api

Jonny Burgerabad3406a0bd
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index d275e1de27..af5f740a8e 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -220,7 +220,11 @@ export async function bundle(...args: Arguments): Promise<string> {
remotionRoot: resolvedRemotionRoot,
previewServerCommand: null,
numberOfAudioTags: 0,
- publicFiles: readRecursively({folder: '.', startPath: from}),
+ publicFiles: readRecursively({
+ folder: '.',
+ startPath: from,
+ staticHash: '',
+ }),
includeFavicon: false,
title: 'Remotion Bundle',
});
diff --git a/packages/bundler/src/read-recursively.ts b/packages/bundler/src/read-recursively.ts
index a6f0ec6027..7226cd95d1 100644
--- a/packages/bundler/src/read-recursively.ts
+++ b/packages/bundler/src/read-recursively.ts
@@ -6,10 +6,12 @@ export const readRecursively = ({
folder,
output = [],
startPath,
+ staticHash,
}: {
folder: string;
startPath: string;
output?: StaticFile[];
+ staticHash: string;
}): StaticFile[] => {
const absFolder = path.join(startPath, folder);
const files = fs.readdirSync(absFolder);
@@ -20,25 +22,32 @@ export const readRecursively = ({
const stat = statSync(path.join(absFolder, file));
if (stat.isDirectory()) {
- readRecursively({startPath, folder: path.join(folder, file), output});
+ readRecursively({
+ startPath,
+ folder: path.join(folder, file),
+ output,
+ staticHash,
+ });
} else if (stat.isFile()) {
output.push({
- path: path.join(folder, file),
+ name: path.join(folder, file),
lastModified: Math.floor(stat.mtimeMs),
sizeInBytes: stat.size,
+ src: staticHash + '/' + path.join(folder, file),
});
} else if (stat.isSymbolicLink()) {
const realpath = fs.realpathSync(path.join(folder, file));
const realStat = fs.statSync(realpath);
if (realStat.isFile()) {
output.push({
- path: realpath,
+ name: realpath,
lastModified: Math.floor(realStat.mtimeMs),
sizeInBytes: realStat.size,
+ src: staticHash + '/' + realpath,
});
}
}
}
- return output.sort((a, b) => a.path.localeCompare(b.path));
+ return output.sort((a, b) => a.name.localeCompare(b.name));
};
diff --git a/packages/cli/src/preview-server/public-folder.ts b/packages/cli/src/preview-server/public-folder.ts
index 5f9c834b60..84a3c1e8b1 100644
--- a/packages/cli/src/preview-server/public-folder.ts
+++ b/packages/cli/src/preview-server/public-folder.ts
@@ -9,23 +9,32 @@ let files: StaticFile[] = [];
export const initPublicFolderWatch = ({
publicDir,
onUpdate,
+ staticHash,
}: {
publicDir: string;
remotionRoot: string;
onUpdate: () => void;
+ staticHash: string;
}) => {
- fetchFolder({publicDir});
- watchPublicFolder({publicDir, onUpdate});
+ fetchFolder({publicDir, staticHash});
+ watchPublicFolder({publicDir, onUpdate, staticHash});
};
-const fetchFolder = ({publicDir}: {publicDir: string}) => {
+const fetchFolder = ({
+ publicDir,
+ staticHash,
+}: {
+ publicDir: string;
+ staticHash: string;
+}) => {
files = BundlerInternals.readRecursively({
folder: '.',
startPath: publicDir,
+ staticHash,
}).map((f) => {
return {
...f,
- path: f.path.split(path.sep).join('/'),
+ name: f.name.split(path.sep).join('/'),
};
});
};
@@ -33,12 +42,14 @@ const fetchFolder = ({publicDir}: {publicDir: string}) => {
export const watchPublicFolder = ({
publicDir,
onUpdate,
+ staticHash,
}: {
publicDir: string;
onUpdate: () => void;
+ staticHash: string;
}) => {
watch(publicDir, {recursive: true}, () => {
- fetchFolder({publicDir});
+ fetchFolder({publicDir, staticHash});
onUpdate();
});
};
diff --git a/packages/cli/src/preview-server/start-server.ts b/packages/cli/src/preview-server/start-server.ts
index e98933be60..a0603271bc 100644
--- a/packages/cli/src/preview-server/start-server.ts
+++ b/packages/cli/src/preview-server/start-server.ts
@@ -1,6 +1,5 @@
import {BundlerInternals, webpack} from '@remotion/bundler';
import {RenderInternals} from '@remotion/renderer';
-import crypto from 'crypto';
import fs from 'fs';
import http from 'http';
import os from 'os';
@@ -15,22 +14,22 @@ import type {LiveEventsServer} from './live-events';
import {makeLiveEventsRouter} from './live-events';
import {handleRoutes} from './routes';
-export const startServer = async (
- entry: string,
- userDefinedComponent: string,
- options: {
- webpackOverride: WebpackOverrideFn;
- getCurrentInputProps: () => object;
- getEnvVariables: () => Record<string, string>;
- port: number | null;
- maxTimelineTracks?: number;
- remotionRoot: string;
- keyboardShortcutsEnabled: boolean;
- publicDir: string;
- userPassedPublicDir: string | null;
- poll: number | null;
- }
-): Promise<{
+export const startServer = async (options: {
+ entry: string;
+ userDefinedComponent: string;
+ webpackOverride: WebpackOverrideFn;
+ getCurrentInputProps: () => object;
+ getEnvVariables: () => Record<string, string>;
+ port: number | null;
+ maxTimelineTracks?: number;
+ remotionRoot: string;
+ keyboardShortcutsEnabled: boolean;
+ publicDir: string;
+ userPassedPublicDir: string | null;
+ poll: number | null;
+ hash: string;
+ hashPrefix: string;
+}): Promise<{
port: number;
liveEventsServer: LiveEventsServer;
}> => {
@@ -39,8 +38,8 @@ export const startServer = async (
);
const [, config] = BundlerInternals.webpackConfig({
- entry,
- userDefinedComponent,
+ entry: options.entry,
+ userDefinedComponent: options.userDefinedComponent,
outDir: tmpDir,
environment: 'development',
webpackOverride:
@@ -58,9 +57,6 @@ export const startServer = async (
const compiler = webpack(config);
- const hashPrefix = '/static-';
- const hash = `${hashPrefix}${crypto.randomBytes(6).toString('hex')}`;
-
const wdmMiddleware = wdm(compiler);
const whm = webpackHotMiddleware(compiler);
@@ -81,8 +77,8 @@ export const startServer = async (
})
.then(() => {
return handleRoutes({
- hash,
- hashPrefix,
+ hash: options.hash,
+ hashPrefix: options.hashPrefix,
request,
response,
liveEventsServer,
diff --git a/packages/cli/src/preview.ts b/packages/cli/src/preview.ts
index 543fdff70a..c6a734178d 100644
--- a/packages/cli/src/preview.ts
+++ b/packages/cli/src/preview.ts
@@ -1,4 +1,5 @@
import betterOpn from 'better-opn';
+import crypto from 'crypto';
import path from 'path';
import {chalk} from './chalk';
import {ConfigInternals} from './config';
@@ -104,6 +105,9 @@ export const previewCommand = async (remotionRoot: string, args: string[]) => {
remotionRoot,
});
+ const hashPrefix = '/static-';
+ const staticHash = `${hashPrefix}${crypto.randomBytes(6).toString('hex')}`;
+
initPublicFolderWatch({
publicDir,
remotionRoot,
@@ -115,24 +119,25 @@ export const previewCommand = async (remotionRoot: string, args: string[]) => {
});
});
},
+ staticHash,
});
- const {port, liveEventsServer} = await startServer(
- path.resolve(__dirname, 'previewEntry.js'),
- fullPath,
- {
- getCurrentInputProps: () => inputProps,
- getEnvVariables: () => envVariables,
- port: desiredPort,
- maxTimelineTracks: ConfigInternals.getMaxTimelineTracks(),
- remotionRoot,
- keyboardShortcutsEnabled: ConfigInternals.getKeyboardShortcutsEnabled(),
- publicDir,
- webpackOverride: ConfigInternals.getWebpackOverrideFn(),
- poll: ConfigInternals.getWebpackPolling(),
- userPassedPublicDir: ConfigInternals.getPublicDir(),
- }
- );
+ const {port, liveEventsServer} = await startServer({
+ entry: path.resolve(__dirname, 'previewEntry.js'),
+ userDefinedComponent: fullPath,
+ getCurrentInputProps: () => inputProps,
+ getEnvVariables: () => envVariables,
+ port: desiredPort,
+ maxTimelineTracks: ConfigInternals.getMaxTimelineTracks(),
+ remotionRoot,
+ keyboardShortcutsEnabled: ConfigInternals.getKeyboardShortcutsEnabled(),
+ publicDir,
+ webpackOverride: ConfigInternals.getWebpackOverrideFn(),
+ poll: ConfigInternals.getWebpackPolling(),
+ userPassedPublicDir: ConfigInternals.getPublicDir(),
+ hash: staticHash,
+ hashPrefix,
+ });
setLiveEventsListener(liveEventsServer);
const networkAddress = getNetworkAddress();
diff --git a/packages/core/src/get-static-files.ts b/packages/core/src/get-static-files.ts
index 14c0e40150..fc64ac5a12 100644
--- a/packages/core/src/get-static-files.ts
+++ b/packages/core/src/get-static-files.ts
@@ -1,5 +1,3 @@
-import type {StaticFile} from './static-file';
-
let warnedServer = false;
let warnedPlayer = false;
@@ -38,3 +36,10 @@ export const getStaticFiles = (): StaticFile[] => {
return window.remotion_staticFiles;
};
+
+export type StaticFile = {
+ src: string;
+ name: string;
+ sizeInBytes: number;
+ lastModified: number;
+};
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 5aa36be6e7..12f4a565d7 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,10 +1,10 @@
import './asset-types';
import {Clipper} from './Clipper';
import type {TAsset, TCompMetadata} from './CompositionManager';
+import type {StaticFile} from './get-static-files';
import {checkMultipleRemotionVersions} from './multiple-versions-warning';
import type {ClipRegion} from './NativeLayers';
import {Null} from './Null';
-import type {StaticFile} from './static-file';
declare global {
interface Window {
@@ -66,7 +66,7 @@ export * from './delay-render';
export * from './easing';
export * from './Folder';
export * from './freeze';
-export {getStaticFiles} from './get-static-files';
+export {getStaticFiles, StaticFile} from './get-static-files';
export * from './IFrame';
export * from './Img';
export * from './internals';
@@ -80,7 +80,7 @@ export {registerRoot} from './register-root';
export {Sequence} from './Sequence';
export {Series} from './series';
export * from './spring';
-export {staticFile, StaticFile} from './static-file';
+export {staticFile} from './static-file';
export * from './Still';
export type {PlayableMediaTag} from './timeline-position-state';
export {useCurrentFrame} from './use-current-frame';
diff --git a/packages/core/src/static-file.ts b/packages/core/src/static-file.ts
index c8874d3b0c..16857bee31 100644
--- a/packages/core/src/static-file.ts
+++ b/packages/core/src/static-file.ts
@@ -14,12 +14,6 @@ const inner = (path: string): string => {
return `/${trimLeadingSlash(path)}`;
};
-export type StaticFile = {
- path: string;
- sizeInBytes: number;
- lastModified: number;
-};
-
/**
* Reference a file from the public/ folder.
* If the file does not appear in the autocomplete, type the path manually.