fix: set useDefineForClassFields to false in tsup swc config#82
Merged
fix: set useDefineForClassFields to false in tsup swc config#82
Conversation
tsup's swcPlugin (enabled by emitDecoratorMetadata) bypasses esbuild's TypeScript handling and hardcodes jsc.target to es2022, which preserves native class field syntax. This causes class field initializers to run before constructor parameter assignments, breaking classes that depend on injected constructor parameters (e.g. HttpServer.port accessing this.config_data.get() before config_data is assigned). Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Releases: @tarpit/barbeque@2.0.1 @tarpit/cli@2.0.1 @tarpit/config@2.0.1 @tarpit/content-type@2.0.1 @tarpit/core@2.0.1 @tarpit/cron@2.0.1 @tarpit/dora@2.0.1 @tarpit/http@2.1.1 @tarpit/judge@2.0.1 @tarpit/mongodb@2.0.2 @tarpit/negotiator@2.0.1 @tarpit/rabbitmq@2.0.2 @tarpit/schedule@2.0.1 @tarpit/transformer@2.0.1 @tarpit/type-tools@2.0.1
e4fa1da to
8c4fe80
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
tsup's swcPlugin (activated by
emitDecoratorMetadata: true) intercepts all.tsfiles via an esbuildonLoadhook and transforms them with SWC instead of esbuild. The plugin hardcodesjsc.target: "es2022"and does not passuseDefineForClassFields: false, so SWC preserves native ES class field syntax.This causes class field initializers to execute before constructor parameter assignments at runtime:
Fix
Add a root-level
tsup.config.tsthat setsswc.jsc.transform.useDefineForClassFields: false. tsup's config resolution (via joycon) walks up from each package directory, so all packages automatically pick up this config.After the fix, the output correctly moves field initializers into the constructor body, after parameter assignments:
Verification
pnpm -r buildpasses, all packages use the root configpnpm -r testpasses (1 pre-existing network-dependent failure in rabbitmq unrelated to this change)