-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JIT: Optimize universal selector usage by inlining only the relevant …
…selectors (#4850) * WIP * run prettier * drop new lines in custom matcher Drop all newlines, let prettier handle everything for us. * add cache for the selector parser * add `@apply` tests for the universal optimizer * drop comments * initial replacements * WIP * WIP * MAKE IT WORK * Rename to resolveDefaultsAtRules * Update tests + defaults identifiers * Don't add @defaults in AOT mode Co-authored-by: Robin Malfait <[email protected]>
- Loading branch information
1 parent
369c7b5
commit fe27356
Showing
44 changed files
with
1,172 additions
and
488 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
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,69 @@ | ||
import postcss from 'postcss' | ||
import selectorParser from 'postcss-selector-parser' | ||
|
||
let elementSelectorParser = selectorParser((selectors) => { | ||
return selectors.map((s) => { | ||
return s | ||
.split((n) => n.type === 'combinator') | ||
.pop() | ||
.filter((n) => n.type !== 'pseudo' || n.value.startsWith('::')) | ||
.join('') | ||
.trim() | ||
}) | ||
}) | ||
|
||
let cache = new Map() | ||
|
||
function extractElementSelector(selector) { | ||
if (!cache.has(selector)) { | ||
cache.set(selector, elementSelectorParser.transformSync(selector)) | ||
} | ||
|
||
return cache.get(selector) | ||
} | ||
|
||
export default function resolveDefaultsAtRules() { | ||
return (root) => { | ||
let variableNodeMap = new Map() | ||
let universals = new Set() | ||
|
||
root.walkAtRules('defaults', (rule) => { | ||
if (rule.nodes.length > 0) { | ||
universals.add(rule) | ||
return | ||
} | ||
|
||
let variable = rule.params | ||
if (!variableNodeMap.has(variable)) { | ||
variableNodeMap.set(variable, new Set()) | ||
} | ||
|
||
variableNodeMap.get(variable).add(rule.parent) | ||
|
||
rule.remove() | ||
}) | ||
|
||
for (let universal of universals) { | ||
let selectors = new Set() | ||
|
||
let rules = variableNodeMap.get(universal.params) ?? [] | ||
|
||
for (let rule of rules) { | ||
for (let selector of extractElementSelector(rule.selector)) { | ||
selectors.add(selector) | ||
} | ||
} | ||
|
||
if (selectors.size === 0) { | ||
universal.remove() | ||
continue | ||
} | ||
|
||
let universalRule = postcss.rule() | ||
universalRule.selectors = [...selectors] | ||
universalRule.append(universal.nodes) | ||
universal.before(universalRule) | ||
universal.remove() | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.