From 26bc1b3fa6e0a14995923b4027c9120c58ff351c Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:33:31 +0900 Subject: [PATCH] perf(bun-plugin): write the runtime sheet only when a module changes it 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 --- ...angepack_log_bun_runtime_sheet_writes.json | 1 + .../bun-plugin/src/__tests__/plugin.test.ts | 23 +++++++++++++++++++ packages/bun-plugin/src/plugin.ts | 15 ++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .changepacks/changepack_log_bun_runtime_sheet_writes.json diff --git a/.changepacks/changepack_log_bun_runtime_sheet_writes.json b/.changepacks/changepack_log_bun_runtime_sheet_writes.json new file mode 100644 index 000000000..ce781471a --- /dev/null +++ b/.changepacks/changepack_log_bun_runtime_sheet_writes.json @@ -0,0 +1 @@ +{"changes":{"packages/bun-plugin/package.json":"Patch"},"note":"Under the Bun runtime, rewrite devup-ui.css only when a module adds or removes styles, from the sheet the extraction already serialized, instead of serializing and writing the whole sheet after every compiled module.","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/bun-plugin/src/__tests__/plugin.test.ts b/packages/bun-plugin/src/__tests__/plugin.test.ts index 6e2ea54a2..0cbc75eda 100644 --- a/packages/bun-plugin/src/__tests__/plugin.test.ts +++ b/packages/bun-plugin/src/__tests__/plugin.test.ts @@ -355,6 +355,29 @@ describe('registered Bun hooks', () => { expect(writeSync).not.toHaveBeenCalled() }) + it('writes the runtime sheet only when a module changes it', async () => { + const first = join(root, 'sheet-first.ts') + const same = join(root, 'sheet-same.ts') + const style = + "import { css } from '@devup-ui/react'; export const className = css({ color: 'olive' })" + await Bun.write(first, style) + await Bun.write(same, style) + await register({ engine: 'wasm' }) + + await load(first) + expect(writeSync).toHaveBeenCalledTimes(1) + expect(writeSync).toHaveBeenCalledWith( + expect.stringContaining('devup-ui.css'), + wasm.getCss(null, false), + 'utf-8', + ) + + // The same file again, and another file with only styles the sheet holds + await load(first) + await load(same) + expect(writeSync).toHaveBeenCalledTimes(1) + }) + it('propagates source read errors instead of returning partial output', async () => { await register({ engine: 'wasm' }) await expect(load(join(root, 'missing.ts'))).rejects.toThrow() diff --git a/packages/bun-plugin/src/plugin.ts b/packages/bun-plugin/src/plugin.ts index 284772623..d846551e8 100644 --- a/packages/bun-plugin/src/plugin.ts +++ b/packages/bun-plugin/src/plugin.ts @@ -155,14 +155,13 @@ async function loadSourceFile( importAliases, ) // Under the runtime the stylesheet is read from disk. singleCss stores - // every extracted style in the base sheet; synchronous writes keep - // concurrent source loads from overwriting a newer sheet with an older one. - if (!bundling) - writeFileSync( - join(cssDir, 'devup-ui.css'), - compiler.getCss(null, false), - 'utf-8', - ) + // every extracted style in the base sheet, which the output carries only + // when this module changed it, so a module that adds no style writes + // nothing. Synchronous writes keep concurrent source loads from + // overwriting a newer sheet with an older one. + const sheet = code.css + if (!bundling && sheet !== undefined) + writeFileSync(join(cssDir, 'devup-ui.css'), sheet, 'utf-8') return { contents: code.code, loader } } return { contents, loader }