🎥 Make videos programmatically with React
`@remotion/studio`: Move `watchStaticFile()` and `getStaticFiles()` into a new package
Jonny Burger8db33345dcac
diff --git a/packages/core/src/get-static-files.ts b/packages/core/src/get-static-files.tsindex 2a6a296da1..086e40b805 100644--- a/packages/core/src/get-static-files.ts+++ b/packages/core/src/get-static-files.ts@@ -1,3 +1,5 @@+import {ENABLE_V5_BREAKING_CHANGES} from './v5-flag';+let warnedServer = false;let warnedPlayer = false;@@ -30,6 +32,12 @@ const warnPlayerOnce = () => {* @see [Documentation](https://www.remotion.dev/docs/getstaticfiles)*/export const getStaticFiles = (): StaticFile[] => {+ if (ENABLE_V5_BREAKING_CHANGES) {+ throw new Error(+ 'getStaticFiles() has moved into the `@remotion/studio` package. Update your imports.',+ );+ }+if (typeof document === 'undefined') {warnServerOnce();return [];diff --git a/packages/core/src/watch-static-file.ts b/packages/core/src/watch-static-file.tsindex b117f3618f..830c860ee8 100644--- a/packages/core/src/watch-static-file.ts+++ b/packages/core/src/watch-static-file.ts@@ -1,5 +1,6 @@import {getRemotionEnvironment} from './get-remotion-environment';import type {StaticFile} from './get-static-files';+import {ENABLE_V5_BREAKING_CHANGES} from './v5-flag';type WatcherCallback = (newData: StaticFile | null) => void;@@ -20,6 +21,12 @@ export const watchStaticFile = (fileName: string,callback: WatcherCallback,): {cancel: () => void} => {+ if (ENABLE_V5_BREAKING_CHANGES) {+ throw new Error(+ 'watchStaticFile() has moved into the `@remotion/studio` package. Update your imports.',+ );+ }+// Check if function is called in Remotion Studioif (!getRemotionEnvironment().isStudio) {// eslint-disable-next-line no-consolediff --git a/packages/studio/src/api/get-static-files.ts b/packages/studio/src/api/get-static-files.tsnew file mode 100644index 0000000000..2a6a296da1--- /dev/null+++ b/packages/studio/src/api/get-static-files.ts@@ -0,0 +1,61 @@+let warnedServer = false;+let warnedPlayer = false;++const warnServerOnce = () => {+ if (warnedServer) {+ return;+ }++ warnedServer = true;+ // eslint-disable-next-line no-console+ console.warn(+ 'Called getStaticFiles() on the server. The API is only available in the browser. An empty array was returned.',+ );+};++const warnPlayerOnce = () => {+ if (warnedPlayer) {+ return;+ }++ warnedPlayer = true;+ // eslint-disable-next-line no-console+ console.warn(+ 'Called getStaticFiles() while using the Remotion Player. The API is only available while using the Remotion Studio. An empty array was returned.',+ );+};++/**+ * @description The function array containing all files in the public/ folder. You can reference them by using staticFile().+ * @see [Documentation](https://www.remotion.dev/docs/getstaticfiles)+ */+export const getStaticFiles = (): StaticFile[] => {+ if (typeof document === 'undefined') {+ warnServerOnce();+ return [];+ }++ if (window.remotion_isPlayer) {+ warnPlayerOnce();+ return [];+ }++ return window.remotion_staticFiles;+};++export type StaticFile = {+ /**+ * A string that you can pass to the `src` attribute of an `<Audio>`, `<Img>` and `<Video>` element.+ */+ src: string;+ /**+ * The filepath of the file, relative to the public folder.+ * Example: `subfolder/image.png`+ */+ name: string;+ sizeInBytes: number;+ /**+ * UNIX timestamp in milliseconds+ */+ lastModified: number;+};diff --git a/packages/studio/src/api/watch-static-file.ts b/packages/studio/src/api/watch-static-file.tsnew file mode 100644index 0000000000..cdae18ba1a--- /dev/null+++ b/packages/studio/src/api/watch-static-file.ts@@ -0,0 +1,78 @@+import {getRemotionEnvironment} from 'remotion';+import type {StaticFile} from './get-static-files';++type WatcherCallback = (newData: StaticFile | null) => void;++export const WATCH_REMOTION_STATIC_FILES = 'remotion_staticFilesChanged';++export type WatchRemotionStaticFilesPayload = {+ files: StaticFile[];+};++/**+ * @description Watch for changes in a specific static file.+ * @param {string} fileName - The name of the static file to watch for changes.+ * @param {WatcherCallback} callback - A callback function to be called when the file changes.+ * @returns {{cancel: () => void}} A function that can be used to cancel the event listener.+ * @see [Documentation](https://www.remotion.dev/docs/watchstaticfile)+ */+export const watchStaticFile = (+ fileName: string,+ callback: WatcherCallback,+): {cancel: () => void} => {+ // Check if function is called in Remotion Studio+ if (!getRemotionEnvironment().isStudio) {+ // eslint-disable-next-line no-console+ console.warn('The API is only available while using the Remotion Studio.');+ return {cancel: () => undefined};+ }++ const withoutStaticBase = fileName.startsWith(window.remotion_staticBase)+ ? fileName.replace(window.remotion_staticBase, '')+ : fileName;+ const withoutLeadingSlash = withoutStaticBase.startsWith('/')+ ? withoutStaticBase.slice(1)+ : withoutStaticBase;++ let prevFileData = window.remotion_staticFiles.find(+ (file: StaticFile) => file.name === withoutLeadingSlash,+ );++ // Check if the specified static file has updated or deleted+ const checkFile = (event: Event) => {+ const staticFiles: StaticFile[] = (+ (event as CustomEvent).detail as WatchRemotionStaticFilesPayload+ ).files;++ // Check for user specified file+ const newFileData = staticFiles.find(+ (file: StaticFile) => file.name === withoutLeadingSlash,+ );++ if (!newFileData) {+ // File is deleted+ if (prevFileData !== undefined) {+ callback(null);+ }++ prevFileData = undefined;+ return;+ }++ if (+ prevFileData === undefined ||+ prevFileData.lastModified !== newFileData.lastModified+ ) {+ callback(newFileData); // File is added or modified+ prevFileData = newFileData;+ }+ };++ window.addEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);++ const cancel = () => {+ return window.removeEventListener(WATCH_REMOTION_STATIC_FILES, checkFile);+ };++ return {cancel};+};diff --git a/packages/studio/src/index.ts b/packages/studio/src/index.tsindex 4a35cb02ba..576c9b0dfa 100644--- a/packages/studio/src/index.ts+++ b/packages/studio/src/index.ts@@ -5,3 +5,6 @@ export type {GitSource, RenderDefaults} from '@remotion/studio-shared';export const StudioInternals = {Studio,};++export {StaticFile, getStaticFiles} from './api/get-static-files';+export {watchStaticFile} from './api/watch-static-file';