Skip to content

🎥 Make videos programmatically with React

distinguish whether public folder exists or not

Jonny Burger82900d317720
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index ea6f093527..013a149963 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -257,6 +257,7 @@ export async function bundle(...args: Arguments): Promise<string> {
includeFavicon: false,
title: 'Remotion Bundle',
renderDefaults: undefined,
+ publicFolderExists: true
});
fs.writeFileSync(path.join(outDir, 'index.html'), html);
diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.ts
index 60d8e75fc9..75eec88fef 100644
--- a/packages/bundler/src/index-html.ts
+++ b/packages/bundler/src/index-html.ts
@@ -48,6 +48,7 @@ export const indexHtml = ({
includeFavicon,
title,
renderDefaults,
+ publicFolderExists,
}: {
staticHash: string;
baseDir: string;
@@ -59,6 +60,7 @@ export const indexHtml = ({
renderQueue: unknown | null;
numberOfAudioTags: number;
publicFiles: StaticFile[];
+ publicFolderExists: boolean;
includeFavicon: boolean;
title: string;
renderDefaults: RenderDefaults | undefined;
@@ -122,6 +124,7 @@ ${
: ''
}
<script>window.remotion_staticFiles = ${JSON.stringify(publicFiles)}</script>
+ <script>window.remotion_publicFolderExists = ${publicFolderExists}</script>
<div id="${Internals.REMOTION_STUDIO_CONTAINER_ELEMENT}"></div>
<div id="menuportal-0"></div>
diff --git a/packages/cli/src/editor/components/AssetSelector.tsx b/packages/cli/src/editor/components/AssetSelector.tsx
index a1807f070c..1f052ffcde 100644
--- a/packages/cli/src/editor/components/AssetSelector.tsx
+++ b/packages/cli/src/editor/components/AssetSelector.tsx
@@ -1,4 +1,5 @@
import React, {useEffect} from 'react';
+import type {StaticFile} from 'remotion';
import {getStaticFiles} from 'remotion';
import {subscribeToEvent} from '../../event-source';
import {BACKGROUND, LIGHT_TEXT} from '../helpers/colors';
@@ -34,14 +35,29 @@ const list: React.CSSProperties = {
overflowY: 'auto',
};
+type State = {
+ staticFiles: StaticFile[];
+ publicFolderExists: boolean;
+};
+
export const AssetSelector: React.FC = () => {
const {tabIndex} = useZIndex();
- const [staticFiles, setStaticFiles] = React.useState(() => getStaticFiles());
+ const [{publicFolderExists, staticFiles}, setState] = React.useState<State>(
+ () => {
+ return {
+ staticFiles: getStaticFiles(),
+ publicFolderExists: window.remotion_publicFolderExists,
+ };
+ }
+ );
useEffect(() => {
const onUpdate = () => {
- setStaticFiles(getStaticFiles());
+ setState({
+ staticFiles: getStaticFiles(),
+ publicFolderExists: window.remotion_publicFolderExists,
+ });
};
const unsub = subscribeToEvent('new-public-folder', onUpdate);
@@ -53,13 +69,23 @@ export const AssetSelector: React.FC = () => {
return (
<div style={container}>
{staticFiles.length === 0 ? (
- <div style={emptyState}>
- <div style={label}>
- To add assets, create a folder called{' '}
- <code style={inlineCodeSnippet}>public</code> in the root of your
- project and place a file in it.
+ publicFolderExists ? (
+ <div style={emptyState}>
+ <div style={label}>
+ To add assets, place a file in the{' '}
+ <code style={inlineCodeSnippet}>public</code> folder of your
+ project.
+ </div>
</div>
- </div>
+ ) : (
+ <div style={emptyState}>
+ <div style={label}>
+ To add assets, create a folder called{' '}
+ <code style={inlineCodeSnippet}>public</code> in the root of your
+ project and place a file in it.
+ </div>
+ </div>
+ )
) : (
<div className="__remotion-vertical-scrollbar" style={list}>
{staticFiles.map((file) => {
diff --git a/packages/cli/src/event-source-events.ts b/packages/cli/src/event-source-events.ts
index 2f66c63e43..6d31793740 100644
--- a/packages/cli/src/event-source-events.ts
+++ b/packages/cli/src/event-source-events.ts
@@ -37,4 +37,5 @@ export type EventSourceEvent =
| {
type: 'new-public-folder';
files: StaticFile[];
+ folderExists: boolean;
};
diff --git a/packages/cli/src/event-source.ts b/packages/cli/src/event-source.ts
index 6acd1ebbd7..bf1c0628f5 100644
--- a/packages/cli/src/event-source.ts
+++ b/packages/cli/src/event-source.ts
@@ -52,6 +52,7 @@ export const openEventSource = () => {
if (newEvent.type === 'new-public-folder') {
window.remotion_staticFiles = newEvent.files;
+ window.remotion_publicFolderExists = newEvent.folderExists;
}
listeners.forEach((l) => {
diff --git a/packages/cli/src/preview-server/public-folder.ts b/packages/cli/src/preview-server/public-folder.ts
index 1e140b9a93..57bc22f839 100644
--- a/packages/cli/src/preview-server/public-folder.ts
+++ b/packages/cli/src/preview-server/public-folder.ts
@@ -58,6 +58,7 @@ const watchPublicFolder = ({
onUpdate,
staticHash,
});
+ onUpdate();
watcher.close();
}
};
@@ -66,6 +67,8 @@ const watchPublicFolder = ({
return;
}
+ // Known bug: If whole public folder is deleted, this will not be called on macOS.
+ // This is not severe, so a wontfix for now.
watch(publicDir, {recursive: envSupportsFsRecursive()}, () => {
fetchFolder({publicDir, staticHash});
onUpdate();
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index f881b17790..6ea82833b5 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -1,6 +1,6 @@
import {BundlerInternals} from '@remotion/bundler';
import {RenderInternals} from '@remotion/renderer';
-import {createReadStream, statSync} from 'node:fs';
+import {createReadStream, existsSync, statSync} from 'node:fs';
import type {IncomingMessage, ServerResponse} from 'node:http';
import path from 'node:path';
import {URLSearchParams} from 'node:url';
@@ -127,6 +127,7 @@ const handleFallback = async ({
ignoreCertificateErrors,
openGlRenderer,
},
+ publicFolderExists: existsSync(publicDir),
})
);
};
diff --git a/packages/cli/src/studio.ts b/packages/cli/src/studio.ts
index ad3bc81c6e..76866d02a5 100644
--- a/packages/cli/src/studio.ts
+++ b/packages/cli/src/studio.ts
@@ -1,4 +1,5 @@
import crypto from 'node:crypto';
+import {existsSync} from 'node:fs';
import path from 'node:path';
import {openBrowser} from './better-opn';
import {chalk} from './chalk';
@@ -115,12 +116,15 @@ export const studioCommand = async (remotionRoot: string, args: string[]) => {
remotionRoot,
onUpdate: () => {
waitForLiveEventsListener().then((listener) => {
+ const files = getFiles();
listener.sendEventToClient({
type: 'new-public-folder',
- files: getFiles(),
+ files,
+ folderExists: files.length > 0 ? true : existsSync(publicDir),
});
});
},
+
staticHash,
});
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index bd56123ee9..c0629620fe 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -23,6 +23,7 @@ declare global {
remotion_setBundleMode: (bundleMode: BundleState) => void;
remotion_staticBase: string;
remotion_staticFiles: StaticFile[];
+ remotion_publicFolderExists: boolean;
remotion_editorName: string | null;
remotion_numberOfAudioTags: number;
remotion_projectName: string;