vite icon indicating copy to clipboard operation
vite copied to clipboard

`build.rollupOptions.output.banner` does not work when it is a comment string.

Open btea opened this issue 1 month ago • 3 comments

Describe the bug

{
  build: {
    minify: false,
    rollupOptions: {
      output: {
        banner: '/* test commnet */',
      },
    },
  },
}

After configuring as described above, there are no related comments at the top of the built artifacts.

context https://github.com/vitejs/vite/pull/21073#issuecomment-3495208448

Reproduction

https://stackblitz.com/edit/vitejs-vite-1cksxf2d?file=vite.config.js

Steps to reproduce

pnpm build

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 3.83 GB / 15.86 GB
  Binaries:
    Node: 24.8.0 - C:\Users\admin\AppData\Local\Volta\tools\image\node\24.8.0\node.EXE
    Yarn: 4.6.0 - C:\Users\admin\AppData\Local\Volta\tools\image\yarn\4.6.0\bin\yarn.CMD
    npm: 11.6.0 - C:\Users\admin\AppData\Local\Volta\tools\image\node\24.8.0\npm.CMD
    pnpm: 8.14.1 - C:\Program Files\Volta\pnpm.EXE
  Browsers:
    Chrome: 115.0.5790.171
    Edge: Chromium (140.0.3485.54)
    Internet Explorer: 11.0.19041.5794

Used Package Manager

pnpm

Logs

No response

Validations

btea avatar Nov 06 '25 10:11 btea

Quoting some comments from #21073:

I have another question: when I configure the banner as shown in the example below, the related comments don't appear in the actual output. It seems they've been removed by esbuild plugins. Is it possible to configure some options to retain the comments?

Ah, that's unexpected. Esbuild probably stripped them off when we run it on each output chunks, so probably you'd have to configure the banner on the esbuild option instead to workaround, but I wonder if we should like pass build.rollupOptions.output.banner to esbuild.banner to make it work.

This will not work with rolldown-vite as esbuild.banner is removed and oxc.banner does not exist. It has to use generateBundle hook to inject code/comments after minification (related: https://github.com/rolldown/rolldown/issues/6790).

sapphi-red avatar Nov 08 '25 06:11 sapphi-red

This will not work with rolldown-vite as esbuild.banner is removed and oxc.banner does not exist. It has to use generateBundle hook to inject code/comments after minification (related: rolldown/rolldown#6790).

Should we add the relevant logic plugins internally or explain it in the documentation?

It appears that Vite can currently use esbuild.banner to configure comments to take effect.

btea avatar Nov 09 '25 03:11 btea

Doing this properly requires having the knowledge of sourcemap combination, which feels too complicated for just injecting a string. I think we should provide a plugin or add output.postBanner/output.postFooter.

sapphi-red avatar Nov 10 '25 11:11 sapphi-red