Fix warning with vite dev

This commit is contained in:
2026-11-11 14:46:51 -03:00
parent 216d54a0ae
commit 8aa05e104d
9 changed files with 132 additions and 42 deletions
Generated
+20
View File
@@ -11,6 +11,8 @@
"jsr:@deno/graph@~0.82.3": "0.82.3",
"jsr:@deno/loader@0.4": "0.4.0",
"jsr:@deno/loader@~0.3.10": "0.3.14",
"jsr:@fresh/build-id@1": "1.0.1",
"jsr:@fresh/core@2.3.3": "2.3.3",
"jsr:@marvinh-test/fresh-island@^0.0.3": "0.0.3",
"jsr:@marvinh-test/import-json@^0.0.1": "0.0.1",
"jsr:@matmen/imagescript@^1.3.0": "1.3.1",
@@ -194,6 +196,24 @@
"@deno/loader@0.4.0": {
"integrity": "6c1b18cfa18592740613ce79e15625c24268d60dbfc54bebfb5153bf512c536b"
},
"@fresh/build-id@1.0.1": {
"integrity": "12a2ec25fd52ae9ec68c26848a5696cd1c9b537f7c983c7e56e4fb1e7e816c20",
"dependencies": [
"jsr:@std/encoding@^1.0.10"
]
},
"@fresh/core@2.3.3": {
"integrity": "6c2aad199976644352c89a1d601f3ef331b1d24cdadb53ed469324a303598383",
"dependencies": [
"jsr:@fresh/build-id",
"jsr:@std/fmt@^1.0.8",
"jsr:@std/fs@^1.0.19",
"jsr:@std/html@^1.0.5",
"jsr:@std/http@^1.0.21",
"jsr:@std/net",
"jsr:@std/path@^1.1.2"
]
},
"@marvinh-test/fresh-island@0.0.3": {
"integrity": "6d06b6009b7dfba9bba28e941e03e6ff652c4ef4f2fbfdf4b78741abd6c6c1c6",
"dependencies": [
+1 -2
View File
@@ -10,7 +10,7 @@ import {
} from "./context.ts";
import { mergePath, type Method, UrlPatternRouter } from "./router.ts";
import type { FreshConfig, ResolvedFreshConfig } from "./config.ts";
import type { BuildCache } from "./build_cache.ts";
import { type BuildCache, MockBuildCache } from "./build_cache.ts";
import { HttpError } from "./error.ts";
import { STATUS_TEXT } from "@std/http/status";
import type { LayoutConfig, MaybeLazy, Route, RouteConfig } from "./types.ts";
@@ -29,7 +29,6 @@ import {
newNotFoundCmd,
newRouteCmd,
} from "./commands.ts";
import { MockBuildCache } from "./test_utils.ts";
// TODO: Completed type clashes in older Deno versions
// deno-lint-ignore no-explicit-any
+26
View File
@@ -102,6 +102,32 @@ export class ProdBuildCache<State> implements BuildCache<State> {
}
}
/** Fallback build cache for when there's no build output (and for tests). */
export class MockBuildCache<State> implements BuildCache<State> {
#files: FsRouteFile<State>[];
root = "";
clientEntry = "";
islandRegistry: ServerIslandRegistry = new Map();
features = { errorOverlay: false };
constructor(files: FsRouteFile<State>[], mode: "development" | "production") {
this.features.errorOverlay = mode === "development";
this.#files = files;
}
getEntryAssets(): string[] {
return [];
}
getFsRoutes(): Command<State>[] {
return fsItemsToCommands(this.#files);
}
readFile(_pathname: string): Promise<StaticFile | null> {
return Promise.resolve(null);
}
}
export class IslandPreparer {
#namer = new UniqueNamer();
+1 -8
View File
@@ -8,7 +8,7 @@ import * as pathWin32 from "@std/path/windows";
import { encodeHex } from "@std/encoding/hex";
import { fsAdapter } from "../fs.ts";
import type { FileTransformer } from "./file_transformer.ts";
import { assertInDir, pathToSpec } from "../utils.ts";
import { assertInDir, pathToSpec, toPosix } from "../utils.ts";
import type { ResolvedBuildConfig } from "./builder.ts";
import { fsItemsToCommands, type FsRouteFile } from "../fs_routes.ts";
import type { Command } from "../commands.ts";
@@ -17,13 +17,6 @@ import { contentType as getStdContentType } from "@std/media-types/content-type"
const WINDOWS_SEPARATOR = pathWin32.SEPARATOR;
/** Normalize a path to use forward slashes so that generated files
* are portable across operating systems (e.g. build on Windows,
* deploy on Linux). */
export function toPosix(p: string): string {
return p.replaceAll(WINDOWS_SEPARATOR, "/");
}
export interface MemoryFile {
hash: string | null;
contentType: string;
+2 -1
View File
@@ -1,6 +1,7 @@
import { type FsAdapter, fsAdapter } from "../fs.ts";
import type { WalkEntry } from "@std/fs/walk";
import { type FsRouteFileNoMod, toPosix } from "./dev_build_cache.ts";
import type { FsRouteFileNoMod } from "./dev_build_cache.ts";
import { toPosix } from "../utils.ts";
import * as path from "@std/path";
import { pathToPattern } from "../router.ts";
import { CommandType } from "../commands.ts";
+1 -2
View File
@@ -7,10 +7,9 @@ export {
type IslandModChunk,
type PendingStaticFile,
prepareStaticFile,
toPosix,
writeCompiledEntry,
} from "./dev/dev_build_cache.ts";
export { specToName } from "./dev/builder.ts";
export { pathToSpec, UniqueNamer } from "./utils.ts";
export { pathToSpec, toPosix, UniqueNamer } from "./utils.ts";
export { updateCheck } from "./dev/update_check.ts";
export { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "./constants.ts";
+4 -29
View File
@@ -1,13 +1,11 @@
import { Context, type ServerIslandRegistry } from "./context.ts";
import { Context } from "./context.ts";
import type { FsAdapter } from "./fs.ts";
import type { BuildCache, StaticFile } from "./build_cache.ts";
import { type BuildCache, MockBuildCache } from "./build_cache.ts";
import type { ResolvedFreshConfig } from "./config.ts";
import type { WalkEntry } from "@std/fs/walk";
import { DEFAULT_CONN_INFO } from "./app.ts";
import type { Command } from "./commands.ts";
import { fsItemsToCommands, type FsRouteFile } from "./fs_routes.ts";
import * as path from "@std/path";
import { toPosix } from "./dev/dev_build_cache.ts";
import { toPosix } from "./utils.ts";
const STUB = {} as unknown as Deno.ServeHandlerInfo;
@@ -162,30 +160,7 @@ export async function withTmpDir(
};
}
export class MockBuildCache<State> implements BuildCache<State> {
#files: FsRouteFile<State>[];
root = "";
clientEntry = "";
islandRegistry: ServerIslandRegistry = new Map();
features = { errorOverlay: false };
constructor(files: FsRouteFile<State>[], mode: "development" | "production") {
this.features.errorOverlay = mode === "development";
this.#files = files;
}
getEntryAssets(): string[] {
return [];
}
getFsRoutes(): Command<State>[] {
return fsItemsToCommands(this.#files);
}
readFile(_pathname: string): Promise<StaticFile | null> {
return Promise.resolve(null);
}
}
export { MockBuildCache };
/**
* Wrapper around `Deno.test` for integration tests that disables sanitizers.
+7
View File
@@ -2,6 +2,13 @@ import * as path from "@std/path";
import type { Lazy, MaybeLazy } from "./types.ts";
import { pathToFileUrl, relativeUrl } from "./file_url.ts";
/** Normalize a path to use forward slashes so that generated files
* are portable across operating systems (e.g. build on Windows,
* deploy on Linux). */
export function toPosix(p: string): string {
return p.replaceAll("\\", "/");
}
export function assertInDir(
filePath: string,
dir: string,
@@ -0,0 +1,70 @@
import { expect } from "@std/expect";
// Guards against runtime code importing the esbuild dev builder (`src/dev/`)
// or test helpers. That happened once through `app.ts` -> `test_utils.ts` ->
// `dev/dev_build_cache.ts`: every app then loaded esbuild at startup, and
// Vite projects printed "dynamic import cannot be analyzed" warnings.
const SRC_URL = new URL("../src/", import.meta.url).href;
/** Entry points that end up in a user's app at runtime (see deno.json). */
const RUNTIME_ENTRIES = [
"mod.ts",
"runtime/shared.ts",
"runtime/client/mod.ts",
"internals.ts",
"compat.ts",
];
/** Under `src/dev/` but lightweight and needed by the server renderer. */
const ALLOWED_DEV_MODULES = ["dev/middlewares/error_overlay/code_frame.ts"];
/** Paths under `src/` of every local module in the graph, plus all others. */
async function moduleGraph(
entry: string,
): Promise<{ local: string[]; other: string[] }> {
const out = await new Deno.Command(Deno.execPath(), {
// Must be a file URL: on Windows a bare `C:\...` path parses as a URL
// with scheme `c:` and the graph comes back empty.
args: ["info", "--json", new URL(entry, SRC_URL).href],
stdout: "piped",
stderr: "piped",
}).output();
if (!out.success) {
throw new Error(new TextDecoder().decode(out.stderr));
}
const info = JSON.parse(new TextDecoder().decode(out.stdout)) as {
modules: { specifier: string }[];
};
// Drive letter casing can differ between the two, so compare lowercased.
const prefix = SRC_URL.toLowerCase();
const local: string[] = [];
const other: string[] = [];
for (const { specifier } of info.modules) {
if (specifier.toLowerCase().startsWith(prefix)) {
local.push(specifier.slice(prefix.length));
} else {
other.push(specifier);
}
}
return { local, other };
}
for (const entry of RUNTIME_ENTRIES) {
Deno.test(`runtime imports - ${entry} doesn't load dev builder or test helpers`, async () => {
const { local, other } = await moduleGraph(entry);
// Sanity check, so a failed lookup can't pass with an empty graph.
expect(local).toContain(entry);
const forbidden = [
...local.filter((rel) =>
rel === "test_utils.ts" ||
(rel.startsWith("dev/") && !ALLOWED_DEV_MODULES.includes(rel))
),
...other.filter((spec) => /esbuild/.test(spec)),
];
expect(forbidden).toEqual([]);
});
}