Skip to content

Commit 5c9ecc3

Browse files
bartlomiejuclaude
andcommitted
fix: use optimizeDeps.exclude for preact to avoid duplicate instances
Instead of disabling the dependency optimizer entirely (noDiscovery), exclude only preact ecosystem packages from optimization. This allows CJS packages like mime-db to be pre-bundled for the browser while preventing duplicate preact instances when remote (JSR) islands resolve deps to /@fs/ paths. Also extends the CJS shim to work in both SSR (with createRequire) and client (with stub require) environments. All dev server tests pass (35/36 — 1 pre-existing flaky failure on remote island that also fails on main). All 31 build tests pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fd9e463 commit 5c9ecc3

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

deno.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/plugin-vite/src/mod.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,26 @@ export function fresh(config?: FreshViteConfig): Plugin[] {
149149
},
150150
},
151151

152+
optimizeDeps: {
153+
// Exclude preact ecosystem from optimizer to prevent
154+
// duplicate instances with remote (JSR) islands. JSR
155+
// islands resolve deps to /@fs/ paths, so if the
156+
// optimizer also bundles them to /.vite/deps/, two
157+
// separate instances load. Other CJS packages (like
158+
// mime-db) are optimized normally.
159+
exclude: [
160+
"preact",
161+
"preact/hooks",
162+
"preact/jsx-runtime",
163+
"preact/jsx-dev-runtime",
164+
"preact/debug",
165+
"preact/compat",
166+
"@preact/signals",
167+
"@preact/signals-core",
168+
"preact-render-to-string",
169+
],
170+
},
171+
152172
publicDir: pathWithRoot(fConfig.staticDir[0], config.root),
153173

154174
builder: {

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ export function deno(): Plugin {
184184
? ssrLoader
185185
: browserLoader;
186186

187-
// In dev mode, non-external CJS files go through Vite's SSR
188-
// module runner which evaluates them as ESM. Wrap CJS files
189-
// with an ESM shim that provides module/exports/require. In
190-
// build mode, Rollup's @rollup/plugin-commonjs handles CJS.
187+
// In dev mode, CJS files need to be wrapped in an ESM shim:
188+
// - SSR: module runner evaluates as ESM, needs module/exports/require
189+
// - Client: browser evaluates as ESM, needs module/exports
190+
// In build mode, Rollup's @rollup/plugin-commonjs handles CJS.
191191
if (
192192
isDev &&
193193
!id.startsWith("\0") &&
@@ -204,13 +204,20 @@ export function deno(): Plugin {
204204
code.includes("exports.") ||
205205
code.includes("require("))
206206
) {
207-
const wrapped = `
208-
import { createRequire as __fresh_createRequire } from "node:module";
209-
import { fileURLToPath as __fresh_fileURLToPath } from "node:url";
210-
import { dirname as __fresh_dirname } from "node:path";
211-
var __filename = __fresh_fileURLToPath(import.meta.url);
212-
var __dirname = __fresh_dirname(__filename);
213-
var require = __fresh_createRequire(import.meta.url);
207+
const isServer =
208+
this.environment.config.consumer === "server";
209+
const preamble = isServer
210+
? `import { createRequire as __cjs_createRequire } from "node:module";
211+
import { fileURLToPath as __cjs_fileURLToPath } from "node:url";
212+
import { dirname as __cjs_dirname } from "node:path";
213+
var __filename = __cjs_fileURLToPath(import.meta.url);
214+
var __dirname = __cjs_dirname(__filename);
215+
var require = __cjs_createRequire(import.meta.url);`
216+
: `var __filename = "";
217+
var __dirname = "";
218+
var require = (id) => { throw new Error("require() not supported in browser: " + id); };`;
219+
220+
const wrapped = `${preamble}
214221
var module = { exports: {} };
215222
var exports = module.exports;
216223

0 commit comments

Comments
 (0)