Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions packages/core/src/app/makeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,10 @@ export function makeProject(project: UserProject): Project {
settings: convertedSettings,
plugins: [],
logger: new Logger(),
versions: createVersionObject('0.10.4'),
};
}

export async function addEditorToProject(project: Project) {
const url = '/@id/@revideo/2d/editor';
const imported = await import(
/* webpackIgnore: true */ /* @vite-ignore */ url
);
const plugin = imported.default();

return {
...project,
plugins: [...project.plugins, plugin],
// Placeholder only. The real, per-package versions are resolved from the
// installed `package.json` files and injected by the vite-plugin's editor
// entry (the sole consumer, the editor footer). They are never used during
// a headless render, so we don't try to resolve them here in core.
versions: createVersionObject('0.0.0'),
};
}
2 changes: 1 addition & 1 deletion packages/create/examples
3 changes: 2 additions & 1 deletion packages/template/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"noEmit": false,
"outDir": "dist",
"rootDir": "./src",
"module": "CommonJS",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"skipLibCheck": true
},
"include": ["src"]
Expand Down
54 changes: 40 additions & 14 deletions packages/vite-plugin/src/partials/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import type {Plugin} from 'vite';
import type {Projects} from '../utils';
import {getVersions} from '../versions';

interface EditorPluginConfig {
editor: string;
Expand All @@ -19,6 +20,43 @@ export function editorPlugin({editor, projects}: EditorPluginConfig): Plugin {

const resolvedEditorId = '\0virtual:editor';

/**
* Generate the editor entry module for a single project.
*
* Editor plugins are declared per-scene as module specifiers (e.g.
* `'@revideo/2d/editor'`). We load them here, in the dev-server-only entry,
* and inject them into the project before handing it to the editor. This
* keeps the dynamic import — and the Vite-internal `/@id/` prefix used to
* resolve it — out of `@revideo/core`, so it never leaks into a headless
* render bundle.
*
* The real package versions (resolved from the installed `package.json`
* files) are injected here too — they're only ever shown in the editor
* footer, so this is the one place that has both the project and access to
* the file system.
*/
const editorEntry = (projectUrl: string) => {
const versions = JSON.stringify(getVersions());
/* language=typescript */
return `\
import {editor} from '${editor}';
import project from '/@fs/${path.resolve(projectUrl)}';
const specifiers = [
...new Set((project.scenes ?? []).flatMap(scene => scene.plugins ?? [])),
];
const plugins = await Promise.all(
specifiers.map(specifier =>
import(/* @vite-ignore */ '/@id/' + specifier).then(mod => mod.default()),
),
);
editor({
...project,
versions: ${versions},
plugins: [...project.plugins, ...plugins],
});
`;
};

return {
name: 'revideo:editor',

Expand All @@ -27,26 +65,14 @@ export function editorPlugin({editor, projects}: EditorPluginConfig): Plugin {

if (id.startsWith(resolvedEditorId)) {
if (projects.list.length === 1) {
/* language=typescript */
return `\
import {editor} from '${editor}';
import project from '/@fs/${path.resolve(projects.list[0].url)}';
import {addEditorToProject} from '@revideo/core';
editor(await addEditorToProject(project));
`;
return editorEntry(projects.list[0].url);
}

if (query) {
const params = new URLSearchParams(query);
const name = params.get('project');
if (name && projects.lookup.has(name)) {
/* language=typescript */
return `\
import {editor} from '${editor}';
import project from '/@fs/${path.resolve(projects.lookup.get(name)!.url)}';
import {addEditorToProject} from '@revideo/core';
editor(await addEditorToProject(project));
`;
return editorEntry(projects.lookup.get(name)!.url);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin/src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function getVersions() {
core: loadVersion('@revideo/core'),
two: loadVersion('@revideo/2d'),
ui: loadVersion('@revideo/ui'),
vitePlugin: loadVersion('..'),
vitePlugin: loadVersion('@revideo/vite-plugin'),
};
}

Expand Down
Loading