remotion
Version:
Make videos programmatically
54 lines (53 loc) • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.watchStaticFile = exports.WATCH_REMOTION_STATIC_FILES = void 0;
const get_remotion_environment_1 = require("./get-remotion-environment");
const v5_flag_1 = require("./v5-flag");
exports.WATCH_REMOTION_STATIC_FILES = 'remotion_staticFilesChanged';
/*
* @description Watches for changes in a specific static file and invokes a callback function when the file changes, enabling dynamic updates in your Remotion projects.
* @see [Documentation](https://www.remotion.dev/docs/watchstaticfile)
*/
const watchStaticFile = (fileName, callback) => {
if (v5_flag_1.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 Studio
if (!(0, get_remotion_environment_1.getRemotionEnvironment)().isStudio) {
// eslint-disable-next-line no-console
console.warn('The watchStaticFile() 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) => file.name === withoutLeadingSlash);
// Check if the specified static file has updated or deleted
const checkFile = (event) => {
const staticFiles = event.detail.files;
// Check for user specified file
const newFileData = staticFiles.find((file) => 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(exports.WATCH_REMOTION_STATIC_FILES, checkFile);
const cancel = () => {
return window.removeEventListener(exports.WATCH_REMOTION_STATIC_FILES, checkFile);
};
return { cancel };
};
exports.watchStaticFile = watchStaticFile;