vite-plugin-token-shaker 0.0.5 → 0.0.6
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/dist/index.mjs +9 -5
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -29,6 +29,11 @@ function findTokensLayers(code) {
|
|
|
29
29
|
}
|
|
30
30
|
return layers;
|
|
31
31
|
}
|
|
32
|
+
function blankOutTokensLayers(code) {
|
|
33
|
+
let result = code;
|
|
34
|
+
for (const layer of findTokensLayers(code)) result = result.slice(0, layer.start) + " ".repeat(layer.end - layer.start) + result.slice(layer.end);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
32
37
|
function extractTokensVariables(code, registry) {
|
|
33
38
|
for (const layer of findTokensLayers(code)) for (const match of layer.content.matchAll(VAR_DEF_REGEX)) {
|
|
34
39
|
const [, varName, varValueRaw] = match;
|
|
@@ -47,9 +52,7 @@ function extractTokensVariables(code, registry) {
|
|
|
47
52
|
}
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
|
-
function
|
|
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);
|
|
55
|
+
function markDeoptimized(codeWithoutTokens, registry) {
|
|
53
56
|
for (const [varName, variable] of registry) if (new RegExp(`${escapeRegex(varName)}\\s*:`, "g").test(codeWithoutTokens)) variable.deoptimized = true;
|
|
54
57
|
}
|
|
55
58
|
function countVariableUsage(code, registry, visited = /* @__PURE__ */ new Set()) {
|
|
@@ -175,8 +178,9 @@ function tokenShaker(options = {}) {
|
|
|
175
178
|
log(`Analyzing ${registry.size} variables across ${cssAssets.length} CSS files`);
|
|
176
179
|
resetUsageCounts(registry);
|
|
177
180
|
for (const [, code] of bundledFiles) {
|
|
178
|
-
|
|
179
|
-
|
|
181
|
+
const codeWithoutTokens = blankOutTokensLayers(code);
|
|
182
|
+
markDeoptimized(codeWithoutTokens, registry);
|
|
183
|
+
countVariableUsage(codeWithoutTokens, registry);
|
|
180
184
|
}
|
|
181
185
|
for (const chunk of Object.values(bundle)) {
|
|
182
186
|
if (chunk.type != "chunk") continue;
|