Dev: client optimizeDeps excludes serverExternalPackages, which breaks optional requires in client deps (linkedom → canvas)
vinext 1.0.0-beta.12 (also on main at c4b705e), App Router, Vite 8.3.1.
The client environment's optimizeDeps.exclude includes the whole default serverExternalPackages list (packages/vinext/src/index.ts, the client environment in the App Router config). canvas is on that list. A client dependency that requires canvas optionally, inside a try/catch, then breaks in vinext dev:
linkedom's commonjs/canvas.cjs:
try { module.exports = require('canvas'); } catch (fallback) { module.exports = require('./canvas-shim.cjs'); }With canvas excluded, the dep optimizer treats it as external and hoists the require into a top-level import * as m from "canvas" in .vite/deps/linkedom.js. Vite resolves that import to __vite-optional-peer-dep:canvas:linkedom, which throws on evaluation, outside the try. Any client module that imports linkedom fails to load:
Error: Could not resolve "canvas" imported by "linkedom".
at http://localhost:5174/@id/__vite-optional-peer-dep:canvas:linkedom:2:7Plain Vite (no exclusion) bundles the optional peer dep inside the try, and the shim is used. vinext build is not affected.
Repro: an App Router client component that does import("linkedom") in the browser, canvas not installed, vinext dev.
Workaround: a plugin whose configEnvironment hook removes canvas from the client environment's optimizeDeps.exclude.
Suggested fix: exclude server-external packages from the client optimizer only when they are imported directly from app code, or do not apply the list to packages that are reached only through a dependency's require.
Source: cloudflare/vinext