1
- import { getRulesByBreakpoint } from "./breakpoints";
2
1
import memoize from "memoizee";
3
- import { objectKeys } from "tsafe/objectKeys";
2
+ import { parseCss } from "../parseCss";
3
+ import { assert } from "tsafe/assert";
4
+ import { typeGuard } from "tsafe/typeGuard";
4
5
5
6
export const parseClassNames = memoize((rawCssCode: string): string[] => {
6
- const rulesByBreakpoint = getRulesByBreakpoint (rawCssCode);
7
+ const parsedCss = parseCss (rawCssCode);
7
8
8
9
const classNames = new Set<string>();
9
10
10
- objectKeys(rulesByBreakpoint).forEach(breakpoint => {
11
- const rules = rulesByBreakpoint[breakpoint];
12
- rules.forEach(({ selectors }) => {
11
+ JSON.stringify(parsedCss, (key, value) => {
12
+ if (key === "selectors") {
13
+ const selectors = value as unknown;
14
+
15
+ assert(
16
+ typeGuard<string[]>(
17
+ selectors,
18
+ selectors instanceof Array &&
19
+ selectors.every(selector => typeof selector === "string")
20
+ )
21
+ );
22
+
13
23
selectors.forEach(selector => {
14
24
const matchArr = selector.match(/\.fr-[a-zA-Z0-9_-]+(?:@[a-zA-Z0-9_-]+)?/g);
15
25
if (matchArr === null) {
@@ -20,8 +30,10 @@ export const parseClassNames = memoize((rawCssCode: string): string[] => {
20
30
.map(matchedStr => matchedStr.replace(/^\./, ""))
21
31
.forEach(className => classNames.add(className));
22
32
});
23
- });
33
+ }
34
+ return value;
24
35
});
36
+
25
37
return Array.from(classNames);
26
38
});
27
39
0 commit comments