From ee604068b6d47222cc7b01d9f03405299e73724f Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 18:27:42 +0900 Subject: [PATCH] test(rsbuild-plugin): drive the plugin through its real hook sequence The existing suite mocks every file write as already done, which hides ordering. The new suite runs setup against a fake Rsbuild API and fake Rspack compilers with the real compiler and a temporary file system: - the hooks are registered in the order a build runs them, and only the build end when CSS is not extracted - a cold, nested cssDir exists before devup-ui.css is written into it - a same-length edit (red to tan) is served and written - with two environments, only the one whose served sheet went stale asks for one additional pass, and only once per run The cold directory case failed with ENOENT: mkdir and the first write ran side by side in Promise.all. writeDataFiles now awaits mkdir before it writes. Refs #683 (PLG-26) Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_rsbuild_cold_css_dir.json | 7 + .../src/__tests__/hooks.test.ts | 309 ++++++++++++++++++ packages/rsbuild-plugin/src/plugin.ts | 19 +- 3 files changed, 324 insertions(+), 11 deletions(-) create mode 100644 .changepacks/changepack_log_rsbuild_cold_css_dir.json create mode 100644 packages/rsbuild-plugin/src/__tests__/hooks.test.ts diff --git a/.changepacks/changepack_log_rsbuild_cold_css_dir.json b/.changepacks/changepack_log_rsbuild_cold_css_dir.json new file mode 100644 index 000000000..bc6e86f67 --- /dev/null +++ b/.changepacks/changepack_log_rsbuild_cold_css_dir.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/rsbuild-plugin/package.json": "Patch" + }, + "note": "Create the Rsbuild CSS directory before writing devup-ui.css into it, so a first build into a missing nested cssDir no longer races mkdir against the write.", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/packages/rsbuild-plugin/src/__tests__/hooks.test.ts b/packages/rsbuild-plugin/src/__tests__/hooks.test.ts new file mode 100644 index 000000000..30049ed18 --- /dev/null +++ b/packages/rsbuild-plugin/src/__tests__/hooks.test.ts @@ -0,0 +1,309 @@ +import { + existsSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, +} from 'node:fs' +import * as fsPromises from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +import { + afterEach, + beforeEach, + describe, + expect, + it, + mock, + spyOn, +} from 'bun:test' + +import { DevupUI } from '../plugin' + +// The plugin driven through the hooks Rsbuild calls, in the order it calls +// them, with the real compiler and a real (temporary) file system: the +// sibling suite mocks every write as already done, which hides ordering. + +type Setup = Parameters['setup']>[0] + +interface Environment { + name: string + entry: Record +} + +interface TransformContext { + code: string + resourcePath: string + environment: Environment + addDependency: (path: string) => void +} + +type Transform = (context: TransformContext) => unknown + +interface RspackPlugin { + apply(compiler: FakeCompiler): void +} + +type ModifyRspackConfig = ( + config: { plugins?: RspackPlugin[] }, + utils: { environment: Environment }, +) => void + +type Tap = (...args: unknown[]) => unknown + +interface FakeCompiler { + options: { entry: Record } + hooks: Record void }> + rspack: { Compilation: { PROCESS_ASSETS_STAGE_REPORT: number } } + modifiedFiles?: Set +} + +function fakeApi(root: string) { + const order: string[] = [] + const transforms: { css: boolean; handler: Transform }[] = [] + const rspackConfigs: ModifyRspackConfig[] = [] + const api = { + context: { rootPath: root }, + onCloseBuild: () => { + order.push('onCloseBuild') + }, + modifyRspackConfig: (fn: ModifyRspackConfig) => { + order.push('modifyRspackConfig') + rspackConfigs.push(fn) + }, + onBeforeBuild: () => { + order.push('onBeforeBuild') + }, + modifyRsbuildConfig: () => { + order.push('modifyRsbuildConfig') + }, + transform: ({ test }: { test: unknown }, handler: Transform) => { + const css = typeof test === 'string' + order.push(css ? 'transform(cssDir)' : 'transform(source)') + transforms.push({ css, handler }) + }, + } + const transform = (css: boolean) => { + const found = transforms.find((item) => item.css === css) + if (!found) throw new Error('transform not registered') + return found.handler + } + return { + api: api as unknown as Setup, + order, + rspackConfigs, + css: (context: TransformContext) => transform(true)(context), + source: (context: TransformContext) => transform(false)(context), + } +} + +/** An Rspack compiler and compilation that run the taps they are given */ +function fakeCompiler() { + const taps = new Map() + const hook = (name: string) => ({ + tap: (_: unknown, fn: Tap) => + taps.set(name, [...(taps.get(name) ?? []), fn]), + }) + const call = (name: string, ...args: unknown[]) => + (taps.get(name) ?? []).map((fn) => fn(...args)) + const compiler: FakeCompiler = { + options: { entry: {} }, + hooks: Object.fromEntries( + ['run', 'invalid', 'watchRun', 'thisCompilation'].map((name) => [ + name, + hook(name), + ]), + ), + rspack: { Compilation: { PROCESS_ASSETS_STAGE_REPORT: 5000 } }, + } + const compilation = { + hooks: Object.fromEntries( + ['finishModules', 'processAssets', 'needAdditionalPass'].map((name) => [ + name, + hook(name), + ]), + ), + assets: { 'main.js': {} }, + deleteAsset: mock(), + } + return { compiler, compilation, call } +} + +let root: string +const realMkdir = fsPromises.mkdir + +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'devup-rsbuild-hooks-')) +}) + +afterEach(() => { + rmSync(root, { recursive: true, force: true }) +}) + +function plugin(options: Parameters[0] = {}) { + return DevupUI({ + engine: 'wasm', + atomHoist: 0, + distDir: join(root, 'df'), + cssDir: join(root, 'df', 'devup-ui'), + devupFile: join(root, 'devup.json'), + ...options, + }) +} + +const web: Environment = { name: 'web', entry: {} } +const app = (color: string) => + `import { Box } from '@devup-ui/react'\nexport const App = () => ` + +async function extract( + hooks: ReturnType, + code: string, + environment = web, +) { + return hooks.source({ + code, + resourcePath: join(root, 'src', 'App.tsx'), + environment, + addDependency: () => {}, + }) +} + +/** The class name a transformed module renders */ +function className(result: unknown): string { + const code = + typeof result === 'object' && result !== null && 'code' in result + ? String(result.code) + : '' + const match = /className="([^"]+)"/.exec(code) + if (!match) throw new Error(`no className in ${code}`) + return match[1] +} + +/** The per-file stylesheets the transforms wrote, by name */ +function sheets(cssDir = join(root, 'df', 'devup-ui')) { + return readdirSync(cssDir).filter((name) => name !== 'devup-ui.css') +} + +describe('hook registration', () => { + it('registers the hooks a build runs, in the order it runs them', async () => { + const hooks = fakeApi(root) + await plugin().setup(hooks.api) + expect(hooks.order).toEqual([ + 'onCloseBuild', + 'modifyRspackConfig', + 'onBeforeBuild', + 'transform(cssDir)', + 'modifyRspackConfig', + 'modifyRsbuildConfig', + 'transform(source)', + ]) + }) + + it('registers only the build end when it does not extract CSS', async () => { + const hooks = fakeApi(root) + await plugin({ extractCss: false }).setup(hooks.api) + expect(hooks.order).toEqual(['onCloseBuild']) + }) +}) + +describe('cold directories', () => { + it('creates the CSS directory before it writes the first stylesheet into it', async () => { + // A slow mkdir: a write started next to it, not after it, fails + const mkdir = spyOn(fsPromises, 'mkdir').mockImplementation( + async (...args: Parameters) => { + await Bun.sleep(20) + return realMkdir(...args) + }, + ) + try { + const cssDir = join(root, 'cold', 'nested', 'devup-ui') + await plugin({ cssDir }).setup(fakeApi(root).api) + expect(existsSync(join(cssDir, 'devup-ui.css'))).toBe(true) + expect(mkdir).toHaveBeenCalledWith(cssDir, { recursive: true }) + } finally { + mkdir.mockRestore() + } + }) +}) + +describe('CSS revisions', () => { + it('serves and writes the rule of a same-length edit', async () => { + const hooks = fakeApi(root) + await plugin().setup(hooks.api) + const first = className(await extract(hooks, app('red'))) + const [sheet] = sheets() + const path = join(root, 'df', 'devup-ui', sheet) + const serve = () => + String( + hooks.css({ + code: '', + resourcePath: path, + environment: web, + addDependency: () => {}, + }), + ) + expect(serve()).toContain(`.${first}{color:red}`) + + // `red` and `tan` have the same length, so a size check alone misses it + const second = className(await extract(hooks, app('tan'))) + + expect(second).not.toBe(first) + expect(sheets()).toEqual([sheet]) + expect(serve()).toContain(`.${second}{color:tan}`) + expect(readFileSync(path, 'utf-8')).toBe(serve()) + }) + + it('builds once more only in the environment whose served sheet went stale', async () => { + const hooks = fakeApi(root) + await plugin().setup(hooks.api) + const node: Environment = { name: 'node', entry: {} } + const builds = [web, node].map((environment) => { + const config: { plugins?: RspackPlugin[] } = {} + hooks.rspackConfigs[1](config, { environment }) + const fake = fakeCompiler() + for (const rspackPlugin of config.plugins ?? []) + rspackPlugin.apply(fake.compiler) + fake.call('thisCompilation', fake.compilation) + return { environment, ...fake } + }) + await extract(hooks, app('red')) + const path = join(root, 'df', 'devup-ui', sheets()[0]) + hooks.css({ + code: '', + resourcePath: path, + environment: web, + addDependency: () => {}, + }) + hooks.css({ + code: '', + resourcePath: path, + environment: node, + addDependency: () => {}, + }) + // Only the web build has seen the sheet change since it served it + await extract(hooks, app('tan')) + hooks.css({ + code: '', + resourcePath: path, + environment: node, + addDependency: () => {}, + }) + + const passes = builds.map((build) => { + build.call('finishModules') + build.call('processAssets') + return build.call('needAdditionalPass')[0] + }) + + expect(passes).toEqual([true, false]) + const [webBuild, nodeBuild] = builds + expect(webBuild.compilation.deleteAsset).toHaveBeenCalledWith('main.js') + expect(webBuild.compiler.modifiedFiles).toEqual(new Set([path])) + expect(nodeBuild.compilation.deleteAsset).not.toHaveBeenCalled() + // A second pass is not asked for again in the same run + webBuild.call('finishModules') + expect(webBuild.call('needAdditionalPass')[0]).toBe(false) + expect(readFileSync(path, 'utf-8')).toContain('color:tan') + }) +}) diff --git a/packages/rsbuild-plugin/src/plugin.ts b/packages/rsbuild-plugin/src/plugin.ts index 91dae66c8..ea4d7f394 100644 --- a/packages/rsbuild-plugin/src/plugin.ts +++ b/packages/rsbuild-plugin/src/plugin.ts @@ -99,17 +99,14 @@ async function writeDataFiles( console.error(error) compiler.registerTheme({}) } - await Promise.all([ - !existsSync(options.cssDir) - ? mkdir(options.cssDir, { recursive: true }) - : Promise.resolve(), - !options.singleCss - ? writeFile( - join(options.cssDir, 'devup-ui.css'), - compiler.getCss(null, false), - ) - : Promise.resolve(), - ]) + // The directory must exist before the first write into it starts + if (!existsSync(options.cssDir)) + await mkdir(options.cssDir, { recursive: true }) + if (!options.singleCss) + await writeFile( + join(options.cssDir, 'devup-ui.css'), + compiler.getCss(null, false), + ) return tailwindFiles }