Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/plugin-vite/src/plugins/client_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,25 @@ if (import.meta.hot) {
];
}

function isIslandPath(
/**
* A `(_islands)` path segment marks a local island folder, the same
* convention the initial route crawl uses.
*/
const LOCAL_ISLAND_REG = /[/\\]\(_islands\)[/\\]/;

export 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 (relRoutes.startsWith("..")) return false;

if (!relIsland.startsWith("..") && relRoutes.includes("(_islands)")) {
return true;
}
return false;
// Leading separator so that a `(_islands)` folder sitting directly in the
// route dir matches too.
return LOCAL_ISLAND_REG.test(`${path.SEPARATOR}${relRoutes}`);
}

function invalidateSnapshots(server: ViteDevServer) {
Expand Down
58 changes: 58 additions & 0 deletions packages/plugin-vite/src/plugins/client_snapshot_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from "@std/expect/expect";
import * as path from "@std/path";
import { isIslandPath } from "./client_snapshot.ts";
import type { ResolvedFreshViteConfig } from "../utils.ts";

const ROOT = path.join(path.SEPARATOR, "project");

const options = {
islandsDir: path.join(ROOT, "islands"),
routeDir: path.join(ROOT, "routes"),
} as ResolvedFreshViteConfig;

Deno.test("client snapshot - isIslandPath detects the islands dir", () => {
expect(isIslandPath(options, path.join(ROOT, "islands", "Counter.tsx")))
.toBe(true);
expect(
isIslandPath(options, path.join(ROOT, "islands", "nested", "Counter.tsx")),
).toBe(true);
});

Deno.test("client snapshot - isIslandPath detects local island folders", () => {
expect(
isIslandPath(options, path.join(ROOT, "routes", "(_islands)", "Foo.tsx")),
).toBe(true);
// Inside a route group.
expect(
isIslandPath(
options,
path.join(ROOT, "routes", "(marketing)", "(_islands)", "Foo.tsx"),
),
).toBe(true);
// And inside a plain route folder.
expect(
isIslandPath(
options,
path.join(ROOT, "routes", "shop", "(_islands)", "Cart.tsx"),
),
).toBe(true);
});

Deno.test("client snapshot - isIslandPath ignores everything else", () => {
expect(isIslandPath(options, path.join(ROOT, "routes", "index.tsx")))
.toBe(false);
// Another route group is not an island folder.
expect(
isIslandPath(
options,
path.join(ROOT, "routes", "(_components)", "Head.tsx"),
),
).toBe(false);
// `(_islands)` has to be a path segment, not a substring.
expect(
isIslandPath(options, path.join(ROOT, "routes", "(_islands)x", "Foo.tsx")),
).toBe(false);
// Outside both dirs, even when the name matches.
expect(isIslandPath(options, path.join(ROOT, "(_islands)", "Foo.tsx")))
.toBe(false);
});