Skip to content

Vite build fails to emit HTML file when generated by virtual file plugin #9662

@fwouts

Description

@fwouts

Describe the bug

When a file is generated by a virtual file plugin such as https://github.com/patak-dev/vite-plugin-virtual, the corresponding module ID may look like /@virtual:vite-plugin-virtual/some/path/to/file.

When the HTML entry point specified for vite build is provided by such a virtual file plugin, its content is correctly loaded, but bundling fails because shortEmitName looks like "../../../../@virtual:vite-plugin-virtual/Users/fwouts/dev/vite-virtual-html/index.html":

✓ 6 modules transformed.
rendering chunks (1)...[vite:build-html] The "fileName" or "name" properties of emitted files must be strings that are neither absolute nor relative paths, received "../../../../@virtual:vite-plugin-virtual/Users/fwouts/dev/vite-virtual-html/index.html".
error during build:
Error: The "fileName" or "name" properties of emitted files must be strings that are neither absolute nor relative paths, received "../../../../@virtual:vite-plugin-virtual/Users/fwouts/dev/vite-virtual-html/index.html".
    at error (file:///Users/fwouts/dev/vite-virtual-html/node_modules/.pnpm/rollup@2.77.3/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
    at FileEmitter.emitFile (file:///Users/fwouts/dev/vite-virtual-html/node_modules/.pnpm/rollup@2.77.3/node_modules/rollup/dist/es/shared/rollup.js:15472:24)
    at Object.generateBundle (file:///Users/fwouts/dev/vite-virtual-html/node_modules/.pnpm/vite@3.0.7/node_modules/vite/dist/node/chunks/dep-0f13c890.js:41599:22)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Overriding fileName with just the hardcoded value "index.html" here fixes it, although that's obviously not the right approach :)

Note 1: This appears related to #2321 but I don't think it's quite the same issue.

Note 2: In case you're wondering, one example where this would be useful is in Preview.js to allow users to generate a static, precompiled version of a particular component so they can share it around. For now, I can work around it by patching Vite on my end.

Note 3: I'd be happy to make a PR for this.

Reproduction

https://github.com/fwouts/vite-virtual-html

System Info

System:
    OS: macOS 12.5
    CPU: (8) arm64 Apple M1
    Memory: 87.44 MB / 8.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.9.0 - ~/.config/yarn/global/node_modules/.bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Firefox: 103.0.1
    Safari: 15.6
  npmPackages:
    vite: ^3.0.7 => 3.0.7

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions