From 342c264df5d4bd4a3c056c29ccf52434666b0976 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Sat, 7 Dec 2024 19:11:04 +0900 Subject: [PATCH] fix: handle external on server build --- packages/vite/misc/rolldown-runtime.js | 4 ++++ .../src/node/server/environments/rolldown.ts | 23 +------------------ 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/packages/vite/misc/rolldown-runtime.js b/packages/vite/misc/rolldown-runtime.js index 1a2ce4e5cb1b3c..09763937d6137e 100644 --- a/packages/vite/misc/rolldown-runtime.js +++ b/packages/vite/misc/rolldown-runtime.js @@ -109,6 +109,10 @@ var rolldown_runtime = (self.rolldown_runtime = { } var factory = this.moduleFactoryMap[id] if (!factory) { + // handle external + if (typeof __require_external !== 'undefined') { + return __require_external(id) + } throw new Error('Module not found: ' + id) } var module = (this.moduleCache[id] = { diff --git a/packages/vite/src/node/server/environments/rolldown.ts b/packages/vite/src/node/server/environments/rolldown.ts index 6a63ecb87eae81..500929078d00ff 100644 --- a/packages/vite/src/node/server/environments/rolldown.ts +++ b/packages/vite/src/node/server/environments/rolldown.ts @@ -412,9 +412,7 @@ class RolldownModuleRunner { __rolldown_hot: { send: () => {}, }, - // TODO: external require doesn't work in app format. - // TODO: also it should be aware of importer for non static require/import. - _require: require, + __require_external: require, } // TODO: support resolution? @@ -458,25 +456,6 @@ ${sourcemap} function patchRuntimePlugin(environment: RolldownEnvironment): rolldown.Plugin { return { name: 'vite:rolldown-patch-runtime', - // TODO: external require doesn't work in app format. - // rewrite `require -> _require` and provide _require from module runner. - // for now just rewrite known ones in "react-dom/server". - transform: { - filter: { - code: { - include: [/require\(['"](stream|util)['"]\)/], - }, - }, - handler(code) { - if (!environment.rolldownDevOptions.ssrModuleRunner) { - return - } - return code.replace( - /require(\(['"](stream|util)['"]\))/g, - '_require($1)', - ) - }, - }, renderChunk(code, chunk) { if (!chunk.isEntry) { return