evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
38.22k stars 1.16k forks source link

Single-line, non-license comment preserved in output #3943

Closed bbrk24 closed 1 month ago

bbrk24 commented 1 month ago

Current reproducer (haven't tried to minimize it): https://github.com/bbrk24/template-qdeql/tree/2d25c18334d914c15b3932baac03db60c849d3ef

The output index.mjs includes one single-line comment:

    // output is always UTF-8

Either this is a bug because that comment is emitted when it shouldn't be, or because no other single-line comment is emitted and they all should.

hyrious commented 1 month ago

esbuild intentionally keeps some comments because there're maybe other tools relying on them to work properly. For example vite supports /* vite-ignore */, some coverage tools may read /* istanbul ignore */ etc. However, esbuild is not designed to keep all comments.

If you want to remove all comments, you can enable --minify-whitespace.

Example.

bbrk24 commented 1 month ago

Why would it keep that comment in particular though? What makes that comment different from the other comments in the file or the rest of the project?

evanw commented 1 month ago

Comments in expression position are sometimes preserved because some tools that use esbuild to transform code make use of them. These comments are intended for machines, not for humans, and were added due to specific requests (for example: https://github.com/evanw/esbuild/issues/2721). This behavior is deliberate, and is not a bug.

Comments in statement position aren't currently preserved unless they contain special pragmas. Legal comments are used by library authors to encourage users of their libraries to embed notices about copyright or similar things. This is described in the documentation here: https://esbuild.github.io/api/#legal-comments. This behavior is also deliberate, and is also not a bug.