From c78156f2247f0846187d7902f1fd384279fe13dc Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:55:00 +0900 Subject: [PATCH 1/2] fix(plugins): forget the styles of source files deleted in watch mode A deleted or renamed source file is never extracted again, so its globalCss, its own bucket and its class names stayed in the shared sheet for the rest of the session (and in webpack's persisted state). The engine gains removeFile(filename), exposed on NAPI and WASM: it drops the file's global CSS and global-selector atoms, its own bucket with its keyframes and class scope when no other file collapses into it, and its bucket membership. File numbers stay, since they are handed out by count. Atoms in buckets other files share stay: nothing records which file added them. Vite calls it on a `delete` watch change and writes the changed sheets again; webpack and Rsbuild call it for the removed files of a watch run (webpack watch mode also saves the pruned state); the Next.js Turbopack coordinator forgets the files gone since whenever an edited file is extracted again. Bun builds start from a reset engine and need nothing. Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_remove_deleted_files.json | 12 +++ bindings/argument-validation.cjs | 1 + bindings/devup-ui-binding/README.md | 4 +- bindings/devup-ui-binding/adapter.cjs | 1 + bindings/devup-ui-binding/index.d.cts | 4 +- bindings/devup-ui-binding/index.mjs | 1 + bindings/devup-ui-binding/src/lib.rs | 5 ++ bindings/devup-ui-wasm/README.md | 1 + bindings/devup-ui-wasm/script.js | 6 +- bindings/devup-ui-wasm/src/lib.rs | 32 ++++++++ e2e/binding/corpus/state.mjs | 9 +++ e2e/binding/host.mjs | 6 +- e2e/binding/package/consumer.mjs | 2 +- e2e/binding/validation-cases.mjs | 5 +- libs/binding/src/lib.rs | 13 +++ libs/binding/tests/remove_file.rs | 76 ++++++++++++++++++ libs/css/src/class_map.rs | 5 ++ libs/css/src/file_map.rs | 11 +++ libs/sheet/src/lib.rs | 79 ++++++++++++++++++- .../src/__tests__/coordinator.test.ts | 76 ++++++++++++++++++ packages/next-plugin/src/coordinator.ts | 32 +++++++- .../src/__tests__/wasm-abi.test.ts | 1 + .../src/__tests__/plugin.test.ts | 22 ++++++ packages/rsbuild-plugin/src/plugin.ts | 9 +++ .../vite-plugin/src/__tests__/plugin.test.ts | 41 +++++++++- packages/vite-plugin/src/plugin.ts | 17 +++- .../src/__tests__/plugin.test.ts | 45 +++++++++++ packages/webpack-plugin/src/plugin.ts | 20 +++++ 28 files changed, 518 insertions(+), 18 deletions(-) create mode 100644 .changepacks/changepack_log_remove_deleted_files.json create mode 100644 libs/binding/tests/remove_file.rs diff --git a/.changepacks/changepack_log_remove_deleted_files.json b/.changepacks/changepack_log_remove_deleted_files.json new file mode 100644 index 000000000..ac7b72005 --- /dev/null +++ b/.changepacks/changepack_log_remove_deleted_files.json @@ -0,0 +1,12 @@ +{ + "changes": { + "bindings/devup-ui-binding/package.json": "Patch", + "bindings/devup-ui-wasm/package.json": "Patch", + "packages/vite-plugin/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch", + "packages/rsbuild-plugin/package.json": "Patch", + "packages/next-plugin/package.json": "Patch" + }, + "note": "A source file deleted or renamed in watch mode or the dev server no longer leaves its styles behind: its `globalCss`, its own stylesheet and the class names handed out in it are dropped, and it leaves the bucket it collapsed into. The engine gains `removeFile(filename)` (native and WASM), which Vite calls on a `delete` watch change, webpack and Rsbuild on the removed files of a watch run (webpack watch mode also saves the pruned state), and the Next.js Turbopack coordinator when an edited file is extracted again. Atoms in a stylesheet other files share stay, as nothing records which file added them; they style nothing once no module uses them", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/bindings/argument-validation.cjs b/bindings/argument-validation.cjs index 66e41fb08..a0e3edb41 100644 --- a/bindings/argument-validation.cjs +++ b/bindings/argument-validation.cjs @@ -16,6 +16,7 @@ module.exports = function createBoundary() { importSheet: 'sheet_object:sheet', registerShorthands: 'shorthands:shorthands', registerTheme: 'theme_object:theme', + removeFile: 'filename:s', reserveClassSlots: 'rows:rows _package:s single_css:b import_main_css_in_code:b _import_main_css_in_css:b import_aliases:aliases', seedFileMap: 'files:a', diff --git a/bindings/devup-ui-binding/README.md b/bindings/devup-ui-binding/README.md index 993025949..71afa1212 100644 --- a/bindings/devup-ui-binding/README.md +++ b/bindings/devup-ui-binding/README.md @@ -2,7 +2,7 @@ Synchronous native build-time compiler for Node.js and Bun. Both `require('@devup-ui/binding')` and named ESM imports expose exactly the existing -29 compiler names, with identical function/class identities and no default init. +30 compiler names, with identical function/class identities and no default init. Authored CommonJS/ESM declarations are included; no WASM dependency is required. The private loader selects one optional platform sibling: Windows MSVC, macOS, @@ -96,4 +96,4 @@ The decoder is private and target-bounded: unused Serde hints use trait forwardi Malformed-enum error paths follow shipped serde-wasm-bindgen 0.6.5: Object.entries result length is read and coerced once for the check, then read and coerced again for the invalid-length diagnostic when the first length is not one, before any index read. The second value supplies the diagnostic; errors thrown by its getter or coercion propagate unchanged. Valid enums retain one length read. -The shared native/WASM boundary validates ordinary calls at 45 argument positions in 18 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 267-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. +The shared native/WASM boundary validates ordinary calls at 46 argument positions in 19 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 272-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. diff --git a/bindings/devup-ui-binding/adapter.cjs b/bindings/devup-ui-binding/adapter.cjs index 77bf341e9..a34c93cac 100644 --- a/bindings/devup-ui-binding/adapter.cjs +++ b/bindings/devup-ui-binding/adapter.cjs @@ -102,6 +102,7 @@ exports.createBinding = function createBinding(raw) { isDebug: raw.isDebug, registerShorthands: (value) => call(raw.registerShorthands, value), registerTheme: (value) => call(raw.registerTheme, value), + removeFile: (filename) => raw.removeFile(filename), reserveClassSlots: (rows, pkg, single, code, css, aliases) => call(raw.reserve, [rows, pkg, single, code, css, aliases]), resetBuildState: () => raw.reset(false), diff --git a/bindings/devup-ui-binding/index.d.cts b/bindings/devup-ui-binding/index.d.cts index cd21d7210..eae4ba96a 100644 --- a/bindings/devup-ui-binding/index.d.cts +++ b/bindings/devup-ui-binding/index.d.cts @@ -11,7 +11,7 @@ export class Output { readonly updatedBaseStyle: boolean } -// Positional signatures intentionally match the existing 29-name compiler ABI. +// Positional signatures intentionally match the existing 30-name compiler ABI. export function codeExtract( filename: string, code: string, @@ -65,6 +65,8 @@ export function importSheet(value: unknown): void export function isDebug(): boolean export function registerShorthands(value: unknown): void export function registerTheme(value: unknown): void +/** Forget a deleted source file's own styles; whether the sheet changed. */ +export function removeFile(filename: string): boolean export function reserveClassSlots( rows: unknown, packageName: string, diff --git a/bindings/devup-ui-binding/index.mjs b/bindings/devup-ui-binding/index.mjs index c9f981b23..015b58724 100644 --- a/bindings/devup-ui-binding/index.mjs +++ b/bindings/devup-ui-binding/index.mjs @@ -22,6 +22,7 @@ export const { isDebug, registerShorthands, registerTheme, + removeFile, reserveClassSlots, resetBuildState, resetStateForTesting, diff --git a/bindings/devup-ui-binding/src/lib.rs b/bindings/devup-ui-binding/src/lib.rs index 205575d4c..7211b6d16 100644 --- a/bindings/devup-ui-binding/src/lib.rs +++ b/bindings/devup-ui-binding/src/lib.rs @@ -215,6 +215,11 @@ pub fn set_atom_hoist(value: Option) { pub fn get_css(value: Option, import_main: bool) -> String { core::get_css(value.map(widen_index), import_main) } +/// Forget a deleted source file, returning whether the sheet changed. +#[napi] +pub fn remove_file(filename: String) -> bool { + core::remove_file_internal(&filename) +} /// Seed the array-like wrapper's copied strings, not arbitrary JS iterables. #[napi] pub fn seed_file_map(files: Vec) { diff --git a/bindings/devup-ui-wasm/README.md b/bindings/devup-ui-wasm/README.md index 1032dec5d..066708bcf 100644 --- a/bindings/devup-ui-wasm/README.md +++ b/bindings/devup-ui-wasm/README.md @@ -29,6 +29,7 @@ Everything is exported from `pkg/index.js` (types in `pkg/index.d.ts`). | `codeExtractWithoutSourceMap(...)` | The same without the source map. | | `hasDevupUI(filename, code, package)` | Whether the file imports Devup UI, so a plugin can skip the rest. | | `getCss(fileNum, importMainCss)` | The stylesheet collected so far: the whole sheet for `null`, one file's for its number. | +| `removeFile(filename)` | Forgets a deleted source file: its global CSS, its own stylesheet and the class names handed out in it. Returns whether the sheet changed. | | `registerTheme(theme)`, `registerShorthands(shorthands)` | Registers the `devup.json` theme and the custom shorthand props before extracting. | | `getThemeInterface(package, color, typography, length, shadows, theme)` | The `.d.ts` text that types the registered theme. | | `getDefaultTheme()` | The theme variant that applies without `data-theme`. | diff --git a/bindings/devup-ui-wasm/script.js b/bindings/devup-ui-wasm/script.js index 279a62ba6..e25c5da00 100644 --- a/bindings/devup-ui-wasm/script.js +++ b/bindings/devup-ui-wasm/script.js @@ -39,9 +39,9 @@ for (const operation of ['begin', 'end', 'error', 'fail']) ) assert.equal([...source.matchAll(/__devupBoundary/g)].length, 4) const names = [...source.matchAll(/^exports\.(\w+) = \1;/gm)] -assert.equal(names.length, 29) -assert.equal(new Set(names.map((match) => match[1])).size, 29) -assert.equal([...source.matchAll(/^exports\./gm)].length, 29) +assert.equal(names.length, 30) +assert.equal(new Set(names.map((match) => match[1])).size, 30) +assert.equal([...source.matchAll(/^exports\./gm)].length, 30) assert.equal(source.includes('module.exports'), false) for (const name of ['codeExtract', 'codeExtractWithoutSourceMap']) { const signatures = [ diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index 6c2e7ed92..6874bc288 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -295,6 +295,16 @@ pub fn seed_file_map(files: Vec) { binding_core::seed_file_map(&files); } +/// Forget a source file that was deleted or renamed away. +/// +/// Its global CSS, its own bucket and the class names handed out in it go, +/// and it no longer collapses into another bucket. Returns whether the sheet +/// changed, so the caller knows to serve its CSS again. +#[wasm_bindgen(js_name = "removeFile")] +pub fn remove_file(filename: &str) -> bool { + binding_core::remove_file_internal(filename) +} + /// Forget everything one build left in the engine. /// /// That is names, numbers, styles, buckets, routes, the prefix, atom hoisting @@ -1719,6 +1729,28 @@ mod tests { assert!(json_str.starts_with('{') || json_str.starts_with("[]")); } + #[test] + #[serial] + fn remove_file_reports_whether_the_sheet_changed() { + let _state = TestStateGuard::new(); + // Given: a file with its own bucket. + let output = code_extract_internal( + "removed.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + "@devup-ui/react", + "df".to_string(), + false, + false, + false, + HashMap::new(), + ) + .unwrap(); + assert!(output.css.is_some()); + // When / Then: the first removal drops it, a second finds nothing. + assert!(remove_file("removed.tsx")); + assert!(!remove_file("removed.tsx")); + } + #[test] #[serial] fn test_exported_maps_are_canonical_json() { diff --git a/e2e/binding/corpus/state.mjs b/e2e/binding/corpus/state.mjs index 04e6ff084..85f5e8b64 100644 --- a/e2e/binding/corpus/state.mjs +++ b/e2e/binding/corpus/state.mjs @@ -42,6 +42,15 @@ add('reservation-malformed-second-row-arity-nonmutation', (h) => { h.call('reserveClassSlots', value, pkg, false, false, false, {}) h.call('reserveClassSlots', rows, pkg, false, false, false, { bad: 7 }) }) +add('remove-file-drops-its-own-styles', (h) => { + h.call('importCanonicalMap', { 'src/child.tsx': 'src/root.tsx' }) + h.extract(source, { single: false }) + h.extract(box('color="blue"'), { filename: 'src/root.tsx', single: false }) + h.extract(box('color="teal"'), { filename: 'src/child.tsx', single: false }) + for (const filename of ['src/root.tsx', file, file, 'src/child.tsx']) + h.call('removeFile', filename) + h.call('removeFile', 'src/root.tsx') +}) add('four-raw-states-explicit-import-roundtrip', (h) => { h.call('registerTheme', theme) h.call('importCanonicalMap', { [file]: 'src/root.tsx' }) diff --git a/e2e/binding/host.mjs b/e2e/binding/host.mjs index 12046f85a..a86141ce5 100644 --- a/e2e/binding/host.mjs +++ b/e2e/binding/host.mjs @@ -68,7 +68,7 @@ save('provenance.json', { scenarios: corpus.map((c) => c.name), }) const names = - 'Output codeExtract codeExtractWithoutSourceMap exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme reserveClassSlots resetBuildState resetStateForTesting seedFileMap setAtomHoist setDebug setModuleResolver setPrefix'.split( + 'Output codeExtract codeExtractWithoutSourceMap exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme removeFile reserveClassSlots resetBuildState resetStateForTesting seedFileMap setAtomHoist setDebug setModuleResolver setPrefix'.split( ' ', ) const abi = { @@ -199,8 +199,8 @@ const h = { ) }, } -assert.equal(corpus.length, 25) -assert.equal(new Set(corpus.map((c) => c.name)).size, 25) +assert.equal(corpus.length, 26) +assert.equal(new Set(corpus.map((c) => c.name)).size, 26) h.note('public-error-inventory', runDiagnostics(w)) for (const test of corpus) { scenario = test.name diff --git a/e2e/binding/package/consumer.mjs b/e2e/binding/package/consumer.mjs index 076623429..3940b9cca 100644 --- a/e2e/binding/package/consumer.mjs +++ b/e2e/binding/package/consumer.mjs @@ -136,7 +136,7 @@ export async function consume(options) { ] .map((match) => match[1]) .sort() - assert.equal(names.length, 29) + assert.equal(names.length, 30) for (const manager of ['npm', 'bun']) { for (const omit of [false, true]) { const startRequest = server.requests.length diff --git a/e2e/binding/validation-cases.mjs b/e2e/binding/validation-cases.mjs index c90b0fda0..f69d04387 100644 --- a/e2e/binding/validation-cases.mjs +++ b/e2e/binding/validation-cases.mjs @@ -38,6 +38,7 @@ export const positions = [ ['importSheet', 'sheet_object:sheet', () => [{}]], ['registerShorthands', 'shorthands:shorthands', () => [{}]], ['registerTheme', 'theme_object:theme', () => [{}]], + ['removeFile', 'filename:s', () => ['src/new.tsx']], [ 'reserveClassSlots', 'rows:rows _package:s single_css:b import_main_css_in_code:b _import_main_css_in_css:b import_aliases:aliases', @@ -153,6 +154,6 @@ export const cases = positions.flatMap((position) => .map((good, index) => (index === position.index ? value : good)), })), ) -assert.equal(positions.length, 45) -assert.equal(new Set(positions.map((p) => p.fn)).size, 18) +assert.equal(positions.length, 46) +assert.equal(new Set(positions.map((p) => p.fn)).size, 19) assert.equal(new Set(cases.map((c) => c.id)).size, cases.length) diff --git a/libs/binding/src/lib.rs b/libs/binding/src/lib.rs index cc89b9a04..0eb40a9f9 100644 --- a/libs/binding/src/lib.rs +++ b/libs/binding/src/lib.rs @@ -112,6 +112,19 @@ pub fn seed_file_map(files: &[String]) { css::file_map::seed_file_numbers(files); } +/// Forget a deleted source file: its global CSS, its own bucket with the class +/// names handed out in it, and the bucket it collapsed into. +/// +/// File numbers stay, as they are handed out by count and a freed one would be +/// handed out twice. Returns whether the sheet changed. +pub fn remove_file_internal(filename: &str) -> bool { + let (bucket, alone) = css::file_map::remove_bucket_membership(filename); + if alone { + css::class_map::remove_class_scope(&bucket); + } + with_style_sheet_mut(|sheet| sheet.rm_file(filename, &bucket, alone)) +} + /// Clear production build state, retaining theme registries, shorthands and debug. /// The host adapter separately clears its resolver handle. pub fn reset_build_state_internal() { diff --git a/libs/binding/tests/remove_file.rs b/libs/binding/tests/remove_file.rs new file mode 100644 index 000000000..f028b212a --- /dev/null +++ b/libs/binding/tests/remove_file.rs @@ -0,0 +1,76 @@ +use devup_ui_binding_core::{ + ExtractionMode, SourceMapMode, code_extract, export_canonical_map_internal, + export_class_map_internal, get_css, import_canonical_map_internal, remove_file_internal, + reset_state_internal, +}; +use extractor::ExtractOption; + +fn extract(filename: &str, code: &str) -> Result<(), String> { + code_extract( + (filename, code), + ExtractOption { + package: "@devup-ui/react".to_string(), + css_dir: "df".to_string(), + ..ExtractOption::default() + }, + ExtractionMode { + source_map: SourceMapMode::Skip, + resolver: None, + resolver_check: None, + import_main_css_in_css: false, + }, + ) + .map(drop) +} + +fn file_num(filename: &str) -> usize { + css::file_map::get_file_num_by_filename(filename) +} + +#[test] +fn removed_files_leave_no_styles_of_their_own() -> Result<(), String> { + // Given: a file with its own bucket and global CSS, beside another file. + reset_state_internal(|| {}); + extract( + "a.tsx", + "import { Box, globalCss } from '@devup-ui/react'; globalCss({ body: { margin: 0 } }); export const view = ;", + )?; + extract( + "b.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + )?; + assert!(get_css(None, false).contains("margin:0")); + assert!(get_css(Some(file_num("a.tsx")), false).contains("red")); + + // When: the first file is deleted. + assert!(remove_file_internal("a.tsx")); + + // Then: its global CSS, bucket and class names are gone; the other file stays. + assert!(!get_css(None, false).contains("margin:0")); + assert!(!get_css(Some(file_num("a.tsx")), false).contains("red")); + assert!(get_css(Some(file_num("b.tsx")), false).contains("blue")); + assert!(!export_class_map_internal()?.contains("a.tsx")); + assert!(!remove_file_internal("a.tsx")); + + // Given: a file collapsed into the remaining file's bucket. + import_canonical_map_internal([("c.tsx".to_string(), "b.tsx".to_string())].into()); + extract( + "c.tsx", + "import { Box } from '@devup-ui/react'; export const view = ;", + )?; + + // When: the bucket's root goes first, the collapsed file still shares it. + assert!(!remove_file_internal("b.tsx")); + assert!(get_css(Some(file_num("b.tsx")), false).contains("green")); + + // When: the collapsed file goes too, it leaves the bucket. + assert!(!remove_file_internal("c.tsx")); + assert_eq!(export_canonical_map_internal()?, "{}"); + + // Then: the bucket belongs to no other file and goes with its root. + assert!(remove_file_internal("b.tsx")); + assert!(!get_css(Some(file_num("b.tsx")), false).contains("green")); + // Names in the shared scope stay: other files may hand out the same ones. + assert!(!export_class_map_internal()?.contains("b.tsx")); + Ok(()) +} diff --git a/libs/css/src/class_map.rs b/libs/css/src/class_map.rs index 62f265773..3ce5f26e7 100644 --- a/libs/css/src/class_map.rs +++ b/libs/css/src/class_map.rs @@ -69,6 +69,11 @@ pub fn get_class_map() -> HashMap> { with_class_map(Clone::clone) } +/// Forget the class names handed out in one bucket. +pub fn remove_class_scope(scope: &str) { + with_class_map_mut(|map| map.remove(scope)); +} + #[cfg(test)] mod tests { use serial_test::serial; diff --git a/libs/css/src/file_map.rs b/libs/css/src/file_map.rs index 24faf84a0..0c12d8723 100644 --- a/libs/css/src/file_map.rs +++ b/libs/css/src/file_map.rs @@ -182,6 +182,17 @@ pub fn get_canonical_map() -> std::collections::HashMap { with_canonical_map(Clone::clone) } +/// Forget the bucket `filename` collapses into, returning that bucket and +/// whether it is the file's own one with no other file collapsed into it. +#[must_use] +pub fn remove_bucket_membership(filename: &str) -> (String, bool) { + with_canonical_map_mut(|map| { + let bucket = map.remove(filename).unwrap_or_else(|| filename.to_string()); + let alone = bucket == filename && !map.values().any(|root| root == filename); + (bucket, alone) + }) +} + /// Resolve a filename to its bucket-root via `CANONICAL_MAP`, or identity when absent. #[must_use] pub fn canonical(filename: &str) -> String { diff --git a/libs/sheet/src/lib.rs b/libs/sheet/src/lib.rs index b66cde22f..38c4695c4 100644 --- a/libs/sheet/src/lib.rs +++ b/libs/sheet/src/lib.rs @@ -557,8 +557,13 @@ impl StyleSheet { } else { canonical(file) }; + self.rm_global_atoms(file, &property_key); + true + } - let bucket_empty = if let Some(prop_map) = self.properties.get_mut(&property_key) { + /// Drop the global-selector atoms `file` wrote into the `property_key` bucket. + fn rm_global_atoms(&mut self, file: &str, property_key: &str) { + let bucket_empty = if let Some(prop_map) = self.properties.get_mut(property_key) { for map in prop_map.values_mut() { for props in map.values_mut() { props.retain(|prop| match prop.selector.as_ref() { @@ -579,9 +584,29 @@ impl StyleSheet { true }; if bucket_empty { - self.properties.remove(&property_key); + self.properties.remove(property_key); } - true + } + + /// Forget a deleted source file: everything it wrote globally and, when + /// `drop_bucket` says no other file shares it, its whole `bucket`. + /// + /// Atoms other files share (the single-CSS sheet, a bucket other files + /// collapse into) stay: nothing records which file added them, and a class + /// no module references any more styles nothing. Returns whether the sheet + /// changed. + pub fn rm_file(&mut self, file: &str, bucket: &str, drop_bucket: bool) -> bool { + // Global CSS of a single-CSS or global file sits in the shared bucket, + // otherwise in the file's own one. + let mut removed = self.rm_global_css(file, true); + if removed { + self.rm_global_atoms(file, bucket); + } + if drop_bucket { + removed |= self.properties.remove(bucket).is_some(); + removed |= self.keyframes.remove(bucket).is_some(); + } + removed } pub fn set_theme(&mut self, theme: Theme) { @@ -1831,6 +1856,54 @@ mod tests { ); } + #[test] + #[serial] + fn rm_file_drops_what_only_the_file_wrote() { + let _state = css::test_state::TestStateGuard::new(); + let global = |file: &str| Some(StyleSelector::Global("body".to_string(), file.to_string())); + let mut sheet = StyleSheet::default(); + // Given: a file's own bucket, keyframes and global CSS in both buckets. + sheet.add_property("a", "color", 0, "red", None, None, Some("a.tsx")); + sheet.add_property( + "b", + "margin", + 0, + "0", + global("a.tsx").as_ref(), + Some(0), + None, + ); + sheet.add_property( + "c", + "padding", + 0, + "0", + global("a.tsx").as_ref(), + Some(0), + Some("root.tsx"), + ); + sheet.add_property("d", "color", 0, "blue", None, None, Some("root.tsx")); + sheet.add_keyframes("spin", BTreeMap::new(), Some("a.tsx")); + sheet.add_css("a.tsx", "html { gap: 1px }"); + + // When / Then: a file collapsed into `root.tsx` takes only its global CSS. + assert!(sheet.rm_file("a.tsx", "root.tsx", false)); + let css = sheet.create_css(None, false) + &sheet.create_css(Some("root.tsx"), false); + assert!( + !css.contains("margin:0") && !css.contains("padding:0") && !css.contains("gap: 1px"), + "{css}" + ); + assert!(css.contains("color:blue"), "{css}"); + assert!(sheet.properties.contains_key("a.tsx")); + + // When / Then: the file's own bucket goes when no other file shares it. + assert!(sheet.rm_file("a.tsx", "a.tsx", true)); + assert!(!sheet.properties.contains_key("a.tsx")); + assert!(!sheet.keyframes.contains_key("a.tsx")); + assert!(!sheet.rm_file("a.tsx", "a.tsx", true)); + assert!(!sheet.rm_file("a.tsx", "a.tsx", false)); + } + // Under single-importer collapse, a collapsed file's globalCss atoms are // bucketed by canonical(file). rm_global_css(raw) must therefore clear them // from the CANONICAL bucket (matching the raw owner via f == file), and must diff --git a/packages/next-plugin/src/__tests__/coordinator.test.ts b/packages/next-plugin/src/__tests__/coordinator.test.ts index 7d346a6ba..125ea63b9 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -1650,3 +1650,79 @@ describe('coordinator per-bucket completion', () => { coordinator.close() }) }) + +describe('coordinator deleted sources', () => { + it('forgets the files deleted since when a file is extracted again', async () => { + // Given: three extracted files, two of which are deleted later + using removeFile = spyOn(wasm, 'removeFile').mockImplementation( + (filename) => filename === 'src/Styled.tsx', + ) + const deleted = new Set() + using existsSync = spyOn(fs, 'existsSync').mockImplementation( + (path) => !deleted.has(String(path)), + ) + codeExtractSpy.mockImplementation(() => ({ + code: 'code', + map: undefined, + cssFile: undefined, + updatedBaseStyle: false, + free: mock(), + [Symbol.dispose]: mock(), + })) + getCssSpy.mockReturnValue('base') + exportSheetSpy.mockReturnValue('sheet') + exportClassMapSpy.mockReturnValue('classes') + exportFileMapSpy.mockReturnValue('files') + const coordinator = startCoordinator(makeOptions()) + await new Promise((r) => setTimeout(r, 100)) + const port = parseInt( + (writeFileSyncSpy.mock.calls[0] as [string, string])[1], + ) + const extract = (filename: string) => + httpRequest( + port, + 'POST', + '/extract', + JSON.stringify({ + filename, + code: 'c', + resourcePath: join(tmpDir, filename), + }), + ) + for (const filename of ['src/Plain.tsx', 'src/Styled.tsx', 'src/App.tsx']) + await extract(filename) + expect(existsSync).not.toHaveBeenCalled() + + // When: an edit of a file that stays, after both were deleted + deleted.add(join(tmpDir, 'src/Plain.tsx')) + deleted.add(join(tmpDir, 'src/Styled.tsx')) + writeFileSpy.mockClear() + await extract('src/App.tsx') + await flushCoordinatorWrites() + + // Then: the engine forgets both, and the sheets are written again + expect(removeFile.mock.calls).toEqual([ + ['src/Plain.tsx'], + ['src/Styled.tsx'], + ]) + const written = new Map( + writeFileSpy.mock.calls.map(([path, data]) => [String(path), data]), + ) + expect(written.get(join(tmpDir, 'sheet.json'))).toBe('sheet') + expect(written.get(join(tmpDir, 'classMap.json'))).toBe('classes') + expect(written.get(join(tmpDir, 'fileMap.json'))).toBe('files') + expect( + String(written.get(join(tmpDir, 'css', 'devup-ui.css'))), + ).toStartWith('base\n/* ') + + // When: another edit finds nothing deleted since, or nothing to forget + writeFileSpy.mockClear() + await extract('src/App.tsx') + deleted.add(join(tmpDir, 'src/App.tsx')) + await extract('src/App.tsx') + await flushCoordinatorWrites() + expect(removeFile.mock.calls.length).toBe(3) + expect(writeFileSpy).not.toHaveBeenCalled() + coordinator.close() + }) +}) diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 892f8287d..22266f499 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -1,4 +1,4 @@ -import { writeFile, writeFileSync } from 'node:fs' +import { existsSync, writeFile, writeFileSync } from 'node:fs' import { createServer, type IncomingMessage, type Server } from 'node:http' import { basename, dirname, join, relative } from 'node:path' @@ -274,6 +274,8 @@ function waitForBase(): Promise { // threshold (flaky CI rendering). Instead we wait for the bucket's KNOWN // members (from the canonical map) — no guessing, no extra extraction. const extractedFiles = new Set() +// Where each file the loaders posted lives, to notice the ones deleted since +const extractedSources = new Map() const fileNumToBucket = new Map() let bucketToMembers = new Map>() let canonicalMapRef: Record = {} @@ -376,6 +378,7 @@ export function startCoordinator(options: CoordinatorOptions): { exportFileMap, exportSheet, getCss, + removeFile, } = wasm const prewarmedOutputs = options.prewarmedOutputs ?? new Map() const extract = @@ -389,8 +392,31 @@ export function startCoordinator(options: CoordinatorOptions): { expectedBaseFiles = new Set(options.expectedBaseFiles ?? []) extractedFiles.clear() for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) + extractedSources.clear() fileNumToBucket.clear() + // A deleted file is never extracted again, so its styles would stay in the + // sheet; its importers are, once they stop importing it. A repeated + // extraction (an edit, never the first build) forgets the files gone since. + const removeDeletedSources = async () => { + let removed = false + for (const [filename, resourcePath] of extractedSources) { + if (existsSync(resourcePath)) continue + extractedSources.delete(filename) + if (removeFile(filename)) removed = true + } + if (!removed) return + await Promise.all([ + safeWrite( + join(cssDir, 'devup-ui.css'), + `${getCss(null, false)}\n/* ${Date.now()} */`, + ), + safeWrite(sheetFile, exportSheet()), + safeWrite(classMapFile, exportClassMap()), + safeWrite(fileMapFile, exportFileMap()), + ]) + } + const coordinatorServer = createServer(async (req, res) => { const url = new URL(req.url ?? '/', `http://${req.headers.host}`) @@ -453,6 +479,8 @@ export function startCoordinator(options: CoordinatorOptions): { resourcePath: string } extractedFilename = filename + const repeated = extractedSources.has(filename) + extractedSources.set(filename, resourcePath) let relCssDir = relative(dirname(resourcePath), cssDir).replaceAll( '\\', @@ -506,6 +534,7 @@ export function startCoordinator(options: CoordinatorOptions): { let sheetSnapshotBytes: number | undefined let sheetSnapshotMs: number | undefined const promises: Promise[] = [] + if (repeated) promises.push(removeDeletedSources()) if (!cacheHit && result.updatedBaseStyle) { const cssStartedAt = @@ -704,6 +733,7 @@ export const resetCoordinator = () => { quietMs = 10_000 maxWaitMs = 60_000 extractedFiles.clear() + extractedSources.clear() fileNumToBucket.clear() bucketToMembers = new Map() canonicalMapRef = {} diff --git a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts index 2edae3e78..45390ebfc 100644 --- a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts +++ b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts @@ -62,6 +62,7 @@ describe('@devup-ui/wasm exports', () => { 'isDebug', 'registerShorthands', 'registerTheme', + 'removeFile', 'reserveClassSlots', 'resetBuildState', 'resetStateForTesting', diff --git a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts index 15afbd3ad..9dc00a1d0 100644 --- a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts +++ b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts @@ -1211,6 +1211,28 @@ const App = () => `, ) }) + it.each([ + [2, resolve('src', 'Gone.tsx').replaceAll('\\', '/')], + [0, resolve('src', 'Gone.tsx')], + ])( + 'forgets deleted sources at watchRun with atomHoist=%s', + async (atomHoist, name) => { + // Given: a watch revision that deleted a source + using removeFile = spyOn(wasm, 'removeFile').mockReturnValue(true) + const { compiler, taps } = await setup({ atomHoist }) + assert(taps.watchRun) + taps.watchRun() + expect(removeFile).not.toHaveBeenCalled() + Object.assign(compiler, { + removedFiles: new Set([resolve('src', 'Gone.tsx')]), + }) + // When + taps.watchRun() + // Then: the engine forgets it under the name the transform used + expect(removeFile.mock.calls).toEqual([[name]]) + }, + ) + it('reserves public compiler revisions without adding watch prewarm', async () => { const reserve = spyOn(wasm, 'reserveClassSlots').mockReturnValue( undefined, diff --git a/packages/rsbuild-plugin/src/plugin.ts b/packages/rsbuild-plugin/src/plugin.ts index 91dae66c8..d7207e76b 100644 --- a/packages/rsbuild-plugin/src/plugin.ts +++ b/packages/rsbuild-plugin/src/plugin.ts @@ -326,6 +326,12 @@ export const DevupUI = ({ }) const servedCss = new Map>() + // Forget deleted source files, under the names the transform extracts + // them under + const removeFiles = (files: ReadonlySet | undefined) => { + for (const file of files ?? []) + compiler.removeFile(atomMode ? file.replaceAll('\\', '/') : file) + } const stylesheet = (resourcePath: string) => // A file's stylesheet imports the shared base, except in atom mode, // where the entry code imports the base itself so that hoisted atoms @@ -365,6 +371,9 @@ export const DevupUI = ({ compiler.hooks.invalid.tap(PLUGIN_NAME, reservation.invalidate) compiler.hooks.watchRun.tap(PLUGIN_NAME, () => { passes = 0 + // A deleted source file is never transformed again; the + // stylesheets it was in are checked against the sheet below + removeFiles(compiler.removedFiles) reservation.reserve() }) compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { diff --git a/packages/vite-plugin/src/__tests__/plugin.test.ts b/packages/vite-plugin/src/__tests__/plugin.test.ts index cbc3643da..7c4e6fcb6 100644 --- a/packages/vite-plugin/src/__tests__/plugin.test.ts +++ b/packages/vite-plugin/src/__tests__/plugin.test.ts @@ -133,7 +133,7 @@ interface ViteTestPlugin { root?: string plugins?: readonly object[] }) => Promise - watchChange: (id: string) => Promise + watchChange: (id: string, change?: { event: string }) => Promise hotUpdate: ( this: { environment: HotUpdateEnvironment }, options: { file: string; modules: object[]; timestamp: number }, @@ -1504,6 +1504,45 @@ describe('devupUIVitePlugin', () => { await plugin.watchChange('wrong') }) + it('forgets a deleted source and writes the sheets it changed', async () => { + // Given: a transformed source whose bucket sheet was written + const plugin = createPlugin({}) + await plugin.configResolved({ command: 'serve' }) + codeExtractSpy.mockReturnValue( + createCodeExtractResult({ cssFile: 'devup-ui-1.css', css: 'old' }), + ) + await plugin.transform('code', 'removed.tsx') + using removeFileSpy = spyOn(wasm, 'removeFile') + .mockReturnValueOnce(false) + .mockReturnValueOnce(true) + getCssSpy.mockImplementation((fileNum: number | null) => + fileNum === null ? 'base' : 'bucket', + ) + // When: an edit is no removal, and a removal that changed nothing writes nothing + await plugin.watchChange('removed.tsx', { event: 'update' }) + await plugin.watchChange('removed.tsx', { event: 'delete' }) + expect(writeFileSpy).not.toHaveBeenCalledWith( + expect.stringContaining('devup-ui-1.css'), + 'bucket', + 'utf-8', + ) + // When: the engine dropped styles of the deleted source + await plugin.watchChange('removed.tsx?v=1', { event: 'delete' }) + // Then: the base and the bucket sheet are written again + const cssDir = resolve('df', 'devup-ui') + expect(removeFileSpy.mock.calls).toEqual([['removed.tsx'], ['removed.tsx']]) + expect(writeFileSpy).toHaveBeenCalledWith( + join(cssDir, 'devup-ui-1.css'), + 'bucket', + 'utf-8', + ) + expect(writeFileSpy).toHaveBeenCalledWith( + join(cssDir, 'devup-ui.css'), + 'base', + 'utf-8', + ) + }) + it('should invalidate and reload on devup hot update', async () => { writeFileSpy.mockResolvedValueOnce(undefined) getThemeInterfaceSpy.mockReturnValue('interface code') diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index f9ce736fc..bc00de942 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -485,6 +485,20 @@ export function DevupUI({ sheetRevisions.set(fileName, (sheetRevisions.get(fileName) ?? 0) + 1) return stateWriter.write(join(cssDir, fileName), css, 'utf-8') } + // A deleted source file is never transformed again, so its styles stay in + // the sheet until the engine forgets the file. Every sheet written so far + // is written again; only the ones that changed reach the disk. + async function removeSource(id: string) { + if (!compiler.removeFile(id.split('?')[0])) return + await Promise.all( + [...new Set(['devup-ui.css', ...writtenCss.keys()])].map((fileName) => + writeCssFile( + fileName, + compiler.getCss(getFileNumByFilename(fileName), false), + ), + ), + ) + } const plugin = { name: 'devup-ui', // The compiler sheet and transform state are intentionally shared. Vite @@ -612,8 +626,9 @@ export function DevupUI({ drained ??= Promise.withResolvers() return drained.promise }, - async watchChange(id) { + async watchChange(id, change) { reservation.invalidate(id) + if (change?.event === 'delete') await own(removeSource)(id) if (isConfigFile(id) || (isTailwindFile(id) && existsSync(id))) { try { await own(refreshConfig)() diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index f22c489e3..3e7c8d3ba 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -433,6 +433,51 @@ describe('devupUIWebpackPlugin', () => { }, ) + it.each([false, true])( + 'forgets deleted sources at watchRun with watch=%s', + async (watch) => { + // Given: a compilation that deleted two sources, one with styles left + const compiler = createCompiler() + new DevupUIWebpackPlugin({ engine: 'wasm', watch }).apply( + asCompiler(compiler), + ) + using removeFile = spyOn(wasm, 'removeFile').mockImplementation( + (file) => file === 'src/styled.tsx', + ) + using exportSheet = spyOn(wasm, 'exportSheet').mockReturnValue('sheet') + const watchRun = compiler.hooks.watchRun.tapPromise.mock.calls[0][1] + // When: nothing was removed, then two files were + await watchRun() + Object.assign(compiler, { + removedFiles: new Set([ + resolve('src', 'plain.tsx'), + resolve('src', 'styled.tsx'), + ]), + }) + writeFileSyncSpy.mockClear() + await watchRun() + // Then: each is forgotten under its cwd-relative name, and only watch + // persistence writes the pruned state + expect(removeFile.mock.calls).toEqual([ + ['src/plain.tsx'], + ['src/styled.tsx'], + ]) + expect( + writeFileSyncSpy.mock.calls.some(([, content]) => content === 'sheet'), + ).toBe(watch) + expect(exportSheet).toHaveBeenCalledTimes(watch ? 1 : 0) + }, + ) + + it('keeps the persisted state when no deleted source had styles', () => { + const plugin = new DevupUIWebpackPlugin({ engine: 'wasm', watch: true }) + using removeFile = spyOn(wasm, 'removeFile').mockReturnValue(false) + using exportSheet = spyOn(wasm, 'exportSheet').mockReturnValue('sheet') + plugin.removeFiles(new Set([resolve('src', 'plain.tsx')])) + expect(removeFile).toHaveBeenCalledTimes(1) + expect(exportSheet).not.toHaveBeenCalled() + }) + it('also reserves at native watchRun when watch persistence is disabled', async () => { const manager = { invalidate: mock(), diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 2c5bd2ed0..f1318f5b8 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -228,6 +228,24 @@ export class DevupUIWebpackPlugin { } } + /** + * Forget the source files deleted since the last compilation: they are never + * loaded again, so their styles would otherwise stay in the sheet. The + * stylesheet modules are built from the sheet on every compilation. + */ + removeFiles(files: ReadonlySet | undefined) { + const cwd = process.cwd() + let removed = false + for (const file of files ?? []) + if (this.compiler.removeFile(relative(cwd, file).replaceAll('\\', '/'))) + removed = true + if (!removed || !this.options.watch) return + // The next start restores the sheet without the deleted files + writeFileSync(this.sheetFile, this.compiler.exportSheet()) + writeFileSync(this.classMapFile, this.compiler.exportClassMap()) + writeFileSync(this.fileMapFile, this.compiler.exportFileMap()) + } + apply(compiler: Compiler) { // A build starts from its own options, not from what an earlier build in // this process left in the engine @@ -396,6 +414,7 @@ export class DevupUIWebpackPlugin { compiler.hooks.watchRun.tapPromise( 'DevupUIWebpackPluginReservation', async () => { + this.removeFiles(compiler.removedFiles) reserveBeforeCompile() }, ) @@ -407,6 +426,7 @@ export class DevupUIWebpackPlugin { ) let lastModifiedTime: string | null = null compiler.hooks.watchRun.tapPromise('DevupUIWebpackPlugin', async () => { + this.removeFiles(compiler.removedFiles) const stamps: number[] = [] if (existsDevup) stamps.push((await stat(this.options.devupFile)).mtimeMs) From 3a3834cfd9a619478958c144da996aae99f4d89a Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 20:27:12 +0900 Subject: [PATCH 2/2] test(plugin-utils): count the new removeFile export in the compiler namespace Co-Authored-By: Claude Opus 5.5 --- packages/plugin-utils/src/__tests__/compiler-types.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-utils/src/__tests__/compiler-types.test.ts b/packages/plugin-utils/src/__tests__/compiler-types.test.ts index 10e4921eb..c6b757714 100644 --- a/packages/plugin-utils/src/__tests__/compiler-types.test.ts +++ b/packages/plugin-utils/src/__tests__/compiler-types.test.ts @@ -78,7 +78,7 @@ describe('compiler structural types', () => { Object.keys(compiler).filter( (key) => key !== 'Output' && key !== 'default', ), - ).toHaveLength(28) + ).toHaveLength(29) } finally { compiler.resetStateForTesting() }