@purgeon/analyzer-css 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.mts +76 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +206 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-PRESENT Konstantin Kireyev <https://github.com/knst0>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AnalyzerPlugin, CssPurgeIRRule, GraphNode, OutputBundle, ParseContext, PluginName } from "@purgeon/core";
|
|
2
|
+
//#region src/class-graph.d.ts
|
|
3
|
+
declare class ClassGraph {
|
|
4
|
+
private readonly edges;
|
|
5
|
+
private ensure;
|
|
6
|
+
addClass(className: string): void;
|
|
7
|
+
addCoOccurrence(a: string, b: string): void;
|
|
8
|
+
neighbors(className: string): Set<string> | undefined;
|
|
9
|
+
classes(): IterableIterator<string>;
|
|
10
|
+
toObject(): Record<string, string[]>;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/css-node.d.ts
|
|
14
|
+
interface CssVarDeclaration {
|
|
15
|
+
name: string;
|
|
16
|
+
referencedVars: string[];
|
|
17
|
+
}
|
|
18
|
+
interface CssRule {
|
|
19
|
+
classes: string[];
|
|
20
|
+
declarations: string[];
|
|
21
|
+
declaredVars: string[];
|
|
22
|
+
referencedVars: string[];
|
|
23
|
+
varDeclarations: CssVarDeclaration[];
|
|
24
|
+
}
|
|
25
|
+
interface CssGraphNode extends GraphNode {
|
|
26
|
+
rules: CssRule[];
|
|
27
|
+
}
|
|
28
|
+
interface CssAnalyzerOptions {
|
|
29
|
+
filename?: string;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/var-graph.d.ts
|
|
33
|
+
declare class VarGraph {
|
|
34
|
+
private readonly declaredIn;
|
|
35
|
+
private readonly referencedIn;
|
|
36
|
+
private readonly dependsOn;
|
|
37
|
+
private readonly usedBy;
|
|
38
|
+
private ensure;
|
|
39
|
+
declare(varName: string, moduleId: string): void;
|
|
40
|
+
reference(varName: string, moduleId: string, declaringVar?: string): void;
|
|
41
|
+
declaredModules(varName: string): Set<string> | undefined;
|
|
42
|
+
referencingModules(varName: string): Set<string> | undefined;
|
|
43
|
+
dependenciesOf(varName: string): Set<string> | undefined;
|
|
44
|
+
dependentsOf(varName: string): Set<string> | undefined;
|
|
45
|
+
toObject(): {
|
|
46
|
+
declaredIn: Record<string, string[]>;
|
|
47
|
+
referencedIn: Record<string, string[]>;
|
|
48
|
+
dependsOn: Record<string, string[]>;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/css-analyzer.d.ts
|
|
53
|
+
declare class CssAnalyzer extends AnalyzerPlugin<CssGraphNode> {
|
|
54
|
+
private readonly options;
|
|
55
|
+
readonly name: PluginName;
|
|
56
|
+
private readonly classGraph;
|
|
57
|
+
private readonly varGraph;
|
|
58
|
+
constructor(options?: CssAnalyzerOptions);
|
|
59
|
+
getClassGraph(): ClassGraph;
|
|
60
|
+
getVarGraph(): VarGraph;
|
|
61
|
+
protected isModuleSupported(id: string): boolean;
|
|
62
|
+
protected parseModule(code: string, _id: string, _ctx: ParseContext): unknown;
|
|
63
|
+
protected createNode(id: string): CssGraphNode;
|
|
64
|
+
protected analyzeModule(id: string, _ast: unknown, code: string, node: CssGraphNode): void;
|
|
65
|
+
protected analyzeBundle(bundle: OutputBundle): void;
|
|
66
|
+
private parseCss;
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/purge-ir.d.ts
|
|
70
|
+
declare function toCssPurgeIR(rules: CssRule[]): CssPurgeIRRule[];
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/index.d.ts
|
|
73
|
+
declare const cssAnalyzer: (options?: CssAnalyzerOptions) => CssAnalyzer;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { ClassGraph, CssAnalyzer, CssAnalyzerOptions, CssGraphNode, CssRule, CssVarDeclaration, VarGraph, cssAnalyzer, toCssPurgeIR };
|
|
76
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/class-graph.ts","../src/css-node.ts","../src/var-graph.ts","../src/css-analyzer.ts","../src/purge-ir.ts","../src/index.ts"],"mappings":";;cAAa;mBACM;UAET;EASR,SAAS;EAIT,gBAAgB,WAAW;EAM3B,UAAU,oBAAoB;EAI9B,WAAW;EAIX,YAAY;;;;UC5BG;EACf;EACA;;UAGe;EACf;EACA;EACA;EACA;EACA,iBAAiB;;UAGF,qBAAqB;EACpC,OAAO;;UAGQ;EACf;;;;cCpBW;mBACM;mBACA;mBACA;mBACA;UAET;EASR,QAAQ,iBAAiB;EAIzB,UAAU,iBAAiB,kBAAkB;EAS7C,gBAAgB,kBAAkB;EAIlC,mBAAmB,kBAAkB;EAIrC,eAAe,kBAAkB;EAIjC,aAAa,kBAAkB;EAI/B;IACE,YAAY;IACZ,cAAc;IACd,WAAW;;;;;cC5BF,oBAAoB,eAAe;mBAMjB;WALpB,MAAM;mBAEE;mBACA;cAEY,UAAS;EAItC,iBAAiB;EAIjB,eAAe;YAIL,kBAAkB;YAIlB,YAAY,cAAc,aAAa,MAAM;YAI7C,WAAW,aAAa;YAIxB,cAAc,YAAY,eAAe,cAAc,MAAM;YAW7D,cAAc,QAAQ;UAWxB;;;;iBCnEM,aAAa,OAAO,YAAY;;;cCKnC,cAAe,UAAU,uBAAkB"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { AnalyzerPlugin, classesOf, pluginName, splitCompoundSelectors } from "@purgeon/core";
|
|
2
|
+
import { transform } from "lightningcss";
|
|
3
|
+
//#region src/class-graph.ts
|
|
4
|
+
var ClassGraph = class {
|
|
5
|
+
edges = /* @__PURE__ */ new Map();
|
|
6
|
+
ensure(className) {
|
|
7
|
+
let neighbors = this.edges.get(className);
|
|
8
|
+
if (!neighbors) {
|
|
9
|
+
neighbors = /* @__PURE__ */ new Set();
|
|
10
|
+
this.edges.set(className, neighbors);
|
|
11
|
+
}
|
|
12
|
+
return neighbors;
|
|
13
|
+
}
|
|
14
|
+
addClass(className) {
|
|
15
|
+
this.ensure(className);
|
|
16
|
+
}
|
|
17
|
+
addCoOccurrence(a, b) {
|
|
18
|
+
if (a === b) return;
|
|
19
|
+
this.ensure(a).add(b);
|
|
20
|
+
this.ensure(b).add(a);
|
|
21
|
+
}
|
|
22
|
+
neighbors(className) {
|
|
23
|
+
return this.edges.get(className);
|
|
24
|
+
}
|
|
25
|
+
classes() {
|
|
26
|
+
return this.edges.keys();
|
|
27
|
+
}
|
|
28
|
+
toObject() {
|
|
29
|
+
return Object.fromEntries([...this.edges].map(([className, neighbors]) => [className, [...neighbors]]));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/var-graph.ts
|
|
34
|
+
var VarGraph = class {
|
|
35
|
+
declaredIn = /* @__PURE__ */ new Map();
|
|
36
|
+
referencedIn = /* @__PURE__ */ new Map();
|
|
37
|
+
dependsOn = /* @__PURE__ */ new Map();
|
|
38
|
+
usedBy = /* @__PURE__ */ new Map();
|
|
39
|
+
ensure(map, key) {
|
|
40
|
+
let values = map.get(key);
|
|
41
|
+
if (!values) {
|
|
42
|
+
values = /* @__PURE__ */ new Set();
|
|
43
|
+
map.set(key, values);
|
|
44
|
+
}
|
|
45
|
+
return values;
|
|
46
|
+
}
|
|
47
|
+
declare(varName, moduleId) {
|
|
48
|
+
this.ensure(this.declaredIn, varName).add(moduleId);
|
|
49
|
+
}
|
|
50
|
+
reference(varName, moduleId, declaringVar) {
|
|
51
|
+
this.ensure(this.referencedIn, varName).add(moduleId);
|
|
52
|
+
if (declaringVar && declaringVar !== varName) {
|
|
53
|
+
this.ensure(this.dependsOn, declaringVar).add(varName);
|
|
54
|
+
this.ensure(this.usedBy, varName).add(declaringVar);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
declaredModules(varName) {
|
|
58
|
+
return this.declaredIn.get(varName);
|
|
59
|
+
}
|
|
60
|
+
referencingModules(varName) {
|
|
61
|
+
return this.referencedIn.get(varName);
|
|
62
|
+
}
|
|
63
|
+
dependenciesOf(varName) {
|
|
64
|
+
return this.dependsOn.get(varName);
|
|
65
|
+
}
|
|
66
|
+
dependentsOf(varName) {
|
|
67
|
+
return this.usedBy.get(varName);
|
|
68
|
+
}
|
|
69
|
+
toObject() {
|
|
70
|
+
return {
|
|
71
|
+
declaredIn: toRecord(this.declaredIn),
|
|
72
|
+
referencedIn: toRecord(this.referencedIn),
|
|
73
|
+
dependsOn: toRecord(this.dependsOn)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function toRecord(map) {
|
|
78
|
+
return Object.fromEntries([...map].map(([key, values]) => [key, [...values]]));
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/css-analyzer.ts
|
|
82
|
+
function normalizeModuleId(id) {
|
|
83
|
+
return id.replace(/\?.*$/, "").replace(/\\/g, "/");
|
|
84
|
+
}
|
|
85
|
+
var CssAnalyzer = class extends AnalyzerPlugin {
|
|
86
|
+
options;
|
|
87
|
+
name = pluginName("css-graph");
|
|
88
|
+
classGraph = new ClassGraph();
|
|
89
|
+
varGraph = new VarGraph();
|
|
90
|
+
constructor(options = {}) {
|
|
91
|
+
super();
|
|
92
|
+
this.options = options;
|
|
93
|
+
}
|
|
94
|
+
getClassGraph() {
|
|
95
|
+
return this.classGraph;
|
|
96
|
+
}
|
|
97
|
+
getVarGraph() {
|
|
98
|
+
return this.varGraph;
|
|
99
|
+
}
|
|
100
|
+
isModuleSupported(id) {
|
|
101
|
+
return id.endsWith(".module.css");
|
|
102
|
+
}
|
|
103
|
+
parseModule(code, _id, _ctx) {
|
|
104
|
+
return code;
|
|
105
|
+
}
|
|
106
|
+
createNode(id) {
|
|
107
|
+
return {
|
|
108
|
+
id,
|
|
109
|
+
imports: [],
|
|
110
|
+
importers: [],
|
|
111
|
+
rules: []
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
analyzeModule(id, _ast, code, node) {
|
|
115
|
+
if (node.rules.length > 0) return;
|
|
116
|
+
const rawRules = this.parseCss(code, id);
|
|
117
|
+
const normalizedId = normalizeModuleId(id);
|
|
118
|
+
node.rules = rawRules.map((rule) => ({
|
|
119
|
+
...rule,
|
|
120
|
+
classes: rule.classes.map((cls) => `${normalizedId}::${cls}`)
|
|
121
|
+
}));
|
|
122
|
+
}
|
|
123
|
+
analyzeBundle(bundle) {
|
|
124
|
+
for (const fileName in bundle) {
|
|
125
|
+
const output = bundle[fileName];
|
|
126
|
+
if (output.type !== "asset" || !fileName.endsWith(".css")) continue;
|
|
127
|
+
const source = typeof output.source === "string" ? output.source : Buffer.from(output.source).toString("utf-8");
|
|
128
|
+
const node = this.graph.ensureNode(fileName, (id) => this.createNode(id));
|
|
129
|
+
node.rules = this.parseCss(source, fileName);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
parseCss(code, id) {
|
|
133
|
+
const rules = [];
|
|
134
|
+
let currentRule = null;
|
|
135
|
+
let currentDeclaredVar = null;
|
|
136
|
+
let currentVarDeclaration = null;
|
|
137
|
+
transform({
|
|
138
|
+
filename: this.options.filename ?? id,
|
|
139
|
+
code: Buffer.from(code),
|
|
140
|
+
visitor: {
|
|
141
|
+
Rule: { style: (rule) => {
|
|
142
|
+
const classes = /* @__PURE__ */ new Set();
|
|
143
|
+
for (const selector of rule.value.selectors) for (const group of splitCompoundSelectors(selector)) {
|
|
144
|
+
const groupClasses = classesOf(group);
|
|
145
|
+
for (const className of groupClasses) classes.add(className);
|
|
146
|
+
for (let i = 0; i < groupClasses.length; i++) for (let j = i + 1; j < groupClasses.length; j++) this.classGraph.addCoOccurrence(groupClasses[i], groupClasses[j]);
|
|
147
|
+
}
|
|
148
|
+
for (const className of classes) this.classGraph.addClass(className);
|
|
149
|
+
currentRule = {
|
|
150
|
+
classes: [...classes],
|
|
151
|
+
declarations: (rule.value.declarations?.declarations ?? []).map((declaration) => declaration.property),
|
|
152
|
+
declaredVars: [],
|
|
153
|
+
referencedVars: [],
|
|
154
|
+
varDeclarations: []
|
|
155
|
+
};
|
|
156
|
+
rules.push(currentRule);
|
|
157
|
+
} },
|
|
158
|
+
RuleExit: { style: () => {
|
|
159
|
+
currentRule = null;
|
|
160
|
+
} },
|
|
161
|
+
Declaration: { custom: (declaration) => {
|
|
162
|
+
const varName = declaration.name;
|
|
163
|
+
currentDeclaredVar = varName;
|
|
164
|
+
currentRule?.declaredVars.push(varName);
|
|
165
|
+
this.varGraph.declare(varName, id);
|
|
166
|
+
currentVarDeclaration = {
|
|
167
|
+
name: varName,
|
|
168
|
+
referencedVars: []
|
|
169
|
+
};
|
|
170
|
+
currentRule?.varDeclarations.push(currentVarDeclaration);
|
|
171
|
+
} },
|
|
172
|
+
DeclarationExit: { custom: () => {
|
|
173
|
+
currentDeclaredVar = null;
|
|
174
|
+
currentVarDeclaration = null;
|
|
175
|
+
} },
|
|
176
|
+
Variable: (variable) => {
|
|
177
|
+
const varName = variable.name.ident;
|
|
178
|
+
currentRule?.referencedVars.push(varName);
|
|
179
|
+
currentVarDeclaration?.referencedVars.push(varName);
|
|
180
|
+
this.varGraph.reference(varName, id, currentDeclaredVar ?? void 0);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return rules;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/purge-ir.ts
|
|
189
|
+
function toCssPurgeIR(rules) {
|
|
190
|
+
return rules.map((rule) => ({
|
|
191
|
+
classes: rule.classes,
|
|
192
|
+
declaredVars: rule.declaredVars,
|
|
193
|
+
referencedVars: rule.referencedVars,
|
|
194
|
+
varDeclarations: rule.varDeclarations.map((decl) => ({
|
|
195
|
+
name: decl.name,
|
|
196
|
+
referencedVars: decl.referencedVars
|
|
197
|
+
}))
|
|
198
|
+
}));
|
|
199
|
+
}
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/index.ts
|
|
202
|
+
const cssAnalyzer = (options) => new CssAnalyzer(options);
|
|
203
|
+
//#endregion
|
|
204
|
+
export { ClassGraph, CssAnalyzer, VarGraph, cssAnalyzer, toCssPurgeIR };
|
|
205
|
+
|
|
206
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/class-graph.ts","../src/var-graph.ts","../src/css-analyzer.ts","../src/purge-ir.ts","../src/index.ts"],"sourcesContent":["export class ClassGraph {\n private readonly edges = new Map<string, Set<string>>();\n\n private ensure(className: string): Set<string> {\n let neighbors = this.edges.get(className);\n if (!neighbors) {\n neighbors = new Set();\n this.edges.set(className, neighbors);\n }\n return neighbors;\n }\n\n addClass(className: string): void {\n this.ensure(className);\n }\n\n addCoOccurrence(a: string, b: string): void {\n if (a === b) return;\n this.ensure(a).add(b);\n this.ensure(b).add(a);\n }\n\n neighbors(className: string): Set<string> | undefined {\n return this.edges.get(className);\n }\n\n classes(): IterableIterator<string> {\n return this.edges.keys();\n }\n\n toObject(): Record<string, string[]> {\n return Object.fromEntries([...this.edges].map(([className, neighbors]) => [className, [...neighbors]]));\n }\n}\n","export class VarGraph {\n private readonly declaredIn = new Map<string, Set<string>>();\n private readonly referencedIn = new Map<string, Set<string>>();\n private readonly dependsOn = new Map<string, Set<string>>();\n private readonly usedBy = new Map<string, Set<string>>();\n\n private ensure(map: Map<string, Set<string>>, key: string): Set<string> {\n let values = map.get(key);\n if (!values) {\n values = new Set();\n map.set(key, values);\n }\n return values;\n }\n\n declare(varName: string, moduleId: string): void {\n this.ensure(this.declaredIn, varName).add(moduleId);\n }\n\n reference(varName: string, moduleId: string, declaringVar?: string): void {\n this.ensure(this.referencedIn, varName).add(moduleId);\n\n if (declaringVar && declaringVar !== varName) {\n this.ensure(this.dependsOn, declaringVar).add(varName);\n this.ensure(this.usedBy, varName).add(declaringVar);\n }\n }\n\n declaredModules(varName: string): Set<string> | undefined {\n return this.declaredIn.get(varName);\n }\n\n referencingModules(varName: string): Set<string> | undefined {\n return this.referencedIn.get(varName);\n }\n\n dependenciesOf(varName: string): Set<string> | undefined {\n return this.dependsOn.get(varName);\n }\n\n dependentsOf(varName: string): Set<string> | undefined {\n return this.usedBy.get(varName);\n }\n\n toObject(): {\n declaredIn: Record<string, string[]>;\n referencedIn: Record<string, string[]>;\n dependsOn: Record<string, string[]>;\n } {\n return {\n declaredIn: toRecord(this.declaredIn),\n referencedIn: toRecord(this.referencedIn),\n dependsOn: toRecord(this.dependsOn),\n };\n }\n}\n\nfunction toRecord(map: Map<string, Set<string>>): Record<string, string[]> {\n return Object.fromEntries([...map].map(([key, values]) => [key, [...values]]));\n}\n","import {\n AnalyzerPlugin,\n classesOf,\n pluginName,\n splitCompoundSelectors,\n type OutputBundle,\n type ParseContext,\n type PluginName,\n} from \"@purgeon/core\";\nimport { transform } from \"lightningcss\";\n\nimport { ClassGraph } from \"./class-graph\";\nimport type { CssAnalyzerOptions, CssGraphNode, CssRule, CssVarDeclaration } from \"./css-node\";\nimport { VarGraph } from \"./var-graph\";\n\nfunction normalizeModuleId(id: string): string {\n return id.replace(/\\?.*$/, \"\").replace(/\\\\/g, \"/\");\n}\n\nexport class CssAnalyzer extends AnalyzerPlugin<CssGraphNode> {\n readonly name: PluginName = pluginName(\"css-graph\");\n\n private readonly classGraph = new ClassGraph();\n private readonly varGraph = new VarGraph();\n\n constructor(private readonly options: CssAnalyzerOptions = {}) {\n super();\n }\n\n getClassGraph(): ClassGraph {\n return this.classGraph;\n }\n\n getVarGraph(): VarGraph {\n return this.varGraph;\n }\n\n protected isModuleSupported(id: string): boolean {\n return id.endsWith(\".module.css\");\n }\n\n protected parseModule(code: string, _id: string, _ctx: ParseContext): unknown {\n return code;\n }\n\n protected createNode(id: string): CssGraphNode {\n return { id, imports: [], importers: [], rules: [] };\n }\n\n protected analyzeModule(id: string, _ast: unknown, code: string, node: CssGraphNode): void {\n if (node.rules.length > 0) return;\n\n const rawRules = this.parseCss(code, id);\n const normalizedId = normalizeModuleId(id);\n node.rules = rawRules.map((rule) => ({\n ...rule,\n classes: rule.classes.map((cls) => `${normalizedId}::${cls}`),\n }));\n }\n\n protected analyzeBundle(bundle: OutputBundle): void {\n for (const fileName in bundle) {\n const output = bundle[fileName]!;\n if (output.type !== \"asset\" || !fileName.endsWith(\".css\")) continue;\n\n const source = typeof output.source === \"string\" ? output.source : Buffer.from(output.source).toString(\"utf-8\");\n const node = this.graph.ensureNode(fileName, (id) => this.createNode(id));\n node.rules = this.parseCss(source, fileName);\n }\n }\n\n private parseCss(code: string, id: string): CssRule[] {\n const rules: CssRule[] = [];\n\n let currentRule: CssRule | null = null;\n let currentDeclaredVar: string | null = null;\n let currentVarDeclaration: CssVarDeclaration | null = null;\n\n transform({\n filename: this.options.filename ?? id,\n code: Buffer.from(code),\n visitor: {\n Rule: {\n style: (rule) => {\n const classes = new Set<string>();\n\n for (const selector of rule.value.selectors) {\n for (const group of splitCompoundSelectors(selector)) {\n const groupClasses = classesOf(group);\n for (const className of groupClasses) classes.add(className);\n for (let i = 0; i < groupClasses.length; i++) {\n for (let j = i + 1; j < groupClasses.length; j++) {\n this.classGraph.addCoOccurrence(groupClasses[i]!, groupClasses[j]!);\n }\n }\n }\n }\n\n for (const className of classes) this.classGraph.addClass(className);\n\n currentRule = {\n classes: [...classes],\n declarations: (rule.value.declarations?.declarations ?? []).map((declaration: { property: string }) => declaration.property),\n declaredVars: [],\n referencedVars: [],\n varDeclarations: [],\n };\n rules.push(currentRule);\n },\n },\n RuleExit: {\n style: () => {\n currentRule = null;\n },\n },\n Declaration: {\n custom: (declaration) => {\n const varName = declaration.name;\n currentDeclaredVar = varName;\n currentRule?.declaredVars.push(varName);\n this.varGraph.declare(varName, id);\n\n currentVarDeclaration = { name: varName, referencedVars: [] };\n currentRule?.varDeclarations.push(currentVarDeclaration);\n },\n },\n DeclarationExit: {\n custom: () => {\n currentDeclaredVar = null;\n currentVarDeclaration = null;\n },\n },\n Variable: (variable) => {\n const varName = variable.name.ident;\n currentRule?.referencedVars.push(varName);\n currentVarDeclaration?.referencedVars.push(varName);\n this.varGraph.reference(varName, id, currentDeclaredVar ?? undefined);\n },\n },\n });\n\n return rules;\n }\n}\n","import type { CssPurgeIRRule, CssPurgeIRVarDeclaration } from \"@purgeon/core\";\n\nimport type { CssRule, CssVarDeclaration } from \"./css-node\";\n\nexport function toCssPurgeIR(rules: CssRule[]): CssPurgeIRRule[] {\n return rules.map(\n (rule: CssRule): CssPurgeIRRule => ({\n classes: rule.classes,\n declaredVars: rule.declaredVars,\n referencedVars: rule.referencedVars,\n varDeclarations: rule.varDeclarations.map(\n (decl: CssVarDeclaration): CssPurgeIRVarDeclaration => ({\n name: decl.name,\n referencedVars: decl.referencedVars,\n }),\n ),\n }),\n );\n}\n","import { CssAnalyzer } from \"./css-analyzer\";\nimport type { CssAnalyzerOptions } from \"./css-node\";\n\nexport * from \"./class-graph\";\nexport * from \"./css-analyzer\";\nexport * from \"./css-node\";\nexport * from \"./purge-ir\";\nexport * from \"./var-graph\";\n\nexport const cssAnalyzer = (options?: CssAnalyzerOptions) => new CssAnalyzer(options);\n"],"mappings":";;;AAAA,IAAa,aAAb,MAAwB;CACtB,wBAAyB,IAAI,IAAyB;CAEtD,OAAe,WAAgC;EAC7C,IAAI,YAAY,KAAK,MAAM,IAAI,SAAS;EACxC,IAAI,CAAC,WAAW;GACd,4BAAY,IAAI,IAAI;GACpB,KAAK,MAAM,IAAI,WAAW,SAAS;EACrC;EACA,OAAO;CACT;CAEA,SAAS,WAAyB;EAChC,KAAK,OAAO,SAAS;CACvB;CAEA,gBAAgB,GAAW,GAAiB;EAC1C,IAAI,MAAM,GAAG;EACb,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;EACpB,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;CACtB;CAEA,UAAU,WAA4C;EACpD,OAAO,KAAK,MAAM,IAAI,SAAS;CACjC;CAEA,UAAoC;EAClC,OAAO,KAAK,MAAM,KAAK;CACzB;CAEA,WAAqC;EACnC,OAAO,OAAO,YAAY,CAAC,GAAG,KAAK,KAAK,CAAC,CAAC,KAAK,CAAC,WAAW,eAAe,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;CACxG;AACF;;;ACjCA,IAAa,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aAAa,eAAe,KAAK,OAAO;KACxC,uBAAuB,eAAe,KAAK,OAAO;KAClD,KAAK,SAAS,UAAU,SAAS,IAAI,sBAAsB,KAAA,CAAS;IACtE;GACF;EACF,CAAC;EAED,OAAO;CACT;AACF;;;AC3IA,SAAgB,aAAa,OAAoC;CAC/D,OAAO,MAAM,KACV,UAAmC;EAClC,SAAS,KAAK;EACd,cAAc,KAAK;EACnB,gBAAgB,KAAK;EACrB,iBAAiB,KAAK,gBAAgB,KACnC,UAAuD;GACtD,MAAM,KAAK;GACX,gBAAgB,KAAK;EACvB,EACF;CACF,EACF;AACF;;;ACTA,MAAa,eAAe,YAAiC,IAAI,YAAY,OAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@purgeon/analyzer-css",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Walks bundled CSS via lightningcss visitor, builds class co-occurrence and custom-property graphs for purgeon",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"css",
|
|
7
|
+
"lightningcss",
|
|
8
|
+
"purge",
|
|
9
|
+
"rolldown",
|
|
10
|
+
"tree-shaking",
|
|
11
|
+
"vite"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"author": "Konstantin Kireyev <https://github.com/knst0>",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/knst0/purgeon.git",
|
|
18
|
+
"directory": "packages/analyzer-css"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"default": "./dist/index.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"lightningcss": "^1.32.0",
|
|
35
|
+
"@purgeon/core": "0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsdown"
|
|
39
|
+
}
|
|
40
|
+
}
|