postcss-unique-selectors 7.0.0 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/index.js +40 -32
- package/types/index.d.ts.map +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-unique-selectors",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.1",
|
|
4
4
|
"description": "Ensure CSS selectors are unique.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"repository": "cssnano/cssnano",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"postcss-selector-parser": "^6.0
|
|
26
|
+
"postcss-selector-parser": "^6.1.0"
|
|
27
27
|
},
|
|
28
28
|
"bugs": {
|
|
29
29
|
"url": "https://github.com/cssnano/cssnano/issues"
|
package/src/index.js
CHANGED
|
@@ -3,23 +3,45 @@ const selectorParser = require('postcss-selector-parser');
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @param {string} selectors
|
|
6
|
-
* @param {selectorParser.SyncProcessor<void>} callback
|
|
7
6
|
* @return {string}
|
|
8
7
|
*/
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
function generateUniqueSelector(selectors) {
|
|
9
|
+
/** @type {Map<string, string>} */
|
|
10
|
+
const uniqueSelectors = new Map();
|
|
12
11
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
/** @type {selectorParser.SyncProcessor<void>} */
|
|
13
|
+
const collectUniqueSelectors = (selNode) => {
|
|
14
|
+
for (const node of selNode.nodes) {
|
|
15
|
+
/** @type {string[]} */
|
|
16
|
+
const comments = [];
|
|
17
|
+
|
|
18
|
+
// Duplicates are removed by stripping the comments and using the results as the Map key.
|
|
19
|
+
const keyNode = node.clone();
|
|
20
|
+
keyNode.walk((sel) => {
|
|
21
|
+
if (sel.type === 'comment') {
|
|
22
|
+
comments.push(sel.value);
|
|
23
|
+
sel.remove();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const key = keyNode.toString().trim();
|
|
27
|
+
|
|
28
|
+
const dupeSelector = uniqueSelectors.get(key);
|
|
29
|
+
if (!dupeSelector) {
|
|
30
|
+
uniqueSelectors.set(key, node.toString());
|
|
31
|
+
} else if (comments.length) {
|
|
32
|
+
// If the duplicate selector has a comment, it is concatenated to the end of the selector.
|
|
33
|
+
uniqueSelectors.set(key, `${dupeSelector}${comments.join('')}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
selectorParser(collectUniqueSelectors).processSync(selectors);
|
|
22
39
|
|
|
40
|
+
return [...uniqueSelectors.entries()]
|
|
41
|
+
.sort(([a], [b]) => (a > b ? 1 : a < b ? -1 : 0))
|
|
42
|
+
.map(([, selector]) => selector)
|
|
43
|
+
.join();
|
|
44
|
+
}
|
|
23
45
|
/**
|
|
24
46
|
* @type {import('postcss').PluginCreator<void>}
|
|
25
47
|
* @return {import('postcss').Plugin}
|
|
@@ -29,27 +51,13 @@ function pluginCreator() {
|
|
|
29
51
|
postcssPlugin: 'postcss-unique-selectors',
|
|
30
52
|
OnceExit(css) {
|
|
31
53
|
css.walkRules((nodes) => {
|
|
32
|
-
/** @type {string[]} */
|
|
33
|
-
let comments = [];
|
|
34
|
-
/** @type {selectorParser.SyncProcessor<void>} */
|
|
35
|
-
const removeAndSaveComments = (selNode) => {
|
|
36
|
-
selNode.walk((sel) => {
|
|
37
|
-
if (sel.type === 'comment') {
|
|
38
|
-
comments.push(sel.value);
|
|
39
|
-
sel.remove();
|
|
40
|
-
return;
|
|
41
|
-
} else {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
54
|
if (nodes.raws.selector && nodes.raws.selector.raw) {
|
|
47
|
-
|
|
48
|
-
|
|
55
|
+
nodes.raws.selector.raw = generateUniqueSelector(
|
|
56
|
+
nodes.raws.selector.raw
|
|
57
|
+
);
|
|
58
|
+
} else {
|
|
59
|
+
nodes.selector = generateUniqueSelector(nodes.selector);
|
|
49
60
|
}
|
|
50
|
-
nodes.selector = parseSelectors(nodes.selector, removeAndSaveComments);
|
|
51
|
-
nodes.selector = unique(nodes);
|
|
52
|
-
nodes.selectors = nodes.selectors.concat(comments);
|
|
53
61
|
});
|
|
54
62
|
},
|
|
55
63
|
};
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AA4CA;;;GAGG;AACH,kCAFY,OAAO,SAAS,EAAE,MAAM,CAiBnC"}
|