Repository navigation
Description
Environment
On stackblitz with
"devDependencies": {
"nitro": "npm:nitro-nightly",
"vite": "^7"
},Reproduction
Open stackblitz and run vite build https://stackblitz.com/edit/nitrojs-nitro-vite-examples-ayvvjajm?file=vite.config.mjs
Describe the bug
I have a plugin to employ build time code replacement during renderChunk such as below:
load(id) {
if (id === '\0virtual:js') {
return `export default () => __build_data_placeholder__;`;
}
},
renderChunk(code, chunk) {
if (code.includes('__build_data_placeholder__')) {
this.emitFile({
// asset fails
type: 'asset',
fileName: '__build_data.mjs',
source: 'export default "build-data"',
// prebuilt-chunk works
// type: 'prebuilt-chunk',
// fileName: '__build_data.mjs',
// code: 'export default "build-data"',
});
code = code.replaceAll(
'__build_data_placeholder__',
// in pratice, the path should take into account `chunk.fileName`
`import("./__build_data.mjs")`
);
return code;
}
},During nitro environment build, Nitro fails to resolve such relative import as it goes through:
// #nitro-vite-entry
const service = { ... import("server.mjs") }
// virtual-bundle plugin resolveId
server.mjs -> (cwd)/server.mjs
// virtual-bundle plugin load
// (cwd)/server.mjs
... import("./__build_data.mjs") // <- this is not resolvedThis is because type: "asset" doesn't participate in virtual-bundle resolution override. It works with prebuilt-chunk but rolldown doesn't support it yet rolldown/rolldown#4034
Additional context
This technique is used in @vitejs/plugin-rsc.
Logs
❯ vite build
◐ Building SSR... nitro 6:18:42 PM
vite v7.1.6 building SSR bundle for production...
✓ 2 modules transformed.
✓ built in 80ms
◐ Building Nitro Server (preset: node-server, compatibility date: 2025-09-19) nitro 6:18:43 PM
✔ Generated public .output/public nitro 6:18:43 PM
vite v7.1.6 building SSR bundle for production...
✓ 9 modules transformed.
✗ Build failed in 448ms
error during build:
Could not resolve "./__build_data.mjs" from "server.mjs"
file: /home/projects/nitrojs-nitro-vite-examples-ayvvjajm/server.mjs
at getRollupError (file:///home/projects/nitrojs-nitro-vite-examples-ayvvjajm/node_modules/rollup/dist/es/shared/parseAst.js:568:41)
at Module.error (file:///home/projects/nitrojs-nitro-vite-examples-ayvvjajm/node_modules/rollup/dist/es/shared/parseAst.js:564:42)
at ModuleLoader.handleInvalidResolvedId (file:///home/projects/nitrojs-nitro-vite-examples-ayvvjajm/node_modules/rollup/dist/es/shared/node-entry.js:21604:37)
at ModuleLoader.resolveDynamicImport (file:///home/projects/nitrojs-nitro-vite-examples-ayvvjajm/node_modules/rollup/dist/es/shared/node-entry.js:21664:58)
at async eval (file:///home/projects/nitrojs-nitro-vite-examples-ayvvjajm/node_modules/rollup/dist/es/shared/node-entry.js:21548:32)Reacted by Pooya Parsa
Activity
[-][vite] build fails with relative import to `emitFile({ type: 'asset', ... })`[/-][+][vite] build fails with relative import to `emitFile({ type: 'asset' })`[/+]pi0 commentedon Sep 19, 2025
Shall we:
write: trueis set)prebuilt-chunkfor a current solution and wait for rolldown supporthi-ogawa commentedon Sep 19, 2025
I think this being opt-in is nice. I saw that the plugin changed not to override explicit
write: true#3532, so for this case, can we automatically disablevirtual-module?pi0 commentedon Sep 19, 2025
So you mean disable VFS when
write: trueis set? Makes sense.(only for simple single plugin usage i was thinking to keep it on by default and fix issues)
hi-ogawa commentedon Sep 19, 2025
Sorry, I said something confusing. It's fine this being opt-out. Detecting
write: trueseemed also weird and a bit confusing to do on rsc plugin side too. Btw, what was use case where #3532 was necessary?pi0 commentedon Sep 19, 2025
I don't have the full context. It must have been a requirement from the tanstack-start plugin.
hi-ogawa commentedon Oct 2, 2025
Coming back to this, I realized
emitFileis not what@vitejs/plugin-rscuses, but it requires direct fs writing to output (soemitFile({ type: "prebuilt-chunk"is not even possible).This is a common ssr framework build pattern to feed server and client build metadata back to server runtime (e.g. Astro and Sveltekit have similar build technique), so I believe Nitro should avoid this optimization for the time being.
Can you reconsider making this virtual bundle feature as opt-in?
pi0 commentedon Oct 2, 2025
More than happy to make it opt-in and reintroduce later.
What im worried is we didn’t gather enough context of the issue here and if not, we cannot enable restrictions back easily without breaking plugins.
Apart from performance, directly writing to server out dir can compromise runtime agnostic nature of nitro. Some deployment targets only allow single server file and nitro final build has to bundle all server assets as well.
By this you mean client/env manifest?
hi-ogawa commentedon Oct 2, 2025
I had impression that this is only for performance. Can you explain how this might lose agnosticness? Temporary build output is only written out to
.nitro/...directory (and then read back duringnitroenvironment build), so I thought this won't be an issue.pi0 commentedon Oct 2, 2025
Ah i thought you mean writing to .output/* (outdir). If temporary files are still resolvable by module graph (in last nitro env build) then thats fine.
Wondering why you can’t use emitFile. Is it because of lifecycle issues? Would later exposing a special nitro writeFile hook help adopting virtual fs?
hi-ogawa commentedon Oct 2, 2025
Yes, I think it's something like that. The idea of the above example is that plugin can point some internal code to a raw file
import("./__build_data.mjs")as an external during rollup build, then the actual__build_data.mjscan be prepared during framework-level build pipeline down the load. For example, Astro/Sveltekit/RSC's build order is "server -> client", so it's inherently impossible to doemitFileduring server build for the thing related to client build.