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 }