Skip to content

Visualize Your Ideas With Code

feat: add new hooks for plugins (#679)

Jacob74e18bce71ab
diff --git a/packages/core/src/app/Project.ts b/packages/core/src/app/Project.ts
index 9b494981..d408eab6 100644
--- a/packages/core/src/app/Project.ts
+++ b/packages/core/src/app/Project.ts
@@ -9,10 +9,20 @@ export interface ProjectSettings {
plugins?: Plugin[];
logger?: Logger;
audio?: string;
+ /**
+ * @deprecated Configure the offset in the Video Settings tab of th editor.
+ */
audioOffset?: number;
variables?: Record<string, unknown>;
}
+export interface Versions {
+ core: string;
+ two: string | null;
+ ui: string | null;
+ vitePlugin: string | null;
+}
+
export interface Project {
name: string;
scenes: FullSceneDescription[];
@@ -20,20 +30,10 @@ export interface Project {
logger: Logger;
meta: ProjectMetadata;
audio?: string;
- audioOffset?: number;
variables?: Record<string, unknown>;
- versions: {
- core: string;
- two: string | null;
- ui: string | null;
- vitePlugin: string | null;
- };
+ versions: Versions;
}
export function makeProject(settings: ProjectSettings) {
- return {
- logger: new Logger(),
- plugins: [],
- ...settings,
- };
+ return settings;
}
diff --git a/packages/core/src/app/bootstrap.ts b/packages/core/src/app/bootstrap.ts
new file mode 100644
index 00000000..aa8a89a8
--- /dev/null
+++ b/packages/core/src/app/bootstrap.ts
@@ -0,0 +1,43 @@
+import {Logger} from './Logger';
+import {Project, ProjectSettings, Versions} from './Project';
+import {ProjectMetadata} from './ProjectMetadata';
+import {Plugin} from '../plugin';
+import {MetaFile} from '../meta';
+
+/**
+ * Bootstrap a project.
+ *
+ * @param name - The name of the project.
+ * @param versions - Package versions.
+ * @param plugins - Loaded plugins.
+ * @param settings - Project settings.
+ * @param metaFile - The meta file.
+ *
+ * @internal
+ */
+export function bootstrap(
+ name: string,
+ versions: Versions,
+ plugins: Plugin[],
+ settings: ProjectSettings,
+ metaFile: MetaFile<any>,
+): Project {
+ const reducedSettings = plugins.reduce(
+ (settings, plugin) => ({
+ ...settings,
+ ...(plugin.settings?.(settings) ?? {}),
+ }),
+ {name, ...settings} as ProjectSettings,
+ );
+
+ const project = {...reducedSettings} as Project;
+ project.versions = versions;
+ project.logger = new Logger();
+ project.plugins = plugins;
+ project.meta = new ProjectMetadata(project);
+ metaFile.attach(project.meta);
+
+ plugins.forEach(plugin => plugin.project?.(project));
+
+ return project;
+}
diff --git a/packages/core/src/app/index.ts b/packages/core/src/app/index.ts
index ac25aa04..64a40dbc 100644
--- a/packages/core/src/app/index.ts
+++ b/packages/core/src/app/index.ts
@@ -4,6 +4,7 @@
* @packageDocumentation
*/
+export * from './bootstrap';
export * from './Exporter';
export * from './ImageExporter';
export * from './Logger';
diff --git a/packages/core/src/plugin/DefaultPlugin.ts b/packages/core/src/plugin/DefaultPlugin.ts
index 02e5eeef..6c64ac9b 100644
--- a/packages/core/src/plugin/DefaultPlugin.ts
+++ b/packages/core/src/plugin/DefaultPlugin.ts
@@ -1,12 +1,12 @@
import {makePlugin} from './makePlugin';
-import {type ExporterClass, ImageExporter} from '../app';
+import {ImageExporter} from '../app';
/**
* The default plugin included in every Motion Canvas project.
*/
export default makePlugin({
name: 'mc-default-plugin',
- exporters(): ExporterClass[] {
+ exporters() {
return [ImageExporter];
},
});
diff --git a/packages/core/src/plugin/Plugin.ts b/packages/core/src/plugin/Plugin.ts
index 22307524..8210c016 100644
--- a/packages/core/src/plugin/Plugin.ts
+++ b/packages/core/src/plugin/Plugin.ts
@@ -1,9 +1,36 @@
-import type {ExporterClass, Project} from '../app';
+import type {ExporterClass, Project, Player, ProjectSettings} from '../app';
/**
* Represents a runtime Motion Canvas plugin.
*/
export interface Plugin {
name: string;
+
+ /**
+ * Modify the project settings before the project is initialized.
+ *
+ * @param settings - The project settings.
+ */
+ settings?(settings: ProjectSettings): ProjectSettings | void;
+
+ /**
+ * Receive the project instance right after it is initialized.
+ *
+ * @param project - The project instance.
+ */
+ project?(project: Project): void;
+
+ /**
+ * Receive the player instance right after it is initialized.
+ *
+ * @param player - The player instance.
+ */
+ player?(player: Player): void;
+
+ /**
+ * Provide custom exporters for the project.
+ *
+ * @param project - The project instance.
+ */
exporters?(project: Project): ExporterClass[];
}
diff --git a/packages/core/src/plugin/makePlugin.ts b/packages/core/src/plugin/makePlugin.ts
index a6ffa734..c5fa905b 100644
--- a/packages/core/src/plugin/makePlugin.ts
+++ b/packages/core/src/plugin/makePlugin.ts
@@ -12,6 +12,6 @@ import type {Plugin} from './Plugin';
* });
* ```
*/
-export function makePlugin(plugin: Plugin): Plugin {
- return plugin;
+export function makePlugin(plugin: Plugin | (() => Plugin)): () => Plugin {
+ return typeof plugin === 'function' ? plugin : () => plugin;
}
diff --git a/packages/ui/src/main.tsx b/packages/ui/src/main.tsx
index e22e3601..3c99a8d7 100644
--- a/packages/ui/src/main.tsx
+++ b/packages/ui/src/main.tsx
@@ -43,6 +43,7 @@ export function editor(project: Project) {
getItem(playerKey, {}),
getItem(frameKey, -1),
);
+ project.plugins.forEach(plugin => plugin.player?.(player));
player.onStateChanged.subscribe(state => {
setItem(playerKey, state);
diff --git a/packages/vite-plugin/src/main.ts b/packages/vite-plugin/src/main.ts
index 65572c4b..b2535a90 100644
--- a/packages/vite-plugin/src/main.ts
+++ b/packages/vite-plugin/src/main.ts
@@ -228,31 +228,34 @@ export default ({
await createMeta(path.join(dir, metaFile));
const imports: string[] = [];
- const pluginNames: string[] = ['...config.plugins'];
+ const pluginNames: string[] = [];
let index = 0;
for (const plugin of plugins) {
if (plugin.entryPoint) {
const pluginName = `plugin${index}`;
+ let options = (await plugin.runtimeConfig?.()) ?? '';
+ if (typeof options !== 'string') {
+ options = JSON.stringify(options);
+ }
+
imports.push(`import ${pluginName} from '${plugin.entryPoint}'`);
- pluginNames.push(pluginName);
+ pluginNames.push(`${pluginName}(${options})`);
index++;
}
}
return source(
...imports,
- `import {ProjectMetadata} from '@motion-canvas/core/lib/app';`,
+ `import {bootstrap} from '@motion-canvas/core/lib/app';`,
`import metaFile from './${metaFile}';`,
`import config from './${name}';`,
- `const project = {`,
- ` name: '${name}',`,
- ` versions: ${versions},`,
- ` ...config,`,
- ` plugins: [${pluginNames.join(', ')}],`,
- `};`,
- `project.meta = new ProjectMetadata(project);`,
- `metaFile.attach(project.meta)`,
- `export default project;`,
+ `export default bootstrap(`,
+ ` '${name}',`,
+ ` ${versions},`,
+ ` [${pluginNames.join(', ')}],`,
+ ` config,`,
+ ` metaFile`,
+ `);`,
);
}
}
diff --git a/packages/vite-plugin/src/plugins.ts b/packages/vite-plugin/src/plugins.ts
index e3066775..39862226 100644
--- a/packages/vite-plugin/src/plugins.ts
+++ b/packages/vite-plugin/src/plugins.ts
@@ -60,6 +60,36 @@ export interface PluginOptions {
* @param config - The configuration passed to the plugin.
*/
config?(config: PluginConfig): Promise<void>;
+
+ /**
+ * Get custom configuration that will be passed to the runtime plugin.
+ *
+ * @remarks
+ * The config will be passed as the first argument to the default export of
+ * the runtime plugin. When provided as a string, it will be injected to the
+ * code as is, letting you define non-serializable values such as functions.
+ *
+ * If the returned value is an object, it will be converted to a JavaScript
+ * object using JSON serialization.
+ *
+ * @example
+ * Returning an object:
+ * ```ts
+ * {
+ * runtimeConfig: () => ({
+ * myText: 'Hello!',
+ * myNumber: 42,
+ * })
+ * }
+ * ```
+ * Returning a string:
+ * ```ts
+ * {
+ * runtimeConfig: () => `{myRegex: /\\.wav$/}`
+ * }
+ * ```
+ */
+ runtimeConfig?(): Promise<any>;
}
/**