-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(postcss-normalize-display-values): increase perf (#695)
- Loading branch information
1 parent
07948cb
commit 33ebbfb
Showing
1 changed file
with
40 additions
and
16 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 |
---|---|---|
@@ -1,25 +1,49 @@ | ||
import postcss from 'postcss'; | ||
import valueParser from 'postcss-value-parser'; | ||
import getMatchFactory from 'lerna:cssnano-util-get-match'; | ||
import mappings from './lib/map'; | ||
import postcss from "postcss"; | ||
import valueParser from "postcss-value-parser"; | ||
import getMatchFactory from "lerna:cssnano-util-get-match"; | ||
import mappings from "./lib/map"; | ||
|
||
const getMatch = getMatchFactory(mappings); | ||
|
||
function evenValues (list, index) { | ||
return index % 2 === 0; | ||
} | ||
|
||
function transform (node) { | ||
const {nodes} = valueParser(node.value); | ||
if (nodes.length === 1) { | ||
return; | ||
} | ||
const match = getMatch(nodes.filter(evenValues).map(n => n.value.toLowerCase())); | ||
if (match) { | ||
node.value = match; | ||
} | ||
} | ||
export default postcss.plugin("postcss-normalize-display-values", () => { | ||
return css => { | ||
const cache = {}; | ||
|
||
css.walkDecls(/display/i, decl => { | ||
const value = decl.value; | ||
|
||
if (cache[value]) { | ||
decl.value = cache[value]; | ||
|
||
return; | ||
} | ||
|
||
const {nodes} = valueParser(value); | ||
|
||
if (nodes.length === 1) { | ||
cache[value] = value; | ||
|
||
return; | ||
} | ||
|
||
const match = getMatch( | ||
nodes.filter(evenValues).map(n => n.value.toLowerCase()) | ||
); | ||
|
||
if (!match) { | ||
cache[value] = value; | ||
|
||
return; | ||
} | ||
|
||
const result = match; | ||
|
||
export default postcss.plugin('postcss-normalize-display-values', () => { | ||
return css => css.walkDecls(/display/i, transform); | ||
decl.value = result; | ||
cache[value] = result; | ||
}); | ||
}; | ||
}); |