Skip to content

perf(bun-plugin): write the runtime sheet only when a module changes it - #942

Merged
owjs3901 merged 1 commit into
mainfrom
perf/bun-runtime-sheet-writes
Nov 15, 2026
Merged

owjs3901 merged 1 commit into
mainfrom
perf/bun-runtime-sheet-writes

Conversation

@owjs3901

Copy link
Copy Markdown
Contributor

Summary

PLG-24 재검증 결과입니다.

  • Bun.build 경로: main에서 이미 고쳐져 있습니다. CSS는 onLoad의 defer() 뒤에 한 번 만들어 돌려주고 디스크에 쓰지 않습니다.
  • Bun 런타임 경로 (남은 부분): 컴파일되는 모듈마다 compiler.getCss(null, false)로 시트 전체를 직렬화한 뒤 writeFileSync로 썼습니다. 스타일을 하나도 더하지 않는 모듈도 마찬가지라 N개 모듈에서 직렬화와 동기 I/O가 O(N)번씩 일어났습니다.

변경

  • Bun 플러그인은 singleCss가 항상 true라, 추출 결과의 Output.css는 이 모듈이 시트를 바꿨을 때만 직렬화된 base 시트를 담습니다(libs/binding/src/output.rs: collected나 default_collected가 없으면 None). 이 값은 getCss(null, false)와 같은 create_css(None, false)입니다.
  • 이제 런타임에서는 code.css가 있을 때만 그 문자열을 씁니다. 시트를 바꾸지 않는 모듈은 직렬화도 쓰기도 하지 않습니다.
  • 쓰기는 계속 동기입니다. 런타임은 import 직후 디스크에서 시트를 읽으므로(read-after-import), 디바운스/비동기화는 이 계약을 깨서 하지 않았습니다. 그래서 스타일을 새로 더하는 모듈마다 시트 전체를 쓰는 비용은 남습니다.

겹치는 PR

Test plan

  • bun test packages/bun-plugin/src/__tests__/plugin.test.ts (25 pass)
    • 새 테스트: 첫 로드에서 한 번 쓰고 내용이 getCss(null, false)와 같음, 같은 파일 재로드와 이미 있는 스타일만 쓰는 다른 파일은 쓰지 않음
  • bun test packages/bun-plugin: 실패 4개는 이 변경 전 main에서도 로컬에서 똑같이 실패합니다(실제 빌드 root 비교 테스트, 로컬 native/WASM 빌드 문제)
  • bunx tsc --noEmit -p packages/bun-plugin, prettier

Refs #686

🤖 Generated with Claude Code

Under the Bun runtime every compiled module serialized the whole sheet
and wrote it synchronously, even when it added no style. The extraction
output already carries the serialized base sheet exactly when the
module changed it (singleCss), so write that and skip the rest. Writes
stay synchronous: the runtime reads the sheet right after the import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
packages/bun-plugin/src/plugin.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@owjs3901
owjs3901 merged commit f61479e into main Nov 15, 2026
27 checks passed
@owjs3901
owjs3901 deleted the perf/bun-runtime-sheet-writes branch November 15, 2026 22:41
owjs3901 added a commit that referenced this pull request Nov 15, 2026
Conflicts:
- webpack, Rsbuild and Vite plugin.ts imports: kept main's setupWarning
  (#945) next to this branch's SOURCE_FILE_REGEX.
- Rsbuild transform: kept this branch's `environment` parameter, with
  main's hoisted excludeRegex (#914) in place of the per-call
  createNodeModulesExcludeRegex(include).
- Vite transform: kept this branch's SOURCE_FILE_REGEX check, with
  main's hoisted excludeRegex.
- Bun loadSourceFile: kept main's runtime sheet write, which writes only
  the sheet a module changed (#942), and this branch's
  withDependencyImports return.

Dropped from this branch in favor of main: the per-call exclude regex in
the Rsbuild and Vite transforms (#914) and the unconditional runtime
sheet write in Bun (#942).

Also updated the "Compiled Files" section that #949 added to the Bun
README: `.mts`/`.cts` are compiled, and `Bun.build` compiles the
project's own `.js`/`.cjs` outside node_modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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