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
@@ -1,22 +1,40 @@
import type { Plugin } from "vite";
import { pathWithRoot, type ResolvedFreshViteConfig } from "../utils.ts";
export function clientEntryPlugin(): Plugin {
export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
const modName = "fresh:client-entry";
const modNameUser = "fresh:client-entry-user";
let clientEntry = "";
return {
name: "fresh:client-entry",
applyToEnvironment(env) {
return env.name === "client";
},
configResolved(config) {
clientEntry = pathWithRoot(options.clientEntry, config.root);
},
resolveId(id) {
if (id === modName) {
return `\0${modName}`;
} else if (id === modNameUser) {
return clientEntry;
}
},
load(id) {
async load(id) {
if (id !== `\0${modName}`) return;
let exists = false;
try {
const stat = await Deno.stat(clientEntry);
exists = stat.isFile;
} catch {
exists = false;
}
return `export * from "fresh/runtime-client";
${exists ? `import "fresh:client-entry-user";` : ""}
if (import.meta.hot) {
import.meta.hot.accept(() => {