forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframeworkUsage.ts
More file actions
33 lines (30 loc) · 1.27 KB
/
Copy pathframeworkUsage.ts
File metadata and controls
33 lines (30 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Framework class usage — which framework-generated utility classes are
* actually assigned to nodes anywhere in the site.
*
* Reconcile keys every framework-generated class under the `framework:` id
* prefix. This collector is shared by the publisher's tree-shake (drop unused
* classes from published CSS) and the editor's "remove unused framework
* classes" action (prune the tokens/generators that produce only-unused
* classes).
*/
import type { SiteDocument } from '@core/page-tree'
const FRAMEWORK_ID_PREFIX = 'framework:'
function addFrameworkClassIds(target: Set<string>, classIds: string[] | undefined): void {
if (!classIds) return
for (const id of classIds) {
if (id.startsWith(FRAMEWORK_ID_PREFIX)) target.add(id)
}
}
/** Every framework-generated class id assigned to a node (pages + Visual Components). */
export function collectUsedFrameworkClassIds(site: SiteDocument): Set<string> {
const used = new Set<string>()
for (const page of site.pages) {
for (const node of Object.values(page.nodes)) addFrameworkClassIds(used, node.classIds)
}
for (const vc of site.visualComponents) {
addFrameworkClassIds(used, vc.classIds)
for (const node of Object.values(vc.tree.nodes)) addFrameworkClassIds(used, node.classIds)
}
return used
}