Skip to content

Commit

Permalink
fix: handle external on server build
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Dec 7, 2024
1 parent 8861521 commit 342c264
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/vite/misc/rolldown-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
23 changes: 1 addition & 22 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 342c264

Please sign in to comment.