Skip to content

Commit 6540ac9

Browse files
bartlomiejuclaude
andcommitted
fix: guard Deno global access in build-id for browser compat
build-id/mod.ts uses Deno.env.get() which throws in the browser. The old deno.ts plugin transformed this away, but @deno/vite-plugin serves it to the client untransformed. Also: - Add exclude option to @deno/vite-plugin config to skip Fresh virtual module IDs (fresh-island::, fresh-client-island::, fresh:) - Skip Fresh virtual module prefixes in freshSsrTransform resolveId Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9a383ab commit 6540ac9

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

packages/build-id/mod.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414

1515
import { encodeHex } from "@std/encoding/hex";
1616

17-
export const DENO_DEPLOYMENT_ID: string | undefined = Deno.env.get(
17+
// deno-lint-ignore no-explicit-any
18+
const _Deno = typeof Deno !== "undefined" ? Deno : undefined as any;
19+
20+
export const DENO_DEPLOYMENT_ID: string | undefined = _Deno?.env.get(
1821
"DENO_DEPLOYMENT_ID",
1922
);
2023
const deploymentId = DENO_DEPLOYMENT_ID ||
2124
// For CI
22-
Deno.env.get("GITHUB_SHA") ||
23-
Deno.env.get("CI_COMMIT_SHA") ||
25+
_Deno?.env.get("GITHUB_SHA") ||
26+
_Deno?.env.get("CI_COMMIT_SHA") ||
2427
crypto.randomUUID();
2528
const buildIdHash = await crypto.subtle.digest(
2629
"SHA-1",

packages/plugin-vite/src/mod.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
277277
client: { platform: "browser", preserveJsx: true, cachedOnly: true },
278278
},
279279
onLoad: createFreshOnLoad(() => isDev),
280+
exclude: [
281+
"fresh-island",
282+
"fresh-client-island",
283+
"fresh:",
284+
"@fresh/build-id",
285+
],
280286
};
281287
// deno-lint-ignore no-explicit-any
282288
plugins.push(...denoPlugin(denoOpts) as any);

packages/plugin-vite/src/plugins/deno_transforms.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,19 @@ export function freshSsrTransform(
7878
id.startsWith("\0") ||
7979
id.startsWith("node:") ||
8080
id.startsWith("npm:") ||
81-
id.startsWith("jsr:")
81+
id.startsWith("jsr:") ||
82+
id.startsWith("fresh-island::") ||
83+
id.startsWith("fresh-client-island::") ||
84+
id.startsWith("fresh:")
8285
) {
8386
return;
8487
}
8588

89+
// deno-lint-ignore no-console
90+
console.log(
91+
`[fresh:ssr-transform] resolveId: "${id}" (env: ${this.environment?.name})`,
92+
);
93+
8694
// Try to resolve through the Deno loader's import map
8795
try {
8896
const l = await loader();
@@ -91,16 +99,26 @@ export function freshSsrTransform(
9199
undefined,
92100
ResolutionMode.Import,
93101
);
102+
// deno-lint-ignore no-console
103+
console.log(
104+
`[fresh:ssr-transform] loader resolved "${id}" -> "${resolved}"`,
105+
);
94106
if (resolved && resolved !== id) {
95107
// Re-resolve the result through the plugin pipeline
96108
// so @deno/vite-plugin can create the proper deno:: specifier
97109
const result = await this.resolve(resolved, undefined, {
98110
...options,
99111
skipSelf: true,
100112
});
113+
// deno-lint-ignore no-console
114+
console.log(
115+
`[fresh:ssr-transform] pipeline re-resolved -> ${result?.id}`,
116+
);
101117
return result;
102118
}
103-
} catch {
119+
} catch (err) {
120+
// deno-lint-ignore no-console
121+
console.log(`[fresh:ssr-transform] loader failed for "${id}": ${err}`);
104122
// Not resolvable by Deno — let Vite handle it
105123
}
106124
},

0 commit comments

Comments
 (0)