fix(vite): asset handling (#3225)

This PR reworks asset handling in our vite plugin. Importing assets in
js is now supported.
This commit is contained in:
Marvin Hagemeister
2025-08-19 16:45:33 +02:00
committed by GitHub
parent 54110c6372
commit afdf5eaf6c
47 changed files with 760 additions and 85 deletions
+36 -12
View File
@@ -81,6 +81,10 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
this.clientEntry = getClientEntry(config.buildId);
}
getEntryAssets(): string[] {
return [];
}
getFsRoutes(): Command<State>[] {
return this.#commands;
}
@@ -247,6 +251,10 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
this.root = config.root;
}
getEntryAssets(): string[] {
return [];
}
getFsRoutes(): Command<State>[] {
return [];
}
@@ -357,6 +365,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
},
fsRoutesFiles: this.#fsRoutes.files,
outDir: root,
entryAssets: [],
}),
);
@@ -422,6 +431,7 @@ export async function generateSnapshotServer(
// deno-lint-ignore no-explicit-any
fsRoutesFiles: FsRouteFileNoMod<any>[];
staticFiles: PendingStaticFile[];
entryAssets: string[];
writeSpecifier: (filePath: string) => string;
},
): Promise<string> {
@@ -475,21 +485,13 @@ export async function generateSnapshotServer(
const staticFiles = await Promise.all(
options.staticFiles.map(async (item) => {
const file = await Deno.open(item.filePath);
const hash = item.hash ? item.hash : await hashContent(file.readable);
const url = new URL(item.pathname, "http://localhost");
return {
name: url.pathname,
hash,
filePath: path.isAbsolute(item.filePath)
? path.relative(outDir, item.filePath)
: item.filePath,
contentType: getContentType(item.filePath),
};
return await prepareStaticFile(item, outDir);
}),
);
const entryAssets = options.entryAssets.map((url) => JSON.stringify(url))
.join(",\n");
return `${EDIT_WARNING}
import { IslandPreparer } from "fresh/internal";
${islandImports}
@@ -510,12 +512,34 @@ ${
}
]);
export const entryAssets = [${entryAssets}];
export const fsRoutes = [
${serializedFsRoutes}
];
`.replaceAll(/\n[\n]+/g, "\n\n");
}
export async function prepareStaticFile(
item: PendingStaticFile,
outDir: string,
): Promise<
{ name: string; hash: string; filePath: string; contentType: string }
> {
const file = await Deno.open(item.filePath);
const hash = item.hash ? item.hash : await hashContent(file.readable);
const url = new URL(item.pathname, "http://localhost");
return {
name: url.pathname,
hash,
filePath: path.isAbsolute(item.filePath)
? path.relative(outDir, item.filePath)
: item.filePath,
contentType: getContentType(item.filePath),
};
}
export function generateServerEntry(
options: {
root: string;