feat(vite): check valid imports per environment (#3299)

Fixes https://github.com/denoland/fresh/issues/3296
This commit is contained in:
Marvin Hagemeister
2025-09-01 17:30:30 +02:00
committed by GitHub
parent 5d052c0fdb
commit ff9db336f7
11 changed files with 291 additions and 35 deletions
+17
View File
@@ -15,6 +15,8 @@ import { serverSnapshot } from "./plugins/server_snapshot.ts";
import { patches } from "./plugins/patches.ts";
import process from "node:process";
import { specToName, UniqueNamer } from "@fresh/core/internal-dev";
import { checkImports } from "./plugins/verify_imports.ts";
import { isBuiltin } from "node:module";
export function fresh(config?: FreshViteConfig): Plugin[] {
const fConfig: ResolvedFreshViteConfig = {
@@ -25,8 +27,22 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
ignore: config?.ignore ?? [],
islandSpecifiers: new Map(),
namer: new UniqueNamer(),
checkImports: config?.checkImports ?? [],
};
fConfig.checkImports.push((id, env) => {
if (env === "client") {
if (isBuiltin(id)) {
return {
type: "error",
message: "Node built-in modules cannot be imported in the browser.",
description:
"This is an error in your application code or in one of its dependencies.",
};
}
}
});
const plugins: Plugin[] = [
{
name: "fresh",
@@ -133,6 +149,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
"topLevelAwait",
],
}),
checkImports({ checks: fConfig.checkImports }),
];
if (typeof process.versions.deno === "string") {