Skip to content

Commit 228288f

Browse files
committed
use postcss for parsing css
1 parent 01a7958 commit 228288f

File tree

5 files changed

+85
-45
lines changed

5 files changed

+85
-45
lines changed

package-lock.json

Lines changed: 66 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@
8181
},
8282
"dependencies": {
8383
"bluebird": "^3.5.1",
84-
"css": "^2.2.4",
8584
"htmlparser2": "^3.9.2",
8685
"lodash": "^4.17.11",
86+
"postcss": "^7.0.32",
8787
"request": "^2.88.0",
8888
"request-promise": "^4.2.1",
8989
"source-map-support": "^0.5.3",

src/parse-engines/common/css-class-extractor.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import * as css from "css";
1+
import * as postcss from 'postcss';
22
import CssClassDefinition from "../../common/css-class-definition";
33

44
export default class CssClassExtractor {
55
/**
66
* @description Extracts class names from CSS AST
77
*/
8-
public static extract(ast: css.Stylesheet): CssClassDefinition[] {
8+
public static extract(ast: postcss.Root): CssClassDefinition[] {
99
const classNameRegex: RegExp = /[.]([\w-]+)/g;
1010

1111
const definitions: CssClassDefinition[] = [];
1212

1313
// go through each of the rules...
14-
ast.stylesheet.rules.forEach((rule: css.Rule) => {
15-
// ...of type rule
16-
if (rule.type === "rule") {
17-
// go through each of the selectors of the current rule
18-
rule.selectors.forEach((selector: string) => {
19-
let item: RegExpExecArray = classNameRegex.exec(selector);
20-
while (item) {
21-
definitions.push(new CssClassDefinition(item[1]));
22-
item = classNameRegex.exec(selector);
23-
}
24-
});
25-
}
14+
ast.walkRules((rule) => {
15+
// go through each of the selectors of the current rule
16+
rule.selectors.forEach((selector) => {
17+
let item: RegExpExecArray = classNameRegex.exec(selector);
18+
while (item) {
19+
definitions.push(new CssClassDefinition(item[1]));
20+
item = classNameRegex.exec(selector);
21+
}
22+
});
2623
});
2724

2825
return definitions;

src/parse-engines/types/css-parse-engine.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as css from "css";
2-
import * as vscode from "vscode";
1+
import * as postcss from 'postcss';
32
import CssClassDefinition from "../../common/css-class-definition";
43
import CssClassExtractor from "../common/css-class-extractor";
54
import IParseEngine from "../common/parse-engine";
@@ -11,9 +10,9 @@ class CssParseEngine implements IParseEngine {
1110

1211
public async parse(textDocument: ISimpleTextDocument): Promise<CssClassDefinition[]> {
1312
const code: string = textDocument.getText();
14-
const codeAst: css.Stylesheet = css.parse(code);
15-
16-
return CssClassExtractor.extract(codeAst);
13+
const result = await postcss().process(code);
14+
if (!result.root) return [];
15+
return CssClassExtractor.extract(result.root);
1716
}
1817
}
1918

src/parse-engines/types/html-parse-engine.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as Bluebird from "bluebird";
2-
import * as css from "css";
32
import * as html from "htmlparser2";
3+
import * as postcss from 'postcss';
44
import * as request from "request-promise";
5-
import * as vscode from "vscode";
65
import CssClassDefinition from "../../common/css-class-definition";
76
import CssClassExtractor from "../common/css-class-extractor";
87
import IParseEngine from "../common/parse-engine";
@@ -42,7 +41,7 @@ class HtmlParseEngine implements IParseEngine {
4241
},
4342
ontext: (text: string) => {
4443
if (tag === "style") {
45-
definitions.push(...CssClassExtractor.extract(css.parse(text)));
44+
definitions.push(...CssClassExtractor.extract(postcss().process(text).root));
4645
}
4746
},
4847
});
@@ -52,7 +51,7 @@ class HtmlParseEngine implements IParseEngine {
5251

5352
await Bluebird.map(urls, async (url) => {
5453
const content = await request.get(url);
55-
definitions.push(...CssClassExtractor.extract(css.parse(content)));
54+
definitions.push(...CssClassExtractor.extract(postcss().process(content).root));
5655
}, { concurrency: 10 });
5756

5857
return definitions;

0 commit comments

Comments
 (0)