Skip to content

Commit 7cb301e

Browse files
committed
feat(cssModules): update css modules when global style file change
1 parent 5574b7b commit 7cb301e

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/documents.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import path from 'path';
44
import { getCssModulesNames } from './postcss';
55
import * as Uri from 'vscode-uri';
66
import { setting } from './setting';
7+
import { cssExtName } from './constant';
78

89
class CssModules {
910
globalClassNames = new Map<string, Record<string, [number, number][]>>();
1011
classNames = new Map<string, Record<string, [number, number][]>>();
1112

13+
async resolvesGlobalClassName(filePath: string, code: string) {
14+
const res = await getCssModulesNames(filePath, code);
15+
this.globalClassNames.set(filePath, res);
16+
}
17+
1218
async resolvesGlobalClassNames() {
1319
const resolveStyleByFilePath = async (parentDir: string, filePath: string) => {
1420
const absPath = path.join(parentDir, filePath);
@@ -23,7 +29,7 @@ class CssModules {
2329
await resolveStyleByFilePath(absPath, filePath);
2430
}
2531
}
26-
} else if (st.isFile() && /\.(css|less|scss)$/.test(absPath)) {
32+
} else if (st.isFile() && cssExtName.test(absPath)) {
2733
const res = await getCssModulesNames(absPath, readFileSync(absPath, { encoding: 'utf-8' }));
2834
this.globalClassNames.set(absPath, res);
2935
}

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,15 @@ documents.onDidChangeContent((change) => {
105105
}
106106

107107
if (cssExtName.test(extname)) {
108-
cssModules.resolveClassNames(Uri.URI.parse(change.document.uri).fsPath, change.document.getText());
108+
const fsPath = Uri.URI.parse(change.document.uri).fsPath;
109+
110+
// locale file
111+
cssModules.resolveClassNames(fsPath, change.document.getText());
112+
113+
// global file
114+
if (cssModules.globalClassNames.has(fsPath)) {
115+
cssModules.resolvesGlobalClassName(fsPath, change.document.getText());
116+
}
109117
}
110118
});
111119

src/postcss.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import postcss from 'postcss';
1+
import postcss, { Result } from 'postcss';
22
import postLess from 'postcss-less';
33
import postScss from 'postcss-scss';
44
import postModules from 'postcss-modules';
@@ -20,17 +20,27 @@ export const getCssModulesNames = async (
2020
let json: Record<string, string> = {};
2121
const syntax = getSyntax(filePath);
2222

23-
const res = await postcss([
24-
postModules({
25-
getJSON(_cssFilename, outputJson) {
26-
json = outputJson;
27-
},
28-
}),
29-
]).process(code, {
30-
map: false,
31-
from: filePath,
32-
syntax,
33-
});
23+
let res: Result | undefined;
24+
25+
try {
26+
res = await postcss([
27+
postModules({
28+
getJSON(_cssFilename, outputJson) {
29+
json = outputJson;
30+
},
31+
}),
32+
]).process(code, {
33+
map: false,
34+
from: filePath,
35+
syntax,
36+
});
37+
} catch (error) {
38+
//
39+
}
40+
41+
if (!res) {
42+
return {};
43+
}
3444

3545
const valueKey: Record<
3646
string,

0 commit comments

Comments
 (0)