Skip to content

Fix SWC decorator parsing to support TS5 stage-3 decorators#94274

Open
arab971 wants to merge 1 commit into
vercel:canaryfrom
arab971:fix/ts5-decorators-support
Open

Fix SWC decorator parsing to support TS5 stage-3 decorators#94274
arab971 wants to merge 1 commit into
vercel:canaryfrom
arab971:fix/ts5-decorators-support

Conversation

@arab971

@arab971 arab971 commented May 30, 2026

Copy link
Copy Markdown

What?

Fix SWC decorator parsing to support TS5 stage-3 decorators. Currently, SWC rejects @decorator syntax at the parser level for TypeScript files unless experimentalDecorators is set in tsconfig, making TS5 stage-3 decorators impossible.

Why?

TypeScript 5 introduced stage-3 decorators as the default (no flag needed). Next.js users adopting TS5 decorators hit a parse error (Unexpected token '@') because SWC's parser only enabled decorator syntax when experimentalDecorators: true was set. This is the #1 blocker reported in #48360.

How?

  • Parser (swc/options.ts): Always enable decorator parsing for .ts/.tsx files, regardless of experimentalDecorators
  • Transform (swc/options.ts): Use decoratorVersion: "2022-03" (stage-3 Ecma proposal) when experimentalDecorators is not set, matching Turbopack's DecoratorsKind::Ecma behavior
  • Webpack (webpack-config.ts): Set decorators: true unconditionally; gate emitDecoratorMetadata on experimentalDecorators (metadata is a legacy-only feature)

Fixes #48360

Test plan

  • Integration test added: test/production/ts5-decorators/ — builds a page using TS5 decorators and verifies it renders
  • Verify pnpm test-start-webpack test/production/ts5-decorators/index.test.ts passes

Manual verification with issue reproduction app

# 1. Build local Next.js
pnpm --filter=next build

# 2. Download the reproduction app from #48360, then update dependencies:
cd <reproduction-app>
# In package.json, set:
#   "next": "link:<path-to-local-next.js>/packages/next"
#   "react": "^19.0.0", "react-dom": "^19.0.0"

# 3. Build with webpack (Turbopack has separate workspace root issue)
rm -f yarn.lock package-lock.json && pnpm install
npx next build --webpack

# Expected: ✓ Compiled successfully, decorator logs visible in output

…8360)

SWC rejected @decorator syntax at the parser level for TypeScript files
unless experimentalDecorators was set in tsconfig. This made TS5 stage-3
decorators impossible even when the transform was correctly configured.

- Always enable decorator parsing for .ts/.tsx files in SWC parser options
- Use decoratorVersion "2022-03" (stage-3) when experimentalDecorators is
  not set, matching Turbopack's DecoratorsKind::Ecma behavior
- Gate emitDecoratorMetadata on experimentalDecorators being enabled
- Add integration test for TS5 decorators in production build
@arab971 arab971 marked this pull request as ready for review May 30, 2026 21:57
@arab971 arab971 marked this pull request as draft May 30, 2026 22:01
@arab971 arab971 marked this pull request as ready for review May 30, 2026 22:01

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2022-03 sounds wrong

@arab971

arab971 commented Jun 3, 2026

Copy link
Copy Markdown
Author

@kdy1 I completely understand why that looks off—it looks like a date! However, 2022-03 is the specific version string required by the SWC compiler to trigger the Stage 3 (EcmaScript) decorator transformation. It's an internal SWC convention rather than a Next.js one. I've aligned this with Turbopack's DecoratorsKind::Ecma to ensure we have consistent behavior across both bundlers.

@arab971 arab971 requested a review from kdy1 June 4, 2026 17:25
@kdy1

kdy1 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Please do not ping me

@arab971 arab971 closed this Jul 3, 2026
@TechQuery

Copy link
Copy Markdown

Please do not ping me

What happened here? Why is this PR closed without reviewing? Why cannot contributers ping reviewers?

TS 7 has already been RC version, Next.js hasn't fully supported TS 5 ?!

Support custom SWC configuration just like Babel, or merge this PR please!

@arab971 arab971 reopened this Jul 4, 2026
@arab971

arab971 commented Jul 4, 2026

Copy link
Copy Markdown
Author

@TechQuery Thank you for your support. I apologize for closing the PR; I was feeling frustrated by the response from the reviewer, as well as the general lack of feedback and reviews over such a long period. I truly appreciate you bringing this to my attention.

@TechQuery

TechQuery commented Jul 5, 2026

Copy link
Copy Markdown

@TechQuery Thank you for your support. I apologize for closing the PR; I was feeling frustrated by the response from the reviewer, as well as the general lack of feedback and reviews over such a long period. I truly appreciate you bringing this to my attention.

You're welcome, Next.js mainteners are the people who should be blamed. I had similar situation in fixing the broken Vercel serverless template: vercel/examples#835

I think, maybe Vercel is humanless now. If I'm not too busy in the future, I will finish my SSR framework based on Web components standard & fully customable SWC compiler: EasyWebApp/WebSSR#1

@arab971

arab971 commented Jul 5, 2026

Copy link
Copy Markdown
Author

@lubieowoce — this PR fixes TS5/TS7 stage-3 decorator support in SWC builds. kdy1 confirmed the SWC convention is correct. Can you take a look?

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.

Typescript 5 decorators do not build

3 participants