From 2820fe96c66e7dead7d51d2f2bb5e11bae13bf82 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 27 Nov 2026 13:24:04 +0800 Subject: [PATCH] fix: clean stale declarations when watch misses a deletion --- src/clean.ts | 2 +- src/index.ts | 18 +++++++++++++++++- test/clean.test.ts | 17 ++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/clean.ts b/src/clean.ts index dbb3e9e..0c4e827 100644 --- a/src/clean.ts +++ b/src/clean.ts @@ -3,7 +3,7 @@ import fs from 'node:fs/promises'; import path from 'node:path'; import { DECLARATION_HEADER } from './constants.js'; -const STYLE_REGEX = /\.(css|scss|sass|less|styl|stylus)$/i; +export const STYLE_REGEX = /\.(css|scss|sass|less|styl|stylus)$/i; const isMissing = (error: unknown) => (error as NodeJS.ErrnoException).code === 'ENOENT'; diff --git a/src/index.ts b/src/index.ts index 3f915ed..b85db7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; import type { CSSLoaderOptions, RsbuildPlugin } from '@rsbuild/core'; -import { createDeclarationCleaner } from './clean.js'; +import { createDeclarationCleaner, STYLE_REGEX } from './clean.js'; export const PLUGIN_TYPED_CSS_MODULES_NAME = 'rsbuild:typed-css-modules'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -59,6 +59,22 @@ export const pluginTypedCSSModules = ( } }, ); + let previousStyles = new Set(); + item.hooks.afterCompile.tapPromise( + PLUGIN_TYPED_CSS_MODULES_NAME, + async (compilation) => { + if (!item.watchMode) return; + // A deleted stylesheet can leave the graph without a watch removal event. + const styles = new Set(); + for (const file of compilation.fileDependencies) { + if (STYLE_REGEX.test(file)) styles.add(file); + } + await cleaner.removeDeleted( + [...previousStyles].filter((file) => !styles.has(file)), + ); + previousStyles = styles; + }, + ); } }); } diff --git a/test/clean.test.ts b/test/clean.test.ts index 7ac3b7a..43a1f99 100644 --- a/test/clean.test.ts +++ b/test/clean.test.ts @@ -61,7 +61,7 @@ for (const clean of [undefined, true]) { }); } -test('cleans declarations when a watched source is deleted', async () => { +async function testWatchCleanup(ignoreCss: boolean) { const root = await fs.promises.mkdtemp( path.join(os.tmpdir(), 'typed-css-watch-'), ); @@ -74,6 +74,9 @@ test('cleans declarations when a watched source is deleted', async () => { const rsbuild = await createRsbuild({ cwd: root, config: { + tools: { + rspack: { watchOptions: { ignored: ignoreCss ? /\.css$/ : [] } }, + }, plugins: [pluginTypedCSSModules({ clean: true })], source: { entry: { index: './index.js' } }, }, @@ -84,18 +87,22 @@ test('cleans declarations when a watched source is deleted', async () => { }); ({ close } = await rsbuild.build({ watch: true })); await expect.poll(() => builds).toBeGreaterThan(0); - const initialBuilds = builds; expect(fs.existsSync(`${source}.d.ts`)).toBe(true); await fs.promises.unlink(source); await fs.promises.writeFile(entry, 'console.log("deleted");'); - await expect.poll(() => builds).toBeGreaterThan(initialBuilds); - expect(fs.existsSync(`${source}.d.ts`)).toBe(false); + await expect.poll(() => fs.existsSync(`${source}.d.ts`)).toBe(false); } finally { await close?.(); await fs.promises.rm(root, { recursive: true, force: true }); } -}); +} + +test('cleans declarations when a watched source is deleted', () => + testWatchCleanup(false)); + +test('cleans declarations when the watcher misses a deletion', () => + testWatchCleanup(true)); test('cleans old declarations after the CSS Modules configuration changes', async () => { const root = await fs.promises.mkdtemp(