vite: require-externals facade loads lazily required externals at start-up (cosmiconfig -> typescript throws __filename), regression from #4889

Aperta
#5,164 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
43/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
node.js, typescript, vite

Direzione di ricerca

Start by tracing the nitro:require-externals facade and how it handles the lazy require() described in the issue, then inspect the generated _ssr/ssr.mjs and _libs/typescript.mjs output. Reproduce with a CommonJS dependency that requires typescript inside a function in a nitro/vite app. Done means the external is not evaluated at server startup when the lazy loader is unused, while the React external fix from #4889 still works.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • nitro 3.0.261103-rc (via nitro/vite), TanStack Start 1.168, Vite 8.2 (Rolldown), React 19.2
  • Node 22.23.4, Windows, Bun 1.3 as package manager
  • Preset: node server, built with vite build
What happens

On 3.0.260903-beta we hit #4171: use-sync-external-store/shim (via Base UI) left a runtime __require("react") in the SSR bundle, which loaded a second React and every SSR render failed with a null dispatcher.

3.0.261103-rc fixes that (thanks for #4889): the built server has no __require("react") left.

But the same change now loads, at server start-up, an external that CommonJS code only requires lazily. In our case cosmiconfig requires typescript lazily, and only to read .ts config files:

  • With the beta, the output kept it lazy (an await import(...) inside the loader).
  • With the RC it becomes a top-level var import_typescript = __toESM(require_typescript()) in _ssr/ssr.mjs, through the nitro:require-externals facade.

The bundled _libs/typescript.mjs (7.8 MB) then throws on load:

ReferenceError: __filename is not defined in ES module scope
    at _libs/typescript.mjs:7355

The server prints "Listening", but every request answers 500 {"error":true,"status":500,"unhandled":true}.

Expected

A require() that CommonJS makes lazily, inside a function, stays lazy, as it did in the beta. Or the facade is created lazily, so a dependency that is never used at run time is never evaluated.

Notes
  • Nothing in our Nitro config is affected by the RC's breaking changes (we use preset, features.websocket, plugins, traceDeps, compressPublicAssets, routeRules). The build itself runs clean, with no warnings.
  • For now we stay on the beta and redirect use-sync-external-store/shim and use-sync-external-store/shim/with-selector to an ESM module over React's own hook in the SSR environment, as a workaround for #4171.

I don't have a minimal repro yet. The trigger should be any bundled CommonJS dependency that does function load() { return require("typescript") } (cosmiconfig does this) in an app built with nitro/vite.

Lingua principale
TypeScript
Stelle
11.5k
Fork
968
Merge medio
4g 9h
PR unite (30g)
64

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nitrojs/nitro

Tutte le issue di nitrojs/nitro

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.