Skip to content

Visualize Your Ideas With Code

fix(vite-plugin, ui): project file path on selection (#1090)

Kingsley uchef15b3753d47b
diff --git a/packages/ui/index.html b/packages/ui/index.html
index f377f0f1..9c7b9bb6 100644
--- a/packages/ui/index.html
+++ b/packages/ui/index.html
@@ -18,11 +18,27 @@
{
name: 'project.html',
fileName: 'project.html',
- url: './src/project.ts',
+ url: 'src/project',
+ filePath: './src/project.ts',
+ },
+ {
+ name: 'exampleA',
+ fileName: 'exampleA',
+ url: 'src/exampleA',
+ filePath: './src/exampleA.ts',
+ },
+ {
+ name: 'exampleB',
+ fileName: 'exampleB',
+ url: 'src/exampleB',
+ filePath: './src/exampleB.ts',
+ },
+ {
+ name: 'exampleC',
+ fileName: 'exampleC',
+ url: 'src/exampleC',
+ filePath: './src/exampleC.ts',
},
- {name: 'exampleA', fileName: 'exampleA', url: './src/exampleA.ts'},
- {name: 'exampleB', fileName: 'exampleB', url: './src/exampleB.ts'},
- {name: 'exampleC', fileName: 'exampleC', url: './src/exampleC.ts'},
]);
</script>
</body>
diff --git a/packages/ui/src/ProjectSelection.tsx b/packages/ui/src/ProjectSelection.tsx
index c3826620..fa2393f4 100644
--- a/packages/ui/src/ProjectSelection.tsx
+++ b/packages/ui/src/ProjectSelection.tsx
@@ -5,6 +5,7 @@ export interface ProjectData {
name: string;
fileName: string;
url: string;
+ filePath: string;
}
export interface ProjectSelectionProps {
@@ -17,9 +18,9 @@ export function ProjectSelection({projects}: ProjectSelectionProps) {
<Header className={styles.header}>Projects</Header>
<div className={styles.list}>
{projects.map(project => (
- <a className={styles.element} href={`./${project.fileName}`}>
+ <a className={styles.element} href={`./${project.url}`}>
<div className={styles.title}>{project.name}</div>
- <div className={styles.subtitle}>{project.url}</div>
+ <div className={styles.subtitle}>{project.filePath}</div>
</a>
))}
</div>
diff --git a/packages/vite-plugin/src/partials/editor.ts b/packages/vite-plugin/src/partials/editor.ts
index dc7cb56a..45199cfa 100644
--- a/packages/vite-plugin/src/partials/editor.ts
+++ b/packages/vite-plugin/src/partials/editor.ts
@@ -20,7 +20,7 @@ export function editorPlugin({editor, projects}: EditorPluginConfig): Plugin {
const lookup = new Map<string, ProjectData>();
for (const project of projects) {
- lookup.set(project.name, project);
+ lookup.set(project.url, project);
}
return {
@@ -34,7 +34,7 @@ export function editorPlugin({editor, projects}: EditorPluginConfig): Plugin {
/* language=typescript */
return `\
import {editor} from '${editor}';
-import project from '${projects[0].url}?project';
+import project from '${projects[0].filePath}?project';
editor(project);
`;
}
@@ -46,7 +46,7 @@ editor(project);
/* language=typescript */
return `\
import {editor} from '${editor}';
-import project from '${lookup.get(name)!.url}?project';
+import project from '${lookup.get(name)!.filePath}?project';
editor(project);
`;
}
@@ -73,7 +73,13 @@ index(${JSON.stringify(projects)});
const name = url.pathname.slice(1);
if (name && lookup.has(name)) {
res.setHeader('Content-Type', 'text/html');
- res.end(createHtml(`/@id/__x00__virtual:editor?project=${name}`));
+ res.end(
+ createHtml(
+ `/@id/__x00__virtual:editor?project=${encodeURIComponent(
+ name,
+ )}`,
+ ),
+ );
return;
}
}
diff --git a/packages/vite-plugin/src/partials/projects.ts b/packages/vite-plugin/src/partials/projects.ts
index dc431a09..aa60c30a 100644
--- a/packages/vite-plugin/src/partials/projects.ts
+++ b/packages/vite-plugin/src/partials/projects.ts
@@ -83,7 +83,10 @@ import {MetaFile} from '@motion-canvas/core';
rollupOptions: {
preserveEntrySignatures: 'strict',
input: Object.fromEntries(
- projects.map(project => [project.name, project.url + '?project']),
+ projects.map(project => [
+ project.name,
+ project.filePath + '?project',
+ ]),
),
},
},
@@ -95,7 +98,7 @@ import {MetaFile} from '@motion-canvas/core';
jsxImportSource: '@motion-canvas/2d/lib',
},
optimizeDeps: {
- entries: projects.map(project => project.url),
+ entries: projects.map(project => project.filePath),
exclude: ['preact', 'preact/*', '@preact/signals'],
},
};
diff --git a/packages/vite-plugin/src/plugins.ts b/packages/vite-plugin/src/plugins.ts
index e21dd009..ee9be932 100644
--- a/packages/vite-plugin/src/plugins.ts
+++ b/packages/vite-plugin/src/plugins.ts
@@ -13,7 +13,11 @@ export interface ProjectData {
*/
fileName: string;
/**
- * The path to the project relative to the Vite configuration file.
+ * The path to the project file relative to the Vite configuration file.
+ */
+ filePath: string;
+ /**
+ * The path to access the project relative to the Host Name.
*/
url: string;
}
diff --git a/packages/vite-plugin/src/utils.ts b/packages/vite-plugin/src/utils.ts
index 99b67bb5..bb74aeb4 100644
--- a/packages/vite-plugin/src/utils.ts
+++ b/packages/vite-plugin/src/utils.ts
@@ -16,11 +16,17 @@ export async function createMeta(metaPath: string) {
export function getProjects(project: string | string[]): ProjectData[] {
const list: ProjectData[] = [];
const projectList = expandFilePaths(project);
- for (const url of projectList) {
- const {name, dir} = path.posix.parse(url);
+ for (const filePath of projectList) {
+ const {name, dir} = path.posix.parse(filePath);
const metaFile = `${name}.meta`;
const metaData = getMeta(path.join(dir, metaFile));
- const data = {name: metaData?.name ?? name, fileName: name, url};
+ const url = path.posix.join(dir, name);
+ const data = {
+ name: metaData?.name ?? url,
+ fileName: name,
+ url,
+ filePath,
+ };
list.push(data);
}