Files
sweet/packages/plugin-vite/src/plugins/client_entry.ts
T
Eli ab14d1044c refactor(vite): replace old environment APIs (#3634)
I was perusing the code whilst investigating #3606 and noticed that,
despite the release post claiming that Fresh had ["fully
embrace[d]"](https://deno.com/blog/fresh-and-vite#:~:text=fully%20embraces)
the Vite Environment API, it was still using `ssrLoadModule`. Once I
started looking, I noticed a bunch of other code that wasn't
future-proof (env-wise, not rolldown-wise, which'll bring it's own swath
of deprecations[^1]), so I updated them as well.

The newly added source map tests from #3624 currently fail again because
the ModuleRunner API at the heart of the Environment API relies on
[`process.setSourceMapsEnabled`](https://docs.deno.com/api/node/process/~/Process.setSourceMapsEnabled),
which is [currently set to
no-op](https://github.com/denoland/deno/pull/22993) in Deno.

[^1]: Separately tried to try out rolldown-vite but it seems that Deno
doesn't support aliasing dependencies whatsoever? (I know there's not
official support, but I was going to try to edit the lockfile to hack
around it; however, it seems that there's not alias support in there at
all?)
2025-12-09 13:00:36 +01:00

59 lines
1.5 KiB
TypeScript

import type { Plugin } from "vite";
import { pathWithRoot, type ResolvedFreshViteConfig } from "../utils.ts";
export function clientEntryPlugin(options: ResolvedFreshViteConfig): Plugin {
const modName = "fresh:client-entry";
const modNameUser = "fresh:client-entry-user";
let clientEntry = "";
let isDev = false;
return {
name: "fresh:client-entry",
sharedDuringBuild: true,
config(_, env) {
isDev = env.command === "serve";
},
applyToEnvironment(env) {
return env.config.consumer === "client";
},
configResolved(config) {
clientEntry = pathWithRoot(options.clientEntry, config.root);
},
resolveId: {
filter: {
id: /(fresh:client-entry|fresh:client-entry-user|fresh:client-quirks)/,
},
handler(id) {
if (id === modName) {
return `\0${modName}`;
} else if (id === "fresh:client-quirks") {
return "@fresh/plugin-vite/client";
} else if (id === modNameUser) {
return clientEntry;
}
},
},
load: {
filter: {
id: /\0fresh:client-entry/,
},
async handler() {
let exists = false;
try {
const stat = await Deno.stat(clientEntry);
exists = stat.isFile;
} catch {
exists = false;
}
return `
${isDev ? 'import "preact/debug"' : ""}
export * from "fresh/runtime-client";
${exists ? `import "fresh:client-entry-user";` : ""}
import "@fresh/plugin-vite/client";`;
},
},
};
}