Skip to content

refactor(fmt): POC SWCFMT without automatic fallback - #648

Draft
SoonIter wants to merge 7 commits into
mainfrom
refactor-official-swc-next-prettier-plugin
Draft

SoonIter wants to merge 7 commits into
mainfrom
refactor-official-swc-next-prettier-plugin

Conversation

@SoonIter

@SoonIter SoonIter commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Motivation

Evaluate the published @swc-next/formatter-prettier-plugin@0.7.0 as Rstack's default JS/TS formatter. Draft POC: top-level JS/TS now uses SWCFMT without automatic fallback, including templates. This improves throughput but does not preserve embedded-template formatting capability. Import-source remains unsupported.

Changes

  • Replace the local SWC parser/ESTree adapter with the official formatter plugin, retaining the swc-next / swc-next-ts aliases. Prettier stays at the plugin's exact peer 3.9.9; the original baseline uses 3.10.1.
  • Remove the backtick fallback and duplicate plugin/parser resolution. All default JS/TS files select SWCFMT; source is read once. Do not restore the legacy adapter.
  • Explicit parsers and project JS/TS parser plugins retain precedence. Other host languages, including their embedded JS/TS, keep their normal Prettier formatter.
  • Unsupported range (any finite rangeEnd), pragma and layout options throw explicitly before Prettier can skip an empty input. Unsupported syntax propagates; no retry or unchanged-output success is used.
  • Template HTML/CSS/GraphQL contents are not formatted, even with embedding enabled. Explicit babel / typescript selects Prettier when that capability is required. Update both documentation locales and focused behavior tests. Three existing source/test files receive only the formatting changes required by the new formatter.
  • Preserve all previous experiments in the benchmark history, including the slower automatic-fallback results and cause investigation. The new no-fallback report contains scripts, manifests, raw samples, output/AST/route audits and build hashes.

Four-worker CLI comparison

Fixed Rsbuild ad8388c5befec29f5ccf765857faf126c1f05d5d, isolated archive, unchanged config/ignore/plugin set. Old adapter: d1efeb3; prior automatic-fallback POC: dbf75f4; no-fallback implementation: e3d1c22. Same Apple M5 Max (18 cores, 64 GiB), macOS 26.6.2, Node 24.20.0, pnpm 12.18.0, --check --no-cache, two warmups and seven interleaved valid samples per cell. Process startup/config/discovery/workers/formatting are included; builds/copies/audits are excluded. Source repositories were not modified.

Scope Files Original adapter 3.10.1 Adapter at 3.9.9 Fallback POC No fallback New vs original New vs fallback Fallback / new
All JS/TS 2403 746.42 ms 743.98 ms 864.72 ms 497.86 ms -33.30% -42.43% 1.737x
Original fallback subset 462 413.43 ms 418.90 ms 603.73 ms 265.23 ms -35.85% -56.07% 2.276x
Complete supported manifest 3439 1702.91 ms 1702.12 ms 1905.50 ms 1634.33 ms -4.03% -14.23% 1.166x

Old adapters exit 0. Official variants exit 1 only for formatting differences (no-fallback changes 17 files; prior POC changes six). All selected files are processed. These are separate complete CLI invocations, not times to add. The known e2e/cases/wasm/wasm-source-import/src/index.js is excluded equally; a separate no-fallback check confirms exit 2 with formatting import phase ... is not implemented. Supported is not full syntax coverage.

Capability-matched control

All variants additionally run with embeddedLanguageFormatting: 'off'. This is a separate configuration; it also disables host-language embeds. In the old fallback POC this option already selects SWCFMT for the 462 backtick files.

Scope Adapter 3.10.1 Adapter 3.9.9 Fallback POC No fallback
All JS/TS 717.25 ms 711.57 ms 503.03 ms 495.43 ms
Original fallback subset 388.59 ms 388.54 ms 268.06 ms 265.10 ms
Complete supported manifest 1319.83 ms 1319.52 ms 1127.75 ms 1129.58 ms

With matched capability, the official-plugin routes are close. Most original-config gain comes from directing all JS/TS to SWCFMT, not merely removing repeated selection. The comparison with the old adapter still improves at matched embedding settings. Do not attribute all original-config savings to native formatter-core speed or use the embedding-off whole-scope number as the original-config result.

Best measured concurrency, independently confirmed

Scan 1/2/4/6/8/12/18 workers, all four variants at every count, two warmups plus seven interleaved samples. Choose the no-fallback median minimum per scope, then independently repeat that configuration with all baselines at the same worker count. Original configuration throughout.

Scope Workers Scan median Confirmation median (range) Original adapter Adapter 3.9.9 Fallback POC New vs original New vs fallback
All JS/TS 18 434.97 ms 439.04 ms (421.88–478.81) 611.28 ms 595.26 ms 731.39 ms -28.18% -39.97%
Supported manifest 12 1205.17 ms 1210.15 ms (1170.10–1241.12) 1248.96 ms 1206.70 ms 1376.66 ms -3.11% -12.10%

At 12 workers, supported no-fallback and the same-version old adapter are essentially equal. These are the best configurations measured on this machine/input, not a theoretical maximum or universal ranking. The default worker policy is unchanged; no hot API timing is mixed with CLI wall time. See every variant/count/sample/range.

Output, actual parser calls and measurement integrity

  • All 2387 nonempty top-level JS/TS files call the official parser: 862 JS, 1451 TS, 74 TSX. The other 16 are empty/whitespace-only and Prettier short-circuits; selected-route count is 2403. The original 462 fallback files all call SWCFMT (40 JS, 420 TS, 2 TSX), with zero built-in Babel/TS calls.
  • Other-language hosts retain 708 Babel and 2428 TypeScript embedded parser calls. This experiment does not force SWCFMT into host embeds.
  • Every JS/TS original/output was independently parsed with @swc-next/parser@0.7.0: no diagnostics or normalized AST/comment mismatches. Normalization retains observable tagged-template raw values. No-fallback adds 11 formatting differences to the prior six; template contents are unchanged on this Rsbuild input. Focused tests demonstrate the missing GraphQL embedding capability separately. This is structural validation, not execution equivalence; no Rsbuild runtime suite was run.
  • All 3439 supported files pass write and second-check idempotence. Separate audits at selected concurrency produce exactly the same output hashes as four workers for every variant.
  • Timing defers while the separate Parcel task builds/benchmarks; observed contaminated samples are retained with validity flags and retried. Background system services remain active. Early attempts and an entire partial scan are retained but excluded. A macOS temporary input directory lost files during external cleanup; the next invocation failed with no matched files. The scan was restarted under /tmp, with every input byte checked before each timing cell. No valid slow samples were discarded. No snapshots changed.

Reproduce

Build old-adapter, prior fallback and current checkouts with locked dependencies, pnpm build, then pnpm --filter rstack build:native.

python3 benchmarks/swc-next-prettier/no-fallback/prepare.py \
  --before /path/to/old-adapter --fallback /path/to/fallback-poc \
  --after /path/to/current --output /tmp/no-fallback-variants
python3 benchmarks/swc-next-prettier/no-fallback/sequence.py \
  --variants /tmp/no-fallback-variants/variants.json \
  --rsbuild /path/to/rsbuild --output /tmp/no-fallback-results

The CLI command is constructed without a shell: [node, variant/packages/rstack/bin/rs.js, fmt, ...manifest, --check, --no-cache, --parallel-workers, count]. Fixed manifests, config/archive/bundle hashes, runtime patch, route tracing and independent AST-audit commands are in the report. Historical results are preserved rather than overwritten.

Validation

Passed for the runtime change: pnpm build, native build, full pnpm test (457 passed, 1 skipped), focused behavior coverage, pnpm check, pnpm check:spell, full rs fmt --check --no-cache, website build and git diff --check. Final rebuilt formatter/worker/pool JavaScript matches the measured snapshot byte for byte; unrelated lazy app/test chunk names changed. Generated native files are unchanged. Rust source did not change.

All valid timing counts/medians, JSON/script syntax, route counts, output hashes and idempotence were checked. Diff artifacts normalize empty context-marker lines for repository whitespace checks without altering output hashes. CI should be assessed at the latest head; this remains a Draft POC.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Deploying rstack-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: fe5dd36
Status: ✅  Deploy successful!
Preview URL: https://807aebfb.rstack-cli.pages.dev
Branch Preview URL: https://refactor-official-swc-next-p.rstack-cli.pages.dev

View logs

@SoonIter SoonIter changed the title refactor(fmt): POC official SWC Next Prettier plugin refactor(fmt): POC SWCFMT without automatic fallback Nov 16, 2026

This branch has not been deployed

No deployments
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.

1 participant