feat: Add experimental vite plugin (#3184)

This PR adds a vite plugin for Fresh. This enables:

- HMR (only for islands for now)
- self-contained output bundles for both server and client
- smarter chunking

It uses vite's new Environment API under the hood.
This commit is contained in:
Marvin Hagemeister
2025-08-04 18:30:05 +02:00
committed by GitHub
parent 57596dc406
commit a1571b76ed
54 changed files with 2181 additions and 742 deletions
+14 -6
View File
@@ -23,7 +23,7 @@ import {
setActiveUrl,
} from "../shared_internal.tsx";
import type { BuildCache } from "../../build_cache.ts";
import { BUILD_ID } from "../build_id.ts";
import { BUILD_ID } from "fresh/build-id";
import {
DEV_ERROR_OVERLAY_URL,
PARTIAL_SEARCH_PARAM,
@@ -433,7 +433,8 @@ export interface PartialStateJson {
}
function FreshRuntimeScript() {
const { islands, nonce, ctx, islandProps, partialId } = RENDER_STATE!;
const { islands, nonce, ctx, islandProps, partialId, buildCache } =
RENDER_STATE!;
const basePath = ctx.config.basePath;
const islandArr = Array.from(islands);
@@ -470,7 +471,11 @@ function FreshRuntimeScript() {
: island.exportName === island.name
? `{ ${island.exportName} }`
: `{ ${island.exportName} as ${island.name} }`;
return `import ${named} from "${`${basePath}${island.file}`}";`;
const islandSpec = island.file.startsWith(".")
? island.file.slice(1)
: island.file;
return `import ${named} from "${basePath}${islandSpec}";`;
}).join("");
const islandObj = "{" + islandArr.map((island) => island.name)
@@ -482,9 +487,12 @@ function FreshRuntimeScript() {
{ json: true },
);
const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`;
// const runtimeUrl = `${basePath}/_fresh/js/${BUILD_ID}/fresh-runtime.js`;
const runtimeUrl = buildCache.clientEntry.startsWith(".")
? buildCache.clientEntry.slice(1)
: buildCache.clientEntry;
const scriptContent =
`import { boot } from "${runtimeUrl}";${islandImports}boot(${islandObj},${serializedProps});`;
`import { boot } from "${basePath}${runtimeUrl}";${islandImports}boot(${islandObj},${serializedProps});`;
return (
<>
@@ -496,7 +504,7 @@ function FreshRuntimeScript() {
__html: scriptContent,
}}
/>
{ctx.config.mode === "development" ? <ShowErrorOverlay /> : null}
{buildCache.features.errorOverlay ? <ShowErrorOverlay /> : null}
</>
);
}