Skip to content

Commit 6425c98

Browse files
authored
refactor: remove cssnano-utils getMatch (#1279)
* refactor(postcss-normalize-display-values): improve transformation lookup Apply transform by using a map from the original value to the transformed value. Stringify the original value array to use as a key. Refactor the test so it uses an independent mapping, otherwise it will pass even if the mapping in the source code is wrong. Remove cssnano-utils dependency. * refactor(postcss-reduce-transforms): replace cssnano-utils with map lookup * refactor(postcss-normalize-repeat-style): switch lookup from cssnano-utils to a map * refactor(postcss-normalize-timing-functions): replace cssnano-utils with map lookup * feat(cssnano-utils): remove getMatch BREAKING CHANGE: remove getMatch function
1 parent 60ad10b commit 6425c98

File tree

16 files changed

+88
-116
lines changed

16 files changed

+88
-116
lines changed

packages/cssnano-utils/src/__tests__/getMatch.test.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/cssnano-utils/src/getMatch.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/cssnano-utils/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import rawCache from './rawCache.js';
2-
import getMatch from './getMatch.js';
32
import getArguments from './getArguments.js';
43
import sameParent from './sameParent.js';
54

6-
export { rawCache, getMatch, getArguments, sameParent };
5+
export { rawCache, getArguments, sameParent };

packages/postcss-normalize-display-values/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
],
1515
"license": "MIT",
1616
"dependencies": {
17-
"cssnano-utils": "^2.0.1",
1817
"postcss-value-parser": "^4.2.0"
1918
},
2019
"author": {

packages/postcss-normalize-display-values/src/__tests__/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
import { test } from 'uvu';
2-
import mappings from '../lib/map';
3-
import getData from '../../../../util/getData';
42
import {
53
usePostCSSPlugin,
64
processCSSFactory,
75
} from '../../../../util/testHelpers';
86
import plugin from '..';
97

108
const { processCSS, passthroughCSS } = processCSSFactory(plugin);
11-
const data = getData(mappings);
129

1310
test('should pass through "block ruby"', passthroughCSS('display:block ruby;'));
1411

1512
test('should pass through single values', passthroughCSS('display:block;'));
16-
17-
Object.keys(data).forEach((key) => {
18-
const actual = data[key];
19-
const expected = key;
20-
13+
/* source: https://www.w3.org/TR/css-display-3/#the-display-properties */
14+
[
15+
{ input: 'block flow', minified: 'block' },
16+
{ input: 'block flow-root', minified: 'flow-root' },
17+
{ input: 'inline flow', minified: 'inline' },
18+
{ input: 'inline flow-root', minified: 'inline-block' },
19+
{ input: 'run-in flow', minified: 'run-in' },
20+
{ input: 'list-item block flow', minified: 'list-item' },
21+
{ input: 'inline flow list-item', minified: 'inline list-item' },
22+
{ input: 'block flex', minified: 'flex' },
23+
{ input: 'inline flex', minified: 'inline-flex' },
24+
{ input: 'block grid', minified: 'grid' },
25+
{ input: 'inline grid', minified: 'inline-grid' },
26+
{ input: 'inline ruby', minified: 'ruby' },
27+
{ input: 'block table', minified: 'table' },
28+
{ input: 'inline table', minified: 'inline-table' },
29+
{ input: 'table-cell flow', minified: 'table-cell' },
30+
{ input: 'table-caption flow', minified: 'table-caption' },
31+
{ input: 'ruby-base flow', minified: 'ruby-base' },
32+
{ input: 'ruby-text flow', minified: 'ruby-text' },
33+
].forEach((fixture) => {
34+
const { input, minified } = fixture;
2135
test(
22-
`display: ${actual} => display: ${expected}`,
23-
processCSS(`display:${actual}`, `display:${expected}`)
36+
`display: ${input} => display: ${minified}`,
37+
processCSS(`display:${input}`, `display:${minified}`)
2438
);
2539
});
2640

packages/postcss-normalize-display-values/src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import valueParser from 'postcss-value-parser';
2-
import { getMatch } from 'cssnano-utils';
32
import mappings from './lib/map';
43

54
function transform(value) {
@@ -18,7 +17,7 @@ function transform(value) {
1817
return value;
1918
}
2019

21-
const match = getMatch(mappings)(values);
20+
const match = mappings.get(values.toString());
2221

2322
if (!match) {
2423
return value;

packages/postcss-normalize-display-values/src/lib/map.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ const tableCaption = 'table-caption';
2121
* Specification: https://drafts.csswg.org/css-display/#the-display-properties
2222
*/
2323

24-
export default [
25-
[block, [block, flow]],
26-
[flowRoot, [block, flowRoot]],
27-
[inline, [inline, flow]],
28-
[inlineBlock, [inline, flowRoot]],
29-
[runIn, [runIn, flow]],
30-
[listItem, [listItem, block, flow]],
31-
[inline + ' ' + listItem, [inline, flow, listItem]],
32-
[flex, [block, flex]],
33-
[inlineFlex, [inline, flex]],
34-
[grid, [block, grid]],
35-
[inlineGrid, [inline, grid]],
36-
[ruby, [inline, ruby]],
24+
export default new Map([
25+
[[block, flow].toString(), block],
26+
[[block, flowRoot].toString(), flowRoot],
27+
[[inline, flow].toString(), inline],
28+
[[inline, flowRoot].toString(), inlineBlock],
29+
[[runIn, flow].toString(), runIn],
30+
[[listItem, block, flow].toString(), listItem],
31+
[[inline, flow, listItem].toString(), inline + ' ' + listItem],
32+
[[block, flex].toString(), flex],
33+
[[inline, flex].toString(), inlineFlex],
34+
[[block, grid].toString(), grid],
35+
[[inline, grid].toString(), inlineGrid],
36+
[[inline, ruby].toString(), ruby],
3737
// `block ruby` is same
38-
[table, [block, table]],
39-
[inlineTable, [inline, table]],
40-
[tableCell, [tableCell, flow]],
41-
[tableCaption, [tableCaption, flow]],
42-
[rubyBase, [rubyBase, flow]],
43-
[rubyText, [rubyText, flow]],
44-
];
38+
[[block, table].toString(), table],
39+
[[inline, table].toString(), inlineTable],
40+
[[tableCell, flow].toString(), tableCell],
41+
[[tableCaption, flow].toString(), tableCaption],
42+
[[rubyBase, flow].toString(), rubyBase],
43+
[[rubyText, flow].toString(), rubyText],
44+
]);

packages/postcss-normalize-repeat-style/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
],
1515
"license": "MIT",
1616
"dependencies": {
17-
"cssnano-utils": "^2.0.1",
1817
"postcss-value-parser": "^4.2.0"
1918
},
2019
"author": {

packages/postcss-normalize-repeat-style/src/__tests__/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { test } from 'uvu';
2-
import mappings from '../lib/map';
3-
import getData from '../../../../util/getData';
42
import {
53
usePostCSSPlugin,
64
processCSSFactory,
75
} from '../../../../util/testHelpers';
86
import plugin from '..';
97

108
const { processCSS, passthroughCSS } = processCSSFactory(plugin);
11-
const data = getData(mappings);
129

1310
test(
1411
'should pass through two value syntax',
@@ -38,10 +35,17 @@ function suite(fixture, expected) {
3835
]);
3936
}
4037

41-
Object.keys(data).forEach((conversion) => {
42-
const fixture = data[conversion];
43-
44-
test(fixture, suite(fixture, conversion));
38+
[
39+
{ input: 'repeat no-repeat', minified: 'repeat-x' },
40+
{ input: 'no-repeat repeat', minified: 'repeat-y' },
41+
{ input: 'repeat repeat', minified: 'repeat' },
42+
{ input: 'space space', minified: 'space' },
43+
{ input: 'round round', minified: 'round' },
44+
{ input: 'no-repeat no-repeat', minified: 'no-repeat' },
45+
].forEach((conversion) => {
46+
const { input, minified } = conversion;
47+
48+
test(`should convert ${input} to ${minified}`, suite(input, minified));
4549
});
4650

4751
test(

packages/postcss-normalize-repeat-style/src/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import valueParser from 'postcss-value-parser';
2-
import { getMatch as getMatchFactory } from 'cssnano-utils';
32
import mappings from './lib/map';
43

54
function evenValues(list, index) {
65
return index % 2 === 0;
76
}
87

9-
const repeatKeywords = mappings.map((mapping) => mapping[0]);
10-
11-
const getMatch = getMatchFactory(mappings);
8+
const repeatKeywords = [...mappings.values()];
129

1310
function isCommaNode(node) {
1411
return node.type === 'div' && node.value === ',';
@@ -103,10 +100,12 @@ function transform(value) {
103100
if (nodes.length !== 3) {
104101
return;
105102
}
103+
const key = nodes
104+
.filter(evenValues)
105+
.map((n) => n.value.toLowerCase())
106+
.toString();
106107

107-
const match = getMatch(
108-
nodes.filter(evenValues).map((n) => n.value.toLowerCase())
109-
);
108+
const match = mappings.get(key);
110109

111110
if (match) {
112111
nodes[0].value = match;

0 commit comments

Comments
 (0)