feat(vite): check valid imports per environment (#3299)
Fixes https://github.com/denoland/fresh/issues/3296
This commit is contained in:
@@ -72,6 +72,7 @@
|
||||
"@std/testing": "jsr:@std/testing@^1.0.12",
|
||||
|
||||
"@tailwindcss/postcss": "npm:@tailwindcss/postcss@^4.1.10",
|
||||
"rollup": "npm:rollup@^4.50.0",
|
||||
"tailwindcss": "npm:tailwindcss@^4.1.10",
|
||||
"postcss": "npm:postcss@8.5.6",
|
||||
|
||||
|
||||
@@ -65,9 +65,9 @@
|
||||
"npm:@opentelemetry/api@^1.9.0": "1.9.0",
|
||||
"npm:@preact/signals@2": "2.3.1_preact@10.27.1",
|
||||
"npm:@preact/signals@^2.2.1": "2.3.1_preact@10.27.1",
|
||||
"npm:@prefresh/vite@^2.4.8": "2.4.10_preact@10.27.1_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:@prefresh/vite@^2.4.8": "2.4.10_preact@10.27.1_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:@tailwindcss/postcss@^4.1.10": "4.1.12",
|
||||
"npm:@tailwindcss/vite@^4.1.12": "4.1.12_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:@tailwindcss/vite@^4.1.12": "4.1.12_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:@types/babel__core@^7.20.5": "7.20.5",
|
||||
"npm:@types/node@*": "24.2.0",
|
||||
"npm:@types/node@^24.1.0": "24.3.0",
|
||||
@@ -92,14 +92,15 @@
|
||||
"npm:preact@^10.26.9": "10.27.1",
|
||||
"npm:preact@^10.27.0": "10.27.1",
|
||||
"npm:prismjs@^1.29.0": "1.30.0",
|
||||
"npm:rollup-plugin-visualizer@^6.0.3": "6.0.3",
|
||||
"npm:rollup-plugin-visualizer@^6.0.3": "6.0.3_rollup@4.50.0",
|
||||
"npm:rollup@^4.50.0": "4.50.0",
|
||||
"npm:tailwindcss@^3.4.17": "3.4.17_postcss@8.5.6",
|
||||
"npm:tailwindcss@^4.1.10": "4.1.12",
|
||||
"npm:ts-morph@26": "26.0.0",
|
||||
"npm:vite-plugin-inspect@^11.3.2": "11.3.3_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:vite@*": "7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"npm:vite@7.1.3": "7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"npm:vite@^7.1.3": "7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
"npm:vite-plugin-inspect@^11.3.2": "11.3.3_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0",
|
||||
"npm:vite@*": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"npm:vite@7.1.4": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"npm:vite@^7.1.3": "7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
},
|
||||
"jsr": {
|
||||
"@astral/astral@0.5.3": {
|
||||
@@ -1155,7 +1156,7 @@
|
||||
"@prefresh/utils@1.2.1": {
|
||||
"integrity": "sha512-vq/sIuN5nYfYzvyayXI4C2QkprfNaHUQ9ZX+3xLD8nL3rWyzpxOm1+K7RtMbhd+66QcaISViK7amjnheQ/4WZw=="
|
||||
},
|
||||
"@prefresh/vite@2.4.10_preact@10.27.1_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"@prefresh/vite@2.4.10_preact@10.27.1_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"integrity": "sha512-lt+ODASOtXRWaPplp7/DlrgAaInnQYNvcpCglQBMx2OeJPyZ4IqPRaxsK77w96mWshjYwkqTsRSHoAM7aAn0ow==",
|
||||
"dependencies": [
|
||||
"@babel/core",
|
||||
@@ -1164,10 +1165,10 @@
|
||||
"@prefresh/utils",
|
||||
"@rollup/pluginutils",
|
||||
"preact",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3"
|
||||
]
|
||||
},
|
||||
"@prefresh/vite@2.4.10_preact@10.27.1_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"@prefresh/vite@2.4.10_preact@10.27.1_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"integrity": "sha512-lt+ODASOtXRWaPplp7/DlrgAaInnQYNvcpCglQBMx2OeJPyZ4IqPRaxsK77w96mWshjYwkqTsRSHoAM7aAn0ow==",
|
||||
"dependencies": [
|
||||
"@babel/core",
|
||||
@@ -1176,7 +1177,7 @@
|
||||
"@prefresh/utils",
|
||||
"@rollup/pluginutils",
|
||||
"preact",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
]
|
||||
},
|
||||
"@rollup/pluginutils@4.2.1": {
|
||||
@@ -1402,22 +1403,22 @@
|
||||
"tailwindcss@4.1.12"
|
||||
]
|
||||
},
|
||||
"@tailwindcss/vite@4.1.12_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"@tailwindcss/vite@4.1.12_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"integrity": "sha512-4pt0AMFDx7gzIrAOIYgYP0KCBuKWqyW8ayrdiLEjoJTT4pKTjrzG/e4uzWtTLDziC+66R9wbUqZBccJalSE5vQ==",
|
||||
"dependencies": [
|
||||
"@tailwindcss/node",
|
||||
"@tailwindcss/oxide",
|
||||
"tailwindcss@4.1.12",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3"
|
||||
]
|
||||
},
|
||||
"@tailwindcss/vite@4.1.12_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"@tailwindcss/vite@4.1.12_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"integrity": "sha512-4pt0AMFDx7gzIrAOIYgYP0KCBuKWqyW8ayrdiLEjoJTT4pKTjrzG/e4uzWtTLDziC+66R9wbUqZBccJalSE5vQ==",
|
||||
"dependencies": [
|
||||
"@tailwindcss/node",
|
||||
"@tailwindcss/oxide",
|
||||
"tailwindcss@4.1.12",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
]
|
||||
},
|
||||
"@trysound/sax@0.2.0": {
|
||||
@@ -2621,6 +2622,20 @@
|
||||
],
|
||||
"bin": true
|
||||
},
|
||||
"rollup-plugin-visualizer@6.0.3_rollup@4.50.0": {
|
||||
"integrity": "sha512-ZU41GwrkDcCpVoffviuM9Clwjy5fcUxlz0oMoTXTYsK+tcIFzbdacnrr2n8TXcHxbGKKXtOdjxM2HUS4HjkwIw==",
|
||||
"dependencies": [
|
||||
"open@8.4.2",
|
||||
"picomatch@4.0.3",
|
||||
"rollup",
|
||||
"source-map",
|
||||
"yargs"
|
||||
],
|
||||
"optionalPeers": [
|
||||
"rollup"
|
||||
],
|
||||
"bin": true
|
||||
},
|
||||
"rollup@4.50.0": {
|
||||
"integrity": "sha512-/Zl4D8zPifNmyGzJS+3kVoyXeDeT/GrsJM94sACNg9RtUE0hrHa1bNPtRSrfHTMH5HjRzce6K7rlTh3Khiw+pw==",
|
||||
"dependencies": [
|
||||
@@ -2870,35 +2885,35 @@
|
||||
"util-deprecate@1.0.2": {
|
||||
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
|
||||
},
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"integrity": "sha512-pKXZlgoXGoE8sEKiKJSng4hI1sQ4wi5YT24FCrwrLt6opmkjlqPPVmiPWWJn8M8byMxRGzp1CrFuqQs4M/Z39A==",
|
||||
"dependencies": [
|
||||
"birpc",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3",
|
||||
"vite-hot-client@2.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3",
|
||||
"vite-hot-client@2.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0"
|
||||
]
|
||||
},
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"integrity": "sha512-pKXZlgoXGoE8sEKiKJSng4hI1sQ4wi5YT24FCrwrLt6opmkjlqPPVmiPWWJn8M8byMxRGzp1CrFuqQs4M/Z39A==",
|
||||
"dependencies": [
|
||||
"birpc",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"vite-hot-client@2.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"vite-hot-client@2.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0"
|
||||
]
|
||||
},
|
||||
"vite-hot-client@2.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"vite-hot-client@2.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"integrity": "sha512-7SpgZmU7R+dDnSmvXE1mfDtnHLHQSisdySVR7lO8ceAXvM0otZeuQQ6C8LrS5d/aYyP/QZ0hI0L+dIPrm4YlFQ==",
|
||||
"dependencies": [
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3"
|
||||
]
|
||||
},
|
||||
"vite-hot-client@2.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"vite-hot-client@2.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"integrity": "sha512-7SpgZmU7R+dDnSmvXE1mfDtnHLHQSisdySVR7lO8ceAXvM0otZeuQQ6C8LrS5d/aYyP/QZ0hI0L+dIPrm4YlFQ==",
|
||||
"dependencies": [
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0"
|
||||
]
|
||||
},
|
||||
"vite-plugin-inspect@11.3.3_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"vite-plugin-inspect@11.3.3_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0": {
|
||||
"integrity": "sha512-u2eV5La99oHoYPHE6UvbwgEqKKOQGz86wMg40CCosP6q8BkB6e5xPneZfYagK4ojPJSj5anHCrnvC20DpwVdRA==",
|
||||
"dependencies": [
|
||||
"ansis",
|
||||
@@ -2909,11 +2924,11 @@
|
||||
"perfect-debounce",
|
||||
"sirv",
|
||||
"unplugin-utils",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3",
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3",
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0"
|
||||
]
|
||||
},
|
||||
"vite-plugin-inspect@11.3.3_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"vite-plugin-inspect@11.3.3_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0": {
|
||||
"integrity": "sha512-u2eV5La99oHoYPHE6UvbwgEqKKOQGz86wMg40CCosP6q8BkB6e5xPneZfYagK4ojPJSj5anHCrnvC20DpwVdRA==",
|
||||
"dependencies": [
|
||||
"ansis",
|
||||
@@ -2924,12 +2939,12 @@
|
||||
"perfect-debounce",
|
||||
"sirv",
|
||||
"unplugin-utils",
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.3__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0"
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0",
|
||||
"vite-dev-rpc@1.1.0_vite@7.1.4__@types+node@24.3.0__picomatch@4.0.3_@types+node@24.3.0_@types+node@24.2.0"
|
||||
]
|
||||
},
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3": {
|
||||
"integrity": "sha512-OOUi5zjkDxYrKhTV3V7iKsoS37VUM7v40+HuwEmcrsf11Cdx9y3DIr2Px6liIcZFwt3XSRpQvFpL3WVy7ApkGw==",
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3": {
|
||||
"integrity": "sha512-X5QFK4SGynAeeIt+A7ZWnApdUyHYm+pzv/8/A57LqSGcI88U6R6ipOs3uCesdc6yl7nl+zNO0t8LmqAdXcQihw==",
|
||||
"dependencies": [
|
||||
"@types/node@24.3.0",
|
||||
"esbuild",
|
||||
@@ -2947,8 +2962,8 @@
|
||||
],
|
||||
"bin": true
|
||||
},
|
||||
"vite@7.1.3_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0": {
|
||||
"integrity": "sha512-OOUi5zjkDxYrKhTV3V7iKsoS37VUM7v40+HuwEmcrsf11Cdx9y3DIr2Px6liIcZFwt3XSRpQvFpL3WVy7ApkGw==",
|
||||
"vite@7.1.4_@types+node@24.3.0_picomatch@4.0.3_@types+node@24.2.0": {
|
||||
"integrity": "sha512-X5QFK4SGynAeeIt+A7ZWnApdUyHYm+pzv/8/A57LqSGcI88U6R6ipOs3uCesdc6yl7nl+zNO0t8LmqAdXcQihw==",
|
||||
"dependencies": [
|
||||
"@types/node@24.2.0",
|
||||
"esbuild",
|
||||
@@ -3132,6 +3147,7 @@
|
||||
"npm:preact-render-to-string@^6.5.11",
|
||||
"npm:preact@^10.27.0",
|
||||
"npm:prismjs@^1.29.0",
|
||||
"npm:rollup@^4.50.0",
|
||||
"npm:tailwindcss@^4.1.10",
|
||||
"npm:ts-morph@26"
|
||||
],
|
||||
|
||||
@@ -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") {
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
import type { Plugin } from "vite";
|
||||
import * as cl from "@std/fmt/colors";
|
||||
import type { PluginContext } from "rollup";
|
||||
import path from "node:path";
|
||||
import { pathWithRoot } from "../utils.ts";
|
||||
|
||||
export interface ImportCheckDiagnostic {
|
||||
type: "warn" | "error";
|
||||
message: string;
|
||||
description?: string;
|
||||
hint?: string;
|
||||
}
|
||||
|
||||
export type ImportCheck = (
|
||||
id: string,
|
||||
env: "ssr" | "client",
|
||||
) => ImportCheckDiagnostic | void;
|
||||
|
||||
export interface CheckImportOptions {
|
||||
checks: ImportCheck[];
|
||||
}
|
||||
|
||||
export function checkImports(pluginOptions: CheckImportOptions): Plugin {
|
||||
function check(
|
||||
options: CheckImportOptions,
|
||||
id: string,
|
||||
env: "ssr" | "client",
|
||||
): ImportCheckDiagnostic | undefined {
|
||||
for (let i = 0; i < options.checks.length; i++) {
|
||||
const check = options.checks[i];
|
||||
|
||||
const result = check(id, env);
|
||||
if (result) return result;
|
||||
}
|
||||
}
|
||||
|
||||
let root = "";
|
||||
let isDev = false;
|
||||
|
||||
const seen = new Set<string>();
|
||||
|
||||
return {
|
||||
name: "fresh:check-imports",
|
||||
enforce: "pre",
|
||||
applyToEnvironment() {
|
||||
return true;
|
||||
},
|
||||
config(_, env) {
|
||||
isDev = env.command === "serve";
|
||||
},
|
||||
configResolved(config) {
|
||||
root = pathWithRoot(config.root);
|
||||
},
|
||||
async resolveId(id, importer, options) {
|
||||
if (
|
||||
id.startsWith("\0") || id.startsWith("/@fs/") ||
|
||||
id.startsWith("fresh-island::") || id.startsWith("fresh:") ||
|
||||
id.includes("node_modules") ||
|
||||
importer &&
|
||||
(importer.startsWith("\0") || importer.includes("node_modules") ||
|
||||
importer.includes("deno::"))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const env = options.ssr ? "ssr" : "client";
|
||||
|
||||
let result: ImportCheckDiagnostic | undefined;
|
||||
if (id.startsWith(".")) {
|
||||
const resolved = await this.resolve(id, importer, options);
|
||||
|
||||
if (resolved !== null) {
|
||||
const key = `${env}::${resolved.id}::${importer}`;
|
||||
if (!seen.has(key)) {
|
||||
result = check(pluginOptions, resolved.id, env);
|
||||
}
|
||||
|
||||
seen.add(key);
|
||||
}
|
||||
} else {
|
||||
const key = `${env}::${id}::${importer}`;
|
||||
if (!seen.has(key)) {
|
||||
result = check(pluginOptions, id, env);
|
||||
}
|
||||
seen.add(key);
|
||||
}
|
||||
|
||||
if (result) {
|
||||
const label = result.type === "warn"
|
||||
? cl.inverse(cl.yellow(` WARN `))
|
||||
: cl.inverse(cl.red(` ERROR `));
|
||||
|
||||
// deno-lint-ignore no-console
|
||||
console.log();
|
||||
// deno-lint-ignore no-console
|
||||
console.log();
|
||||
// deno-lint-ignore no-console
|
||||
console.log(`${label} ${result.message}`);
|
||||
// deno-lint-ignore no-console
|
||||
console.log();
|
||||
|
||||
if (importer !== undefined) {
|
||||
const ancestors = findAncestors(this, importer, isDev);
|
||||
|
||||
if (ancestors && ancestors.length > 0) {
|
||||
// deno-lint-ignore no-console
|
||||
console.log(`The specifier ${cl.cyan(`"${id}"`)} was imported in:`);
|
||||
ancestors.forEach((spec) => {
|
||||
if (path.isAbsolute(spec)) {
|
||||
spec = path.relative(root, spec);
|
||||
}
|
||||
// deno-lint-ignore no-console
|
||||
console.log(` - ${cl.cyan(spec)}`);
|
||||
});
|
||||
|
||||
// deno-lint-ignore no-console
|
||||
console.log();
|
||||
}
|
||||
}
|
||||
|
||||
if (result.hint) {
|
||||
// deno-lint-ignore no-console
|
||||
console.log(cl.bold(` hint: `) + result.hint);
|
||||
// deno-lint-ignore no-console
|
||||
console.log();
|
||||
}
|
||||
|
||||
if (result.type === "error") {
|
||||
this.error({
|
||||
message: result.message,
|
||||
id: importer,
|
||||
});
|
||||
} else {
|
||||
this.warn({
|
||||
message: result.message,
|
||||
id: importer,
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function findAncestors(
|
||||
ctx: PluginContext,
|
||||
id: string,
|
||||
isDev: boolean,
|
||||
): string[] | null {
|
||||
const mod = ctx.getModuleInfo(id);
|
||||
|
||||
if (mod === null) return null;
|
||||
|
||||
if (isDev || mod.importers.length === 0) {
|
||||
return [id];
|
||||
}
|
||||
|
||||
for (let i = 0; i < mod.importers.length; i++) {
|
||||
const importer = mod.importers[i];
|
||||
|
||||
const result = findAncestors(ctx, importer, isDev);
|
||||
if (result !== null) {
|
||||
result.push(id);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import * as path from "@std/path";
|
||||
import type { FsRouteFileNoMod, UniqueNamer } from "fresh/internal-dev";
|
||||
import type { ImportCheck } from "./plugins/verify_imports.ts";
|
||||
|
||||
export const JS_REG = /\.([tj]sx?|[mc]?[tj]s)(\?.*)?$/;
|
||||
export const JSX_REG = /\.[tj]sx?(\?.*)?$/;
|
||||
@@ -41,6 +42,7 @@ export interface FreshViteConfig {
|
||||
routeDir?: string;
|
||||
ignore?: RegExp[];
|
||||
islandSpecifiers?: string[];
|
||||
checkImports?: ImportCheck[];
|
||||
}
|
||||
|
||||
export type ResolvedFreshViteConfig =
|
||||
|
||||
@@ -375,3 +375,16 @@ Deno.test({
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite build - error on 'node:process' import",
|
||||
fn: async () => {
|
||||
const fixture = path.join(FIXTURE_DIR, "node_builtin");
|
||||
|
||||
await expect(buildVite(fixture)).rejects.toThrow(
|
||||
"Node built-in modules cannot be imported in the browser",
|
||||
);
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -309,3 +309,22 @@ Deno.test({
|
||||
sanitizeResources: false,
|
||||
sanitizeOps: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - error on 'node:process' import",
|
||||
fn: async () => {
|
||||
const fixture = path.join(FIXTURE_DIR, "node_builtin");
|
||||
|
||||
await launchDevServer(fixture, async (address) => {
|
||||
let res = await fetch(`${address}`);
|
||||
await res.body?.cancel();
|
||||
|
||||
res = await fetch(`${address}/@id/fresh-island::NodeIsland`);
|
||||
await res.body?.cancel();
|
||||
|
||||
expect(res.status).toEqual(500);
|
||||
});
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import process from "node:process";
|
||||
|
||||
export function NodeIsland() {
|
||||
return <h1>{process.version}</h1>;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { App, staticFiles } from "@fresh/core";
|
||||
|
||||
export const app = new App()
|
||||
.use(staticFiles())
|
||||
.fsRoutes();
|
||||
@@ -0,0 +1,5 @@
|
||||
import { NodeIsland } from "../islands/NodeIsland.tsx";
|
||||
|
||||
export default function Hello() {
|
||||
return <NodeIsland />;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { defineConfig } from "vite";
|
||||
import { fresh } from "@fresh/plugin-vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [fresh()],
|
||||
});
|
||||
Reference in New Issue
Block a user