Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down Expand Up @@ -59,6 +59,22 @@ export const pluginTypedCSSModules = (
}
},
);
let previousStyles = new Set<string>();
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<string>();
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;
},
);
}
});
}
Expand Down
17 changes: 12 additions & 5 deletions test/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-'),
);
Expand All @@ -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' } },
},
Expand All @@ -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(
Expand Down