Skip to content

feature: (esbuild): add --esbuild-define flag with dead code elimination#82

Merged
divyenduz merged 1 commit intodivyenduz:mainfrom
mattiarossi:feature/81-esbuild-define-support
Apr 1, 2026
Merged

feature: (esbuild): add --esbuild-define flag with dead code elimination#82
divyenduz merged 1 commit intodivyenduz:mainfrom
mattiarossi:feature/81-esbuild-define-support

Conversation

@mattiarossi
Copy link
Copy Markdown
Contributor

Adds a new --esbuild-define CLI flag that accepts a JSON object of string key-value pairs and passes them to esbuild's define option. When defines are present, minifySyntax is automatically enabled so esbuild performs dead code elimination — removing unreachable branches entirely without mangling names or collapsing whitespace.

This enables zero-cost optional infrastructure code in PLV8 functions (e.g. snapshot capture, debug logging) that can be compiled out in production builds:

plv8ify generate --input-file input.ts
--esbuild-define '{"ENABLE_SNAPSHOT_CAPTURE":"false"}'

Changes:

  • src/interfaces/Bundler.ts: add optional define to BundleArgs
  • src/interfaces/PLV8ify.ts: add optional esbuildDefine to BuildArgs
  • src/impl/EsBuild.ts: spread define + minifySyntax into esbuild options when defines present
  • src/impl/PLV8ifyCLI.ts: thread esbuildDefine through build() to bundle()
  • src/helpers/ParseCLI.ts: add esbuildDefine to CLIConfig; add --esbuild-define optional flag with JSON parsing
  • src/commands/generate.ts: destructure and pass esbuildDefine to plv8ify.build()
  • src/impl/EsBuild.test.ts: tests for DCE with ENABLE_FEATURE=true/false and backward compat
  • src/test-fixtures/define.fixture.ts: fixture for define tests
  • README.md: new section and flag table entry documenting the feature

Backward compatible: omitting --esbuild-define produces identical output to current behavior.

Adds a new --esbuild-define CLI flag that accepts a JSON object of
string key-value pairs and passes them to esbuild's define option.
When defines are present, minifySyntax is automatically enabled so
esbuild performs dead code elimination — removing unreachable branches
entirely without mangling names or collapsing whitespace.

This enables zero-cost optional infrastructure code in PLV8 functions
(e.g. snapshot capture, debug logging) that can be compiled out in
production builds:

  plv8ify generate --input-file input.ts \
    --esbuild-define '{"ENABLE_SNAPSHOT_CAPTURE":"false"}'

Changes:
- src/interfaces/Bundler.ts: add optional define to BundleArgs
- src/interfaces/PLV8ify.ts: add optional esbuildDefine to BuildArgs
- src/impl/EsBuild.ts: spread define + minifySyntax into esbuild options when defines present
- src/impl/PLV8ifyCLI.ts: thread esbuildDefine through build() to bundle()
- src/helpers/ParseCLI.ts: add esbuildDefine to CLIConfig; add --esbuild-define optional flag with JSON parsing
- src/commands/generate.ts: destructure and pass esbuildDefine to plv8ify.build()
- src/impl/EsBuild.test.ts: tests for DCE with ENABLE_FEATURE=true/false and backward compat
- src/test-fixtures/define.fixture.ts: fixture for define tests
- README.md: new section and flag table entry documenting the feature

Backward compatible: omitting --esbuild-define produces identical output to current behavior.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@divyenduz
Copy link
Copy Markdown
Owner

@mattiarossi I assume you read the code + tested it already? Asking because I see claude code traces :D, will try to get this merged later today! Merging might not release it anymore. IIRC I got a email from npm about 2FA that I haven't addressed yet. On my list.

@mattiarossi
Copy link
Copy Markdown
Contributor Author

@mattiarossi I assume you read the code + tested it already? Asking because I see claude code traces :D, will try to get this merged later today! Merging might not release it anymore. IIRC I got a email from npm about 2FA that I haven't addressed yet. On my list.

Yes, I used claude code to make a pull request of the patch that was originally created, also used claude to create the tests as well.
I am already using the patched version to test it works with real use cases

@divyenduz
Copy link
Copy Markdown
Owner

Perfect, thank you. I will try to get to reviewing it as soon as possible. Targeting today evening.

Copy link
Copy Markdown
Owner

@divyenduz divyenduz left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution ✅

@divyenduz divyenduz merged commit 866022d into divyenduz:main Apr 1, 2026
2 checks passed
@divyenduz
Copy link
Copy Markdown
Owner

divyenduz commented Apr 1, 2026

There is some issue with the release pipeline (I need to configure 2FA + npm properly), so this is not released yet in npm. Will post the version here once that is done.

(update: that would happen after the easter holidays, so 2nd or 3rd week of April, sorry for the delay 🙏🏼)

@divyenduz
Copy link
Copy Markdown
Owner

Fix release CI via #84

In the future, you can use bun x changeset to include a changeset that would generate a PR like this #85 that would cause the release.

I will document this process separately in README.md.

This particular change is released in version 0.0.67. Thank you again for the contribution.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants