vite-plugin-token-shaker 0.0.1
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/README.md +23 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +192 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# tsdown-starter
|
|
2
|
+
|
|
3
|
+
A starter for creating a TypeScript package.
|
|
4
|
+
|
|
5
|
+
## Development
|
|
6
|
+
|
|
7
|
+
- Install dependencies:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- Run the unit tests:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm run test
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
- Build the library:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm run build
|
|
23
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Plugin } from "vite";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface PluginOptions {
|
|
5
|
+
/** Prefix for mangled names (default: "--_") */
|
|
6
|
+
manglePrefix?: string;
|
|
7
|
+
/** Enable verbose logging (default: false) */
|
|
8
|
+
verbose?: boolean;
|
|
9
|
+
}
|
|
10
|
+
declare function tokenShaker(options?: PluginOptions): Plugin;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { tokenShaker };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
const VAR_REF_REGEX = /var\(\s*(--[\w-]+)(?:\s*,\s*([^)]+))?\s*\)/g;
|
|
3
|
+
const VAR_DEF_REGEX = /(--[\w-]+)\s*:\s*([^;{}]+);?/g;
|
|
4
|
+
const LAYER_TOKENS_REGEX = /@layer\s+tokens\s*\{/g;
|
|
5
|
+
const VAR_ONLY_REGEX = /^\s*var\(\s*--[\w-]+\s*(?:,\s*[^)]+)?\)\s*$/;
|
|
6
|
+
function findMatchingBrace(code, start) {
|
|
7
|
+
let depth = 1;
|
|
8
|
+
for (let i = start; i < code.length; i++) {
|
|
9
|
+
if (code[i] == "{") depth++;
|
|
10
|
+
else if (code[i] == "}") depth--;
|
|
11
|
+
if (depth == 0) return i;
|
|
12
|
+
}
|
|
13
|
+
return code.length;
|
|
14
|
+
}
|
|
15
|
+
function escapeRegex(str) {
|
|
16
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
17
|
+
}
|
|
18
|
+
function findTokensLayers(code) {
|
|
19
|
+
const layers = [];
|
|
20
|
+
for (const match of code.matchAll(LAYER_TOKENS_REGEX)) {
|
|
21
|
+
const startIdx = match.index;
|
|
22
|
+
const contentStart = startIdx + match[0].length;
|
|
23
|
+
const contentEnd = findMatchingBrace(code, contentStart);
|
|
24
|
+
layers.push({
|
|
25
|
+
start: startIdx,
|
|
26
|
+
end: contentEnd + 1,
|
|
27
|
+
content: code.slice(contentStart, contentEnd)
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return layers;
|
|
31
|
+
}
|
|
32
|
+
function extractTokensVariables(code, registry) {
|
|
33
|
+
for (const layer of findTokensLayers(code)) for (const match of layer.content.matchAll(VAR_DEF_REGEX)) {
|
|
34
|
+
const [, varName, varValueRaw] = match;
|
|
35
|
+
const varValue = varValueRaw.trim();
|
|
36
|
+
const isAlias = VAR_ONLY_REGEX.test(varValue);
|
|
37
|
+
if (!registry.has(varName)) registry.set(varName, {
|
|
38
|
+
value: varValue,
|
|
39
|
+
usageCount: 0,
|
|
40
|
+
setElsewhere: false,
|
|
41
|
+
isAlias,
|
|
42
|
+
emitDeclaration: false
|
|
43
|
+
});
|
|
44
|
+
else {
|
|
45
|
+
const v = registry.get(varName);
|
|
46
|
+
if (v.isAlias == void 0) v.isAlias = isAlias;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function markVariablesSetElsewhere(code, registry) {
|
|
51
|
+
let codeWithoutTokens = code;
|
|
52
|
+
for (const layer of findTokensLayers(code)) codeWithoutTokens = codeWithoutTokens.slice(0, layer.start) + " ".repeat(layer.end - layer.start) + codeWithoutTokens.slice(layer.end);
|
|
53
|
+
for (const [varName, variable] of registry) if (new RegExp(`${escapeRegex(varName)}\\s*:`, "g").test(codeWithoutTokens)) variable.setElsewhere = true;
|
|
54
|
+
}
|
|
55
|
+
function countVariableUsage(code, registry, visited = /* @__PURE__ */ new Set()) {
|
|
56
|
+
for (const match of code.matchAll(VAR_REF_REGEX)) {
|
|
57
|
+
const varName = match[1];
|
|
58
|
+
const variable = registry.get(varName);
|
|
59
|
+
if (variable) {
|
|
60
|
+
variable.usageCount++;
|
|
61
|
+
if (!visited.has(varName)) {
|
|
62
|
+
visited.add(varName);
|
|
63
|
+
countVariableUsage(variable.value, registry, visited);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function resetUsageCounts(registry) {
|
|
69
|
+
for (const variable of registry.values()) {
|
|
70
|
+
variable.usageCount = 0;
|
|
71
|
+
variable.mangledName = void 0;
|
|
72
|
+
variable.emitDeclaration = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function resolveVariableValue(varName, registry, depth = 0) {
|
|
76
|
+
if (depth > 10) return `var(${varName})`;
|
|
77
|
+
const variable = registry.get(varName);
|
|
78
|
+
if (!variable) return `var(${varName})`;
|
|
79
|
+
return variable.value.replace(VAR_REF_REGEX, (_, nestedVarName) => resolveVariableValue(nestedVarName, registry, depth + 1));
|
|
80
|
+
}
|
|
81
|
+
function generateMangledNames(registry, manglePrefix) {
|
|
82
|
+
const valueToVars = /* @__PURE__ */ new Map();
|
|
83
|
+
for (const [varName, variable] of registry) {
|
|
84
|
+
if (variable.usageCount == 0) continue;
|
|
85
|
+
const resolvedValue = resolveVariableValue(varName, registry);
|
|
86
|
+
const vars = valueToVars.get(resolvedValue);
|
|
87
|
+
if (vars) vars.push(varName);
|
|
88
|
+
else valueToVars.set(resolvedValue, [varName]);
|
|
89
|
+
}
|
|
90
|
+
let counter = 0;
|
|
91
|
+
for (const [resolvedValue, varNames] of valueToVars) {
|
|
92
|
+
const canonicalVarName = varNames.find((v) => !registry.get(v).isAlias) ?? varNames[0];
|
|
93
|
+
let totalUsage = 0;
|
|
94
|
+
for (const vName of varNames) totalUsage += registry.get(vName).usageCount;
|
|
95
|
+
const mangledName = `${manglePrefix}${counter.toString(36)}`;
|
|
96
|
+
const valueLen = resolvedValue.length;
|
|
97
|
+
const declarationCost = mangledName.length + 2 + valueLen + 1;
|
|
98
|
+
const referenceCost = 5 + mangledName.length + 1;
|
|
99
|
+
if (declarationCost + totalUsage * referenceCost < totalUsage * valueLen) {
|
|
100
|
+
counter++;
|
|
101
|
+
for (const vName of varNames) {
|
|
102
|
+
const v = registry.get(vName);
|
|
103
|
+
v.mangledName = mangledName;
|
|
104
|
+
v.emitDeclaration = vName == canonicalVarName;
|
|
105
|
+
}
|
|
106
|
+
} else for (const vName of varNames) {
|
|
107
|
+
const v = registry.get(vName);
|
|
108
|
+
v.mangledName = void 0;
|
|
109
|
+
v.emitDeclaration = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return counter;
|
|
113
|
+
}
|
|
114
|
+
function transformCode(code, registry) {
|
|
115
|
+
let result = code;
|
|
116
|
+
const layers = findTokensLayers(code);
|
|
117
|
+
const emittedValues = /* @__PURE__ */ new Set();
|
|
118
|
+
for (const layer of layers.reverse()) {
|
|
119
|
+
const declarations = [];
|
|
120
|
+
for (const match of layer.content.matchAll(VAR_DEF_REGEX)) {
|
|
121
|
+
const [, varName, varValue] = match;
|
|
122
|
+
const variable = registry.get(varName);
|
|
123
|
+
if (!variable) continue;
|
|
124
|
+
if (variable.usageCount == 0) continue;
|
|
125
|
+
if (variable.setElsewhere) {
|
|
126
|
+
declarations.push(`${varName}: ${varValue}`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (variable.mangledName) {
|
|
130
|
+
const resolvedValue = resolveVariableValue(varName, registry);
|
|
131
|
+
if (variable.emitDeclaration && !variable.isAlias) {
|
|
132
|
+
if (!emittedValues.has(resolvedValue)) {
|
|
133
|
+
declarations.push(`${variable.mangledName}: ${resolvedValue}`);
|
|
134
|
+
emittedValues.add(resolvedValue);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const newContent = declarations.length > 0 ? `@layer tokens{:root{${declarations.join(";")}}}` : "";
|
|
140
|
+
result = result.slice(0, layer.start) + newContent + result.slice(layer.end);
|
|
141
|
+
}
|
|
142
|
+
result = result.replace(VAR_REF_REGEX, (original, varName, fallback) => {
|
|
143
|
+
const variable = registry.get(varName);
|
|
144
|
+
if (!variable) return original;
|
|
145
|
+
if (variable.mangledName) return `var(${variable.mangledName}${fallback ? `, ${fallback}` : ""})`;
|
|
146
|
+
if (!variable.setElsewhere) return resolveVariableValue(varName, registry);
|
|
147
|
+
return original;
|
|
148
|
+
});
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
function getStats(registry, mangledCount) {
|
|
152
|
+
let drop = 0, inline = 0, mangle = 0, keep = 0;
|
|
153
|
+
for (const variable of registry.values()) if (variable.usageCount == 0) drop++;
|
|
154
|
+
else if (variable.setElsewhere) keep++;
|
|
155
|
+
else if (variable.mangledName) mangle++;
|
|
156
|
+
else inline++;
|
|
157
|
+
return `Analysis: ${drop} drop, ${inline} inline, ${mangle} mangle (${mangledCount} unique values), ${keep} keep`;
|
|
158
|
+
}
|
|
159
|
+
function tokenShaker(options = {}) {
|
|
160
|
+
const { manglePrefix = "--_", verbose = false } = options;
|
|
161
|
+
const registry = /* @__PURE__ */ new Map();
|
|
162
|
+
const log = verbose ? (...args) => console.log("[token-shaker]", ...args) : () => {};
|
|
163
|
+
return {
|
|
164
|
+
name: "vite-plugin-token-shaker",
|
|
165
|
+
enforce: "pre",
|
|
166
|
+
generateBundle(_outputOptions, bundle) {
|
|
167
|
+
registry.clear();
|
|
168
|
+
const cssAssets = Object.entries(bundle).filter(([name, asset]) => asset.type == "asset" && name.endsWith(".css"));
|
|
169
|
+
if (cssAssets.length == 0) return;
|
|
170
|
+
for (const [, asset] of cssAssets) extractTokensVariables(asset.source, registry);
|
|
171
|
+
if (registry.size == 0) return;
|
|
172
|
+
const bundledFiles = cssAssets.map(([name, asset]) => [name, asset.source]);
|
|
173
|
+
log(`Analyzing ${registry.size} variables across ${cssAssets.length} CSS files`);
|
|
174
|
+
resetUsageCounts(registry);
|
|
175
|
+
for (const [, code] of bundledFiles) {
|
|
176
|
+
markVariablesSetElsewhere(code, registry);
|
|
177
|
+
countVariableUsage(code, registry);
|
|
178
|
+
}
|
|
179
|
+
log(getStats(registry, generateMangledNames(registry, manglePrefix)));
|
|
180
|
+
for (const [fileName, asset] of cssAssets) {
|
|
181
|
+
const transformed = transformCode(asset.source, registry);
|
|
182
|
+
if (transformed !== asset.source) {
|
|
183
|
+
log(`✓ Transformed ${fileName}`);
|
|
184
|
+
asset.source = transformed;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
export { tokenShaker };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-token-shaker",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Clean up anything in @layer tokens",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/KTibow/vite-plugin-token-shaker#readme",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/KTibow/vite-plugin-token-shaker/issues"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/KTibow/vite-plugin-token-shaker.git"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"main": "./dist/index.mjs",
|
|
19
|
+
"module": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.mjs",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^24.10.1",
|
|
27
|
+
"tsdown": "^0.16.4",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"vite": "^7.2.4"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"vite": "^7.2.4"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsdown",
|
|
36
|
+
"dev": "tsdown --watch",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|