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
@@ -77,6 +77,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
let islandMods: IslandModChunk[] = [];
let clientEntry = "/@id/fresh:client-entry";
let buildId = "";
const entryAssets: string[] = [];
if (isDev && server !== undefined) {
for (const id of islands.keys()) {
@@ -104,8 +105,10 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
),
) as Manifest;
const clientEntryName = "client-entry";
for (const chunk of Object.values(manifest)) {
if (chunk.name === "client-entry") {
if (chunk.name === clientEntryName) {
clientEntry = pathToSpec(clientOutDir, chunk.file);
}
@@ -115,6 +118,24 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
hash: null,
});
if (chunk.css !== undefined) {
for (let i = 0; i < chunk.css.length; i++) {
const id = chunk.css[i];
const pathname = `/${id}`;
staticFiles.push({
filePath: path.join(clientOutDir, id),
hash: null,
pathname,
});
if (chunk.name === clientEntryName) {
entryAssets.push(pathname);
}
}
}
const namer = new UniqueNamer();
if (chunk.name === "client-snapshot") {
for (const id of chunk.dynamicImports ?? []) {
@@ -165,10 +186,11 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
}
const code = await generateSnapshotServer({
outDir: root,
outDir: path.join(clientOutDir, ".."),
staticFiles,
buildId,
clientEntry,
entryAssets,
fsRoutesFiles: result.routes,
islands: islandMods,
writeSpecifier: (file) => {