Skip to content

Commit

Permalink
perf(postcss-normalize-display-values): increase perf (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jan 30, 2019
1 parent 07948cb commit 33ebbfb
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions packages/postcss-normalize-display-values/src/index.js
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;
});
};
});

0 comments on commit 33ebbfb

Please sign in to comment.