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