Skip to content

Conversation

@robobun
Copy link
Collaborator

@robobun robobun commented Aug 14, 2025

Summary

  • Fixes GitHub issue Bun.build preserve important comments #9795: Bun.build preserve important comments
  • Makes Bun's bundler behavior consistent with esbuild's legal comment preservation
  • Preserves comments containing @license, @preserve, or @copyright (in addition to existing /*! and //! support)
  • NEW: Adds --legal-comments CLI option for full esbuild compatibility
  • NEW: Adds legalComments option to Bun.build() JavaScript API

Features

Legal Comment Preservation

Before this change:

/**
 * @license MIT
 * Important license info
 */
// Output: license comment was removed ❌

After this change:

/**
 * @license MIT
 * Important license info
 */
// Output: license comment is preserved ✅

Legal comment patterns now preserved:

  • /*! style comments (already supported)
  • //! style comments (already supported)
  • Comments containing @license (new)
  • Comments containing @preserve (new)
  • Comments containing @copyright (new)

CLI Option Support

NEW: --legal-comments option with esbuild-compatible values:

  • --legal-comments=none - Disable legal comment preservation
  • --legal-comments=inline - Keep legal comments inline in code
  • --legal-comments=eof - Move legal comments to end of file (default)
  • --legal-comments=linked - Extract to separate .LEGAL.txt file with link
  • --legal-comments=external - Extract to separate .LEGAL.txt file without link

Example CLI usage:

bun build src/index.js --legal-comments=eof --outdir=dist

JavaScript API Support

NEW: legalComments option in Bun.build():

await Bun.build({
  entrypoints: ["src/index.js"],
  legalComments: "eof", // "none" | "inline" | "eof" | "linked" | "external"
  outdir: "dist"
});

Features:

  • Full enum validation with helpful error messages
  • Same values as CLI option for consistency
  • Seamless integration with existing bundler options

Test plan

  • Added comprehensive tests covering all legal comment patterns
  • Verified existing comment tests still pass
  • Tested with minification enabled
  • Verified case sensitivity (only lowercase annotations are preserved, matching esbuild)
  • Tested nested comment positions
  • Tested CLI option with different values
  • Verified CLI help documentation
  • NEW: Added JavaScript API test in bun-build-api.test.ts
  • NEW: Verified API enum validation and error handling

Complete esbuild compatibility: Both CLI and JavaScript API now fully match esbuild's legal comment handling behavior.

Closes #9795

🤖 Generated with Claude Code

Bun.build now preserves legal comments like esbuild does:
- Comments starting with /*! or //! (already supported)
- Comments containing @license, @preserve, or @copyright (new)

This fixes GitHub issue #9795 by making Bun's bundler behavior
consistent with esbuild's legal comment preservation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@robobun
Copy link
Collaborator Author

robobun commented Aug 14, 2025

Updated 5:18 PM PT - Aug 14th, 2025

@autofix-ci[bot], your commit 70bc217 has 4 failures in Build #22983:


🧪   To try this PR locally:

bunx bun-pr 21870

That installs a local version of the PR into your bun-21870 executable, so you can run:

bun-21870 --bun

autofix-ci bot and others added 4 commits August 14, 2025 22:22
Adds the --legal-comments CLI option with values:
- none: Disable legal comment preservation
- inline: Keep legal comments inline in code (default for non-bundling)
- eof: Move legal comments to end of file (default for bundling)
- linked: Extract to separate .LEGAL.txt file with link
- external: Extract to separate .LEGAL.txt file without link

This provides full esbuild CLI compatibility for legal comment handling.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Adds full JavaScript API support for the legalComments option:

- Bun.build({ legalComments: "none" | "inline" | "eof" | "linked" | "external" })
- Proper enum validation with helpful error messages for invalid values
- Integration with bundler configuration pipeline
- Comprehensive test coverage in bun-build-api.test.ts

Example usage:
```javascript
await Bun.build({
  entrypoints: ["src/index.js"],
  legalComments: "eof", // Move legal comments to end of file
  outdir: "dist"
});
```

This completes the esbuild compatibility for legal comment handling
in both CLI and JavaScript API.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

@pfgithub pfgithub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected_version needs to be updated in RuntimeTranspilerCache.zig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bun.build preserve important comments

3 participants