Skip to content

Commit 7d1caad

Browse files
committed
refactor: 💡 Shared CSS modules scoped name pattern
1 parent 7e3a92a commit 7d1caad

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

plugins/css-colocate/css-preprocess.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import fs from 'fs-extra';
22
import path from 'path';
33
import postcss from 'postcss';
44
import postcssModules from 'postcss-modules';
5-
import { getTempDir, findFiles } from './utils';
6-
7-
const generateScopedName = '[hash:base64:8]_[local]';
5+
import { getTempDir, findFiles, generateScopedName } from './utils';
86

97
export async function preprocessCssModules(rootDir: string): Promise<void> {
108
const srcDir = path.join(rootDir, 'src');

plugins/css-colocate/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import fs from 'fs-extra';
22
import { glob } from 'glob';
33
import path from 'path';
44

5+
/**
6+
* Shared CSS modules scoped name pattern.
7+
* Used by both Vite config and CSS preprocessing to ensure consistency.
8+
*/
9+
export const generateScopedName = '[local]__[hash:base64:5]';
10+
511
export const fileExists = async (filePath: string): Promise<boolean> => {
612
try {
713
await fs.access(filePath);

vite.config.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { externalizeDeps } from 'vite-plugin-externalize-deps';
77
import tsconfigPaths from 'vite-tsconfig-paths';
88
import { visualizer } from 'rollup-plugin-visualizer';
99
import { cssColocatePlugin } from './plugins/css-colocate';
10+
import { generateScopedName } from './plugins/css-colocate/utils';
1011

1112
const srcDir = path.resolve(__dirname, 'src').replace(/\\/g, '/');
1213

@@ -81,10 +82,7 @@ const viteConfig = defineConfig({
8182
css: {
8283
modules: {
8384
// Generate predictable class names for debugging in dev
84-
generateScopedName:
85-
process.env.NODE_ENV === 'production'
86-
? '[hash:base64:8]'
87-
: '[name]__[local]__[hash:base64:5]',
85+
generateScopedName,
8886
},
8987
},
9088
plugins: [

0 commit comments

Comments
 (0)