Skip to content

[vite] build fails with relative import to emitFile({ type: 'asset' }) #3577

Closed

Description

@hi-ogawa

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 resolved

This 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)

Activity

  1. added
    pending triage
    on Sep 19, 2025
  2. changed the title [-][vite] build fails with relative import to `emitFile({ type: 'asset', ... })`[/-] [+][vite] build fails with relative import to `emitFile({ type: 'asset' })`[/+] on Sep 19, 2025
  3. added
    bug
    vite
    and removed
    pending triage
    on Sep 19, 2025
  4. pi0 commented on Sep 19, 2025

    @pi0
    Member

    Shall we:

    • Add an option to opt-out from VFS as escape hatch/quick fix (also disable VFS when write: true is set)
    • Use prebuilt-chunk for a current solution and wait for rolldown support
  5. hi-ogawa commented on Sep 19, 2025

    @hi-ogawa
    ContributorAuthor

    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 disable virtual-module?

  6. pi0 commented on Sep 19, 2025

    @pi0
    Member

    So you mean disable VFS when write: true is set? Makes sense.

    (only for simple single plugin usage i was thinking to keep it on by default and fix issues)

  7. hi-ogawa commented on Sep 19, 2025

    @hi-ogawa
    ContributorAuthor

    Sorry, I said something confusing. It's fine this being opt-out. Detecting write: true seemed also weird and a bit confusing to do on rsc plugin side too. Btw, what was use case where #3532 was necessary?

  8. pi0 commented on Sep 19, 2025

    @pi0
    Member

    I don't have the full context. It must have been a requirement from the tanstack-start plugin.

  9. hi-ogawa commented on Oct 2, 2025

    @hi-ogawa
    ContributorAuthor

    Coming back to this, I realized emitFile is not what @vitejs/plugin-rsc uses, but it requires direct fs writing to output (so emitFile({ 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?

  10. pi0 commented on Oct 2, 2025

    @pi0
    Member

    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.

    feed server and client build metadata back to server runtime

    By this you mean client/env manifest?

  11. hi-ogawa commented on Oct 2, 2025

    @hi-ogawa
    ContributorAuthor

    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.

    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 during nitro environment build), so I thought this won't be an issue.

  12. pi0 commented on Oct 2, 2025

    @pi0
    Member

    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?

  13. hi-ogawa commented on Oct 2, 2025

    @hi-ogawa
    ContributorAuthor

    feed server and client build metadata back to server runtime

    By this you mean client/env manifest?

    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?

    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.mjs can 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 do emitFile during server build for the thing related to client build.

  14. added theissue type on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugvite

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions