Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle external on server build #9

Open
wants to merge 1 commit into
base: chore-vue-example
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading