From a243badd2ecc2e4424d9e53c41bedd3cfa9e9507 Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 30 Jan 2019 15:04:53 +0300 Subject: [PATCH] perf(postcss-normalize-display-values): increase perf --- .../src/index.js | 56 +++++++++++++------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/postcss-normalize-display-values/src/index.js b/packages/postcss-normalize-display-values/src/index.js index 17212cd31..21f56aa78 100644 --- a/packages/postcss-normalize-display-values/src/index.js +++ b/packages/postcss-normalize-display-values/src/index.js @@ -1,7 +1,7 @@ -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); @@ -9,17 +9,41 @@ 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; + }); + }; });