@purgeon/rolldown-plugin 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 +16 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +101 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -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,16 @@
|
|
|
1
|
+
import { AnalyzerPlugin } from "@purgeon/core";
|
|
2
|
+
import { Plugin } from "rolldown";
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
type PurgeonPluginResult = Plugin & {
|
|
5
|
+
enforce?: "pre" | "post";
|
|
6
|
+
};
|
|
7
|
+
declare function purgeon(options?: PluginOptions): PurgeonPluginResult;
|
|
8
|
+
interface PluginOptions {
|
|
9
|
+
plugins?: AnalyzerPlugin[];
|
|
10
|
+
debug?: boolean | {
|
|
11
|
+
outFile: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { PluginOptions, purgeon };
|
|
16
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;KAOK,sBAAsB;EAAmB;;iBAE9B,QAAQ,UAAS,gBAAqB;UAmIrC;EACf,UAAU;EACV;IAAoB"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { toCssPurgeIR } from "@purgeon/analyzer-css";
|
|
3
|
+
import { toJsxPurgeIR } from "@purgeon/analyzer-jsx";
|
|
4
|
+
import { extractUsedClasses, extractUsedVars, isAnalyzer, purgeUnusedCss } from "@purgeon/core";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
function purgeon(options = {}) {
|
|
7
|
+
const wrappers = (options.plugins ?? []).filter((p) => isAnalyzer(p)).map((p) => ({
|
|
8
|
+
plugin: p.toRolldownPlugin(),
|
|
9
|
+
getGraph: () => p.getGraph()
|
|
10
|
+
}));
|
|
11
|
+
const outFile = resolveDebugOutFile(options.debug);
|
|
12
|
+
let usage = null;
|
|
13
|
+
return {
|
|
14
|
+
name: "purgeon",
|
|
15
|
+
enforce: "pre",
|
|
16
|
+
transform(code, id) {
|
|
17
|
+
for (const wrapper of wrappers) try {
|
|
18
|
+
wrapper.plugin.transform.call(this, code, id);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
21
|
+
this.warn(`[${wrapper.plugin.name}] failed to analyze ${id}: ${message}`);
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
},
|
|
25
|
+
moduleParsed(moduleInfo) {
|
|
26
|
+
for (const wrapper of wrappers) wrapper.plugin.moduleParsed.call(this, moduleInfo);
|
|
27
|
+
},
|
|
28
|
+
generateBundle(options, bundle) {
|
|
29
|
+
for (const wrapper of wrappers) wrapper.plugin.generateBundle.call(this, options, bundle);
|
|
30
|
+
usage = reportUsage(wrappers, this);
|
|
31
|
+
if (usage) purgeBundle(bundle, usage.usedClasses, usage.usedVars);
|
|
32
|
+
},
|
|
33
|
+
writeBundle() {
|
|
34
|
+
if (!outFile) return;
|
|
35
|
+
const graphs = {};
|
|
36
|
+
for (const wrapper of wrappers) graphs[wrapper.plugin.name] = wrapper.getGraph().toObject();
|
|
37
|
+
if (usage) {
|
|
38
|
+
graphs.usedClasses = usage.classes;
|
|
39
|
+
graphs.usedVars = usage.vars;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
writeFileSync(outFile, JSON.stringify(graphs, null, 2), "utf-8");
|
|
43
|
+
this.info(`Wrote debug output to ${outFile}`);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
this.warn(`Failed to write debug output to ${outFile}: ${error instanceof Error ? error.message : String(error)}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function summarize(used, total) {
|
|
51
|
+
const unused = [...total].filter((name) => !used.has(name));
|
|
52
|
+
return {
|
|
53
|
+
used: [...used],
|
|
54
|
+
unused,
|
|
55
|
+
total: total.size,
|
|
56
|
+
usedCount: used.size,
|
|
57
|
+
unusedCount: unused.length
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function reportUsage(wrappers, ctx) {
|
|
61
|
+
const jsxWrapper = wrappers.find((w) => w.plugin.name === "jsx-graph");
|
|
62
|
+
const cssWrapper = wrappers.find((w) => w.plugin.name === "css-graph");
|
|
63
|
+
if (!jsxWrapper || !cssWrapper) return null;
|
|
64
|
+
const jsxNodes = [...jsxWrapper.getGraph().values()].map((n) => toJsxPurgeIR(n));
|
|
65
|
+
const cssRules = [...cssWrapper.getGraph().values()].flatMap((node) => toCssPurgeIR(node.rules));
|
|
66
|
+
const cssClasses = /* @__PURE__ */ new Set();
|
|
67
|
+
for (const rule of cssRules) for (const className of rule.classes) cssClasses.add(className);
|
|
68
|
+
const usedClasses = extractUsedClasses(jsxNodes, cssClasses);
|
|
69
|
+
const classes = summarize(usedClasses, cssClasses);
|
|
70
|
+
const allVars = /* @__PURE__ */ new Set();
|
|
71
|
+
for (const rule of cssRules) {
|
|
72
|
+
for (const varName of rule.declaredVars) allVars.add(varName);
|
|
73
|
+
for (const varName of rule.referencedVars) allVars.add(varName);
|
|
74
|
+
}
|
|
75
|
+
const usedVars = extractUsedVars(cssRules, usedClasses);
|
|
76
|
+
const vars = summarize(usedVars, allVars);
|
|
77
|
+
ctx.info(`[purgeon] CSS classes: ${classes.usedCount} used, ${classes.unusedCount} unused (of ${classes.total} total)`);
|
|
78
|
+
ctx.info(`[purgeon] CSS vars: ${vars.usedCount} used, ${vars.unusedCount} unused (of ${vars.total} total)`);
|
|
79
|
+
return {
|
|
80
|
+
classes,
|
|
81
|
+
vars,
|
|
82
|
+
usedClasses,
|
|
83
|
+
usedVars
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function purgeBundle(bundle, usedClasses, usedVars) {
|
|
87
|
+
for (const fileName in bundle) {
|
|
88
|
+
const output = bundle[fileName];
|
|
89
|
+
if (output.type !== "asset" || !fileName.endsWith(".css")) continue;
|
|
90
|
+
output.source = purgeUnusedCss(typeof output.source === "string" ? output.source : Buffer.from(output.source).toString("utf-8"), fileName, usedClasses, usedVars);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function resolveDebugOutFile(debug) {
|
|
94
|
+
if (!debug) return null;
|
|
95
|
+
if (typeof debug === "object") return debug.outFile;
|
|
96
|
+
return "graph.json";
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { purgeon };
|
|
100
|
+
|
|
101
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { writeFileSync } from \"node:fs\";\n\nimport { toCssPurgeIR, type CssGraphNode } from \"@purgeon/analyzer-css\";\nimport { toJsxPurgeIR, type JsxGraphNode } from \"@purgeon/analyzer-jsx\";\nimport { extractUsedClasses, extractUsedVars, isAnalyzer, purgeUnusedCss, type AnalyzerPlugin, type OutputBundle } from \"@purgeon/core\";\nimport type { Plugin as RolldownPlugin } from \"rolldown\";\n\ntype PurgeonPluginResult = RolldownPlugin & { enforce?: \"pre\" | \"post\" };\n\nexport function purgeon(options: PluginOptions = {}): PurgeonPluginResult {\n const wrappers = (options.plugins ?? [])\n .filter((p) => isAnalyzer(p))\n .map((p) => ({ plugin: p.toRolldownPlugin(), getGraph: () => p.getGraph() }));\n const outFile = resolveDebugOutFile(options.debug);\n let usage: UsageReport | null = null;\n\n return {\n name: \"purgeon\",\n enforce: \"pre\",\n\n transform(code, id) {\n for (const wrapper of wrappers) {\n try {\n wrapper.plugin.transform.call(this as any, code, id);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n this.warn(`[${wrapper.plugin.name}] failed to analyze ${id}: ${message}`);\n }\n }\n return null;\n },\n\n moduleParsed(moduleInfo) {\n for (const wrapper of wrappers) {\n wrapper.plugin.moduleParsed.call(this, moduleInfo);\n }\n },\n\n generateBundle(options, bundle) {\n for (const wrapper of wrappers) {\n wrapper.plugin.generateBundle.call(this as any, options, bundle as any);\n }\n\n usage = reportUsage(wrappers, this);\n if (usage) purgeBundle(bundle as unknown as OutputBundle, usage.usedClasses, usage.usedVars);\n },\n\n writeBundle() {\n if (!outFile) return;\n\n const graphs: Record<string, unknown> = {};\n for (const wrapper of wrappers) {\n graphs[wrapper.plugin.name] = wrapper.getGraph().toObject();\n }\n if (usage) {\n graphs.usedClasses = usage.classes;\n graphs.usedVars = usage.vars;\n }\n\n try {\n writeFileSync(outFile, JSON.stringify(graphs, null, 2), \"utf-8\");\n this.info(`Wrote debug output to ${outFile}`);\n } catch (error) {\n this.warn(`Failed to write debug output to ${outFile}: ${error instanceof Error ? error.message : String(error)}`);\n }\n },\n };\n}\n\ninterface UsageSummary {\n used: string[];\n unused: string[];\n total: number;\n usedCount: number;\n unusedCount: number;\n}\n\nfunction summarize(used: Set<string>, total: Set<string>): UsageSummary {\n const unused = [...total].filter((name) => !used.has(name));\n return { used: [...used], unused, total: total.size, usedCount: used.size, unusedCount: unused.length };\n}\n\ninterface UsageReport {\n classes: UsageSummary;\n vars: UsageSummary;\n usedClasses: Set<string>;\n usedVars: Set<string>;\n}\n\ninterface AnalyzerWrapper {\n plugin: ReturnType<AnalyzerPlugin[\"toRolldownPlugin\"]>;\n getGraph: () => ReturnType<AnalyzerPlugin[\"getGraph\"]>;\n}\n\nfunction reportUsage(wrappers: AnalyzerWrapper[], ctx: { info(msg: string): void }): UsageReport | null {\n const jsxWrapper = wrappers.find((w) => w.plugin.name === \"jsx-graph\");\n const cssWrapper = wrappers.find((w) => w.plugin.name === \"css-graph\");\n if (!jsxWrapper || !cssWrapper) return null;\n\n const jsxNodes = [...jsxWrapper.getGraph().values()].map((n) => toJsxPurgeIR(n as JsxGraphNode));\n const cssRules = [...cssWrapper.getGraph().values()].flatMap((node) => toCssPurgeIR((node as CssGraphNode).rules));\n\n const cssClasses = new Set<string>();\n for (const rule of cssRules) {\n for (const className of rule.classes) cssClasses.add(className);\n }\n\n const usedClasses = extractUsedClasses(jsxNodes, cssClasses);\n const classes = summarize(usedClasses, cssClasses);\n\n const allVars = new Set<string>();\n for (const rule of cssRules) {\n for (const varName of rule.declaredVars) allVars.add(varName);\n for (const varName of rule.referencedVars) allVars.add(varName);\n }\n const usedVars = extractUsedVars(cssRules, usedClasses);\n const vars = summarize(usedVars, allVars);\n\n ctx.info(`[purgeon] CSS classes: ${classes.usedCount} used, ${classes.unusedCount} unused (of ${classes.total} total)`);\n ctx.info(`[purgeon] CSS vars: ${vars.usedCount} used, ${vars.unusedCount} unused (of ${vars.total} total)`);\n\n return { classes, vars, usedClasses, usedVars };\n}\n\nfunction purgeBundle(bundle: OutputBundle, usedClasses: Set<string>, usedVars: Set<string>): 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 output.source = purgeUnusedCss(source, fileName, usedClasses, usedVars);\n }\n}\n\nfunction resolveDebugOutFile(debug?: boolean | { outFile: string }): string | null {\n if (!debug) return null;\n if (typeof debug === \"object\") return debug.outFile;\n return \"graph.json\";\n}\n\nexport interface PluginOptions {\n plugins?: AnalyzerPlugin[];\n debug?: boolean | { outFile: string };\n}\n"],"mappings":";;;;;AASA,SAAgB,QAAQ,UAAyB,CAAC,GAAwB;CACxE,MAAM,YAAY,QAAQ,WAAW,CAAC,EAAA,CACnC,QAAQ,MAAM,WAAW,CAAC,CAAC,CAAC,CAC5B,KAAK,OAAO;EAAE,QAAQ,EAAE,iBAAiB;EAAG,gBAAgB,EAAE,SAAS;CAAE,EAAE;CAC9E,MAAM,UAAU,oBAAoB,QAAQ,KAAK;CACjD,IAAI,QAA4B;CAEhC,OAAO;EACL,MAAM;EACN,SAAS;EAET,UAAU,MAAM,IAAI;GAClB,KAAK,MAAM,WAAW,UACpB,IAAI;IACF,QAAQ,OAAO,UAAU,KAAK,MAAa,MAAM,EAAE;GACrD,SAAS,OAAO;IACd,MAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;IACrE,KAAK,KAAK,IAAI,QAAQ,OAAO,KAAK,sBAAsB,GAAG,I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nB,IAAI,OAAO,UAAU,UAAU,OAAO,MAAM;CAC5C,OAAO;AACT"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@purgeon/rolldown-plugin",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Rolldown/Vite plugin that cross-references JSX usage against CSS graphs and purges unused rules from emitted CSS",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"css",
|
|
7
|
+
"purge",
|
|
8
|
+
"rolldown-plugin",
|
|
9
|
+
"tree-shaking",
|
|
10
|
+
"vite-plugin"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Konstantin Kireyev <https://github.com/knst0>",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/knst0/purgeon.git",
|
|
17
|
+
"directory": "packages/rolldown-plugin"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@purgeon/analyzer-css": "0.1.0",
|
|
34
|
+
"@purgeon/core": "0.1.0",
|
|
35
|
+
"@purgeon/analyzer-jsx": "0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"rolldown": "^1.1.4"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"rolldown": "^1.0.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsdown"
|
|
45
|
+
}
|
|
46
|
+
}
|