fix(vite): nested islands not working (#3440)
This PR makes nested islands work. Problem was that only entry files have the `src` property in the vite manifest. For that we need to ensure that every island is an entry point. Fixes https://github.com/denoland/fresh/issues/3439
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
|
export function IslandNestedInner() {
|
||||||
|
const [ready, set] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
set(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div class={ready ? "inner-ready" : ""}>
|
||||||
|
<p>Inner</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
import { IslandNestedInner } from "./IslandNestedInner.tsx";
|
||||||
|
|
||||||
|
export function IslandNestedOuter() {
|
||||||
|
const [ready, set] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
set(true);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div class={ready ? "outer-ready" : ""}>
|
||||||
|
<p>Outer</p>
|
||||||
|
<IslandNestedInner />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { RemoteIsland } from "@marvinh-test/fresh-island";
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { IslandNestedOuter } from "../../islands/IslandNestedOuter.tsx";
|
||||||
|
|
||||||
|
export default function Page() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<IslandNestedOuter />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,7 +6,9 @@ import tailwind from "@tailwindcss/vite";
|
|||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [
|
plugins: [
|
||||||
inspect(),
|
inspect(),
|
||||||
fresh(),
|
fresh({
|
||||||
|
islandSpecifiers: ["@marvinh-test/fresh-island"],
|
||||||
|
}),
|
||||||
tailwind(),
|
tailwind(),
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
|||||||
preserveEntrySignatures: "strict",
|
preserveEntrySignatures: "strict",
|
||||||
input: {
|
input: {
|
||||||
"client-entry": "fresh:client-entry",
|
"client-entry": "fresh:client-entry",
|
||||||
"client-snapshot": "fresh:client-snapshot",
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -186,7 +185,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
|
|||||||
patches(),
|
patches(),
|
||||||
...serverSnapshot(fConfig),
|
...serverSnapshot(fConfig),
|
||||||
clientEntryPlugin(fConfig),
|
clientEntryPlugin(fConfig),
|
||||||
clientSnapshot(fConfig),
|
...clientSnapshot(fConfig),
|
||||||
buildIdPlugin(),
|
buildIdPlugin(),
|
||||||
...devServer(),
|
...devServer(),
|
||||||
prefresh({
|
prefresh({
|
||||||
|
|||||||
@@ -1,55 +1,104 @@
|
|||||||
import type { Plugin, ViteDevServer } from "vite";
|
import type { Plugin, ViteDevServer } from "vite";
|
||||||
import type { ResolvedFreshViteConfig } from "../utils.ts";
|
import { pathWithRoot, type ResolvedFreshViteConfig } from "../utils.ts";
|
||||||
import { crawlFsItem } from "fresh/internal-dev";
|
import { crawlFsItem, specToName } from "fresh/internal-dev";
|
||||||
|
import * as path from "@std/path";
|
||||||
|
|
||||||
export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin {
|
export function clientSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
||||||
const modName = "fresh:client-snapshot";
|
const modName = "fresh:client-snapshot";
|
||||||
|
|
||||||
const islands = new Set<string>();
|
const islands = new Set<string>();
|
||||||
let server: ViteDevServer | undefined;
|
let server: ViteDevServer | undefined;
|
||||||
let isDev = false;
|
let isDev = false;
|
||||||
|
|
||||||
return {
|
const entryToIsland = new Map<string, string>();
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
name: "fresh:client-snapshot",
|
name: "fresh:client-snapshot",
|
||||||
applyToEnvironment(env) {
|
applyToEnvironment(env) {
|
||||||
return env.name === "client";
|
return env.name === "client";
|
||||||
},
|
},
|
||||||
config(_, env) {
|
|
||||||
|
async config(cfg, env) {
|
||||||
isDev = env.command === "serve";
|
isDev = env.command === "serve";
|
||||||
|
|
||||||
|
const cwd = Deno.cwd();
|
||||||
|
|
||||||
|
const result = await crawlFsItem({
|
||||||
|
islandDir: pathWithRoot(options.islandsDir, cfg.root ?? cwd),
|
||||||
|
routeDir: pathWithRoot(options.routeDir, cfg.root ?? cwd),
|
||||||
|
ignore: options.ignore,
|
||||||
|
});
|
||||||
|
|
||||||
|
const input: Record<string, string> = {};
|
||||||
|
|
||||||
|
if (isDev) {
|
||||||
|
input["client-snapshot"] = "fresh:client-snapshot";
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < result.islands.length; i++) {
|
||||||
|
const filePath = result.islands[i];
|
||||||
|
islands.add(filePath);
|
||||||
|
|
||||||
|
if (!isDev) {
|
||||||
|
const specName = specToName(filePath);
|
||||||
|
const name = options.namer.getUniqueName(specName);
|
||||||
|
|
||||||
|
entryToIsland.set(name, filePath);
|
||||||
|
input[`fresh-island::${name}`] = `fresh-client-island::${name}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
environments: {
|
environments: {
|
||||||
client: {
|
client: {
|
||||||
build: {
|
build: {
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
input: {
|
input,
|
||||||
"client-snapshot": "fresh:client-snapshot",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
configResolved() {
|
configResolved(cfg) {
|
||||||
|
for (const [name, spec] of entryToIsland.entries()) {
|
||||||
|
const full = pathWithRoot(spec, cfg.root);
|
||||||
|
entryToIsland.set(name, full);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
options(opts) {
|
||||||
options.islandSpecifiers.forEach((_name, spec) => {
|
options.islandSpecifiers.forEach((_name, spec) => {
|
||||||
islands.add(spec);
|
islands.add(spec);
|
||||||
});
|
|
||||||
},
|
|
||||||
async buildStart() {
|
|
||||||
const result = await crawlFsItem({
|
|
||||||
islandDir: options.islandsDir,
|
|
||||||
routeDir: options.routeDir,
|
|
||||||
ignore: options.ignore,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (let i = 0; i < result.islands.length; i++) {
|
if (!isDev) {
|
||||||
const filePath = result.islands[i];
|
const specName = specToName(spec);
|
||||||
islands.add(filePath);
|
const name = options.namer.getUniqueName(specName);
|
||||||
|
entryToIsland.set(name, spec);
|
||||||
|
|
||||||
|
// deno-lint-ignore no-explicit-any
|
||||||
|
(opts.input as any)[`fresh-island::${name}`] =
|
||||||
|
`fresh-client-island::${name}`;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
configureServer(devServer) {
|
configureServer(devServer) {
|
||||||
server = devServer;
|
server = devServer;
|
||||||
|
|
||||||
|
server.watcher.on("add", (filePath) => {
|
||||||
|
if (!isIslandPath(options, filePath)) return;
|
||||||
|
|
||||||
|
islands.add(filePath);
|
||||||
|
|
||||||
|
invalidateSnapshots(server!);
|
||||||
|
});
|
||||||
|
server.watcher.on("unlink", (filePath) => {
|
||||||
|
if (!isIslandPath(options, filePath)) return;
|
||||||
|
|
||||||
|
islands.delete(filePath);
|
||||||
|
|
||||||
|
invalidateSnapshots(server!);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
resolveId: {
|
resolveId: {
|
||||||
filter: {
|
filter: {
|
||||||
@@ -90,5 +139,50 @@ if (import.meta.hot) {
|
|||||||
`;
|
`;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
},
|
||||||
|
{
|
||||||
|
name: "fresh:client-island",
|
||||||
|
resolveId: {
|
||||||
|
filter: {
|
||||||
|
id: /^fresh-client-island::/,
|
||||||
|
},
|
||||||
|
handler(id) {
|
||||||
|
const name = id.slice("fresh-client-island::".length);
|
||||||
|
const full = entryToIsland.get(name);
|
||||||
|
return full;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function isIslandPath(
|
||||||
|
options: ResolvedFreshViteConfig,
|
||||||
|
filePath: string,
|
||||||
|
): boolean {
|
||||||
|
const relIsland = path.relative(options.islandsDir, filePath);
|
||||||
|
if (!relIsland.startsWith("..")) return true;
|
||||||
|
|
||||||
|
const relRoutes = path.relative(options.routeDir, filePath);
|
||||||
|
|
||||||
|
if (!relIsland.startsWith("..") && relRoutes.includes("(_islands)")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function invalidateSnapshots(server: ViteDevServer) {
|
||||||
|
const client = server.environments.client.moduleGraph.getModuleById(
|
||||||
|
"\0fresh:client-snapshot",
|
||||||
|
);
|
||||||
|
if (client !== undefined) {
|
||||||
|
server.environments.client.moduleGraph.invalidateModule(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ssr = server.environments.ssr.moduleGraph.getModuleById(
|
||||||
|
"\0fresh:server-snapshot",
|
||||||
|
);
|
||||||
|
if (ssr !== undefined) {
|
||||||
|
server.environments.ssr.moduleGraph.invalidateModule(ssr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -240,16 +240,13 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const namer = new UniqueNamer();
|
if (chunk.name?.startsWith("fresh-island__")) {
|
||||||
if (chunk.name === "client-snapshot") {
|
const name = chunk.name.slice("fresh-island__".length);
|
||||||
for (const id of chunk.dynamicImports ?? []) {
|
let serverPath = path.join(root, chunk.src ?? chunk.file);
|
||||||
const mod = manifest[id];
|
const idx = chunk.src?.indexOf("deno::") ?? -1;
|
||||||
|
|
||||||
let serverPath = path.join(root, mod.src ?? id);
|
if (idx > -1 && chunk.src) {
|
||||||
const idx = mod.src?.indexOf("deno::") ?? -1;
|
const src = chunk.src
|
||||||
|
|
||||||
if (idx > -1 && mod.src) {
|
|
||||||
const src = mod.src
|
|
||||||
.slice(idx)
|
.slice(idx)
|
||||||
.replace(
|
.replace(
|
||||||
/(https?):\/([^/])/,
|
/(https?):\/([^/])/,
|
||||||
@@ -260,15 +257,13 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
serverPath = resolvedIslandSpecs.get(src)!;
|
serverPath = resolvedIslandSpecs.get(src)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
let spec = pathToSpec(clientOutDir, mod.file);
|
let spec = pathToSpec(clientOutDir, chunk.file);
|
||||||
|
|
||||||
if (spec.startsWith("./")) {
|
if (spec.startsWith("./")) {
|
||||||
spec = spec.slice(1);
|
spec = spec.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunkCss = mod.css?.map((id) => `/${id}`) ?? [];
|
const chunkCss = chunk.css?.map((id) => `/${id}`) ?? [];
|
||||||
|
|
||||||
const name = namer.getUniqueName(specToName(id));
|
|
||||||
islandMods.push({
|
islandMods.push({
|
||||||
name,
|
name,
|
||||||
browser: spec,
|
browser: spec,
|
||||||
@@ -277,7 +272,6 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (await fsAdapter.isDirectory(publicDir)) {
|
if (await fsAdapter.isDirectory(publicDir)) {
|
||||||
const entries = await fsAdapter.walk(
|
const entries = await fsAdapter.walk(
|
||||||
|
|||||||
@@ -83,6 +83,27 @@ Deno.test({
|
|||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "vite build - nested islands",
|
||||||
|
fn: async () => {
|
||||||
|
await launchProd(
|
||||||
|
{ cwd: viteResult.tmp },
|
||||||
|
async (address) => {
|
||||||
|
await withBrowser(async (page) => {
|
||||||
|
await page.goto(`${address}/tests/island_nested`, {
|
||||||
|
waitUntil: "networkidle2",
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.locator(".outer-ready").wait();
|
||||||
|
await page.locator(".inner-ready").wait();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite build - without static/ dir",
|
name: "vite build - without static/ dir",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
@@ -328,6 +328,22 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "vite dev - nested islands",
|
||||||
|
fn: async () => {
|
||||||
|
await withBrowser(async (page) => {
|
||||||
|
await page.goto(`${demoServer.address()}/tests/island_nested`, {
|
||||||
|
waitUntil: "networkidle2",
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.locator(".outer-ready").wait();
|
||||||
|
await page.locator(".inner-ready").wait();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
sanitizeResources: false,
|
||||||
|
sanitizeOps: false,
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "vite dev - remote island",
|
name: "vite dev - remote island",
|
||||||
fn: async () => {
|
fn: async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user