-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
export default function compute() { | ||
|
||
let usedTogether = {}; | ||
|
||
walkRules(ast.stylesheet.rules, rule => { | ||
let props = new Set(); | ||
|
||
if (!rule.declarations) { | ||
return; | ||
} | ||
|
||
let ruleProps = rule.declarations | ||
// Ignore custom properties and prefixed ones. CP because they don't generalize | ||
// and prefixed ones because they will trivially correlate with each other and the non-prefixed version | ||
.filter(d => !d.property.startsWith("-")) | ||
.map(d => d.property); | ||
|
||
for (let prop of ruleProps) { | ||
props.add(prop); | ||
} | ||
|
||
for (let prop of props) { | ||
usedTogether[prop] = usedTogether[prop] || {}; | ||
|
||
for (let prop2 of props) { | ||
if (prop === prop2) { | ||
continue; | ||
} | ||
|
||
usedTogether[prop][prop2] = usedTogether[prop][prop2] || 0; | ||
usedTogether[prop][prop2]++; | ||
} | ||
} | ||
}, { | ||
// rules: r => Boolean(r.declarations), | ||
not: { | ||
type: "font-face" | ||
} | ||
}); | ||
|
||
let ret = {}; | ||
|
||
// Now sort by usage count | ||
for (let prop in usedTogether) { | ||
let obj = usedTogether[prop]; | ||
|
||
// Remove properties that are only used together once | ||
for (let p in obj) { | ||
if (obj[p] === 1) { | ||
delete obj[p]; | ||
} | ||
else { | ||
let key = [prop, p].sort(); | ||
ret[key] = ret[key] || 0; | ||
ret[key] += obj[p]; | ||
} | ||
} | ||
|
||
// let sortedEntries = Object.entries(obj).sort((a, b) => b[1] - a[1]); | ||
// | ||
// if (sortedEntries.length > 0) { | ||
// usedTogether[prop] = Object.fromEntries(sortedEntries); | ||
// } | ||
// else { | ||
// delete usedTogether[prop]; | ||
// } | ||
} | ||
|
||
return sortObject(ret); | ||
|
||
} |