feat(vite): error when using Deno global in the browser (#3305)

Related https://github.com/denoland/fresh/issues/3296
This commit is contained in:
Marvin Hagemeister
2025-09-01 19:56:31 +02:00
committed by GitHub
parent 87b213558b
commit ae2a27e174
10 changed files with 101 additions and 8 deletions
+15 -8
View File
@@ -6,6 +6,7 @@ import babelReact from "@babel/preset-react";
import { inlineEnvVarsPlugin } from "./patches/inline_env_vars.ts";
import { removePolyfills } from "./patches/remove_polyfills.ts";
import { JS_REG, JSX_REG } from "../utils.ts";
import { denoGlobal } from "./patches/deno_global.ts";
export function patches(): Plugin {
let isDev = false;
@@ -30,18 +31,24 @@ export function patches(): Plugin {
}]);
}
const plugins: babel.PluginItem[] = [
cjsPlugin,
removePolyfills,
jsxComments,
inlineEnvVarsPlugin(
isDev ? "development" : "production",
),
];
if (!options?.ssr) {
plugins.push(denoGlobal);
}
const res = babel.transformSync(code, {
filename: id,
babelrc: false,
compact: true,
plugins: [
cjsPlugin,
removePolyfills,
jsxComments,
inlineEnvVarsPlugin(
isDev ? "development" : "production",
),
],
plugins,
presets,
});
@@ -0,0 +1,29 @@
import type { PluginObj, types } from "@babel/core";
export function denoGlobal({ types: t }: { types: typeof types }): PluginObj {
return {
name: "fresh-deno-global",
visitor: {
Identifier(path) {
if (path.node.name === "Deno") {
// Ignore `Deno.env.get()` calls. Those will be inlined.
if (
t.isMemberExpression(path.parent) &&
path.parent.object === path.node &&
t.isIdentifier(path.parent.property) &&
path.parent.property.name === "env" &&
t.isMemberExpression(path.parentPath?.parent) &&
t.isIdentifier(path.parentPath?.parent.property) &&
path.parentPath?.parent.property.name === "get"
) {
return;
}
throw path.buildCodeFrameError(
`The Deno.* global cannot be used in the browser.`,
);
}
},
},
};
}
+24
View File
@@ -388,3 +388,27 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - throw when using Deno.* global inside an island",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "deno_global_island");
await expect(buildVite(fixture)).rejects.toThrow(
"The Deno.* global cannot be used in the browser",
);
},
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "vite build - don't throw when using Deno.* global in ssr",
fn: async () => {
const fixture = path.join(FIXTURE_DIR, "deno_global_ssr");
await buildVite(fixture);
},
sanitizeOps: false,
sanitizeResources: false,
});
@@ -0,0 +1,3 @@
export function Foo() {
return <h1>{Deno.cwd()}</h1>;
}
@@ -0,0 +1,5 @@
import { App, staticFiles } from "@fresh/core";
export const app = new App()
.use(staticFiles())
.fsRoutes();
@@ -0,0 +1,5 @@
import { Foo } from "../islands/Foo.tsx";
export default function Hello() {
return <Foo />;
}
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});
@@ -0,0 +1,5 @@
import { App, staticFiles } from "@fresh/core";
export const app = new App()
.use(staticFiles())
.fsRoutes();
@@ -0,0 +1,3 @@
export default function Hello() {
return <h1>{Deno.cwd()}</h1>;
}
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import { fresh } from "@fresh/plugin-vite";
export default defineConfig({
plugins: [fresh()],
});