feat(vite): check valid imports per environment (#3299)
Fixes https://github.com/denoland/fresh/issues/3296
This commit is contained in:
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user