postcss-unique-selectors 5.0.3 → 5.1.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 +5 -7
- package/src/index.js +59 -0
- package/types/index.d.ts +9 -0
- package/dist/index.js +0 -51
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-unique-selectors",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.1",
|
|
4
4
|
"description": "Ensure CSS selectors are unique.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"LICENSE-MIT",
|
|
8
|
-
"
|
|
9
|
+
"src",
|
|
10
|
+
"types"
|
|
9
11
|
],
|
|
10
12
|
"keywords": [
|
|
11
13
|
"css",
|
|
@@ -35,9 +37,5 @@
|
|
|
35
37
|
"peerDependencies": {
|
|
36
38
|
"postcss": "^8.2.15"
|
|
37
39
|
},
|
|
38
|
-
"scripts": {
|
|
39
|
-
"prebuild": "rimraf dist",
|
|
40
|
-
"build": "babel src --config-file ../../babel.config.json --out-dir dist --ignore \"**/__tests__/\""
|
|
41
|
-
},
|
|
42
40
|
"readme": "# [postcss][postcss]-unique-selectors\n\n> Ensure CSS selectors are unique.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-unique-selectors) do:\n\n```\nnpm install postcss-unique-selectors --save\n```\n\n## Example\n\nSelectors are sorted naturally, and deduplicated:\n\n### Input\n\n```css\nh1,h3,h2,h1 {\n color: red\n}\n```\n\n### Output\n\n```css\nh1,h2,h3 {\n color: red\n}\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
|
|
43
41
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const selectorParser = require('postcss-selector-parser');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} selectors
|
|
6
|
+
* @param {selectorParser.SyncProcessor<void>} callback
|
|
7
|
+
* @return {string}
|
|
8
|
+
*/
|
|
9
|
+
function parseSelectors(selectors, callback) {
|
|
10
|
+
return selectorParser(callback).processSync(selectors);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {import('postcss').Rule} rule
|
|
15
|
+
* @return {string}
|
|
16
|
+
*/
|
|
17
|
+
function unique(rule) {
|
|
18
|
+
const selector = [...new Set(rule.selectors)];
|
|
19
|
+
selector.sort();
|
|
20
|
+
return selector.join();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @type {import('postcss').PluginCreator<void>}
|
|
25
|
+
* @return {import('postcss').Plugin}
|
|
26
|
+
*/
|
|
27
|
+
function pluginCreator() {
|
|
28
|
+
return {
|
|
29
|
+
postcssPlugin: 'postcss-unique-selectors',
|
|
30
|
+
OnceExit(css) {
|
|
31
|
+
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
|
+
if (nodes.raws.selector && nodes.raws.selector.raw) {
|
|
47
|
+
parseSelectors(nodes.raws.selector.raw, removeAndSaveComments);
|
|
48
|
+
nodes.raws.selector.raw = unique(nodes);
|
|
49
|
+
}
|
|
50
|
+
nodes.selector = parseSelectors(nodes.selector, removeAndSaveComments);
|
|
51
|
+
nodes.selector = unique(nodes);
|
|
52
|
+
nodes.selectors = nodes.selectors.concat(comments);
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
pluginCreator.postcss = true;
|
|
59
|
+
module.exports = pluginCreator;
|
package/types/index.d.ts
ADDED
package/dist/index.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
|
-
function parseSelectors(selectors, callback) {
|
|
13
|
-
return (0, _postcssSelectorParser.default)(callback).processSync(selectors);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function unique(rule) {
|
|
17
|
-
const selector = [...new Set(rule.selectors)];
|
|
18
|
-
selector.sort();
|
|
19
|
-
rule.selector = selector.join();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function pluginCreator() {
|
|
23
|
-
return {
|
|
24
|
-
postcssPlugin: 'postcss-unique-selectors',
|
|
25
|
-
|
|
26
|
-
OnceExit(css) {
|
|
27
|
-
css.walkRules(nodes => {
|
|
28
|
-
let comments = [];
|
|
29
|
-
nodes.selector = parseSelectors(nodes.selector, selNode => {
|
|
30
|
-
selNode.walk(sel => {
|
|
31
|
-
if (sel.type === 'comment') {
|
|
32
|
-
comments.push(sel.value);
|
|
33
|
-
sel.remove();
|
|
34
|
-
return;
|
|
35
|
-
} else {
|
|
36
|
-
return sel;
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
unique(nodes);
|
|
41
|
-
nodes.selectors = nodes.selectors.concat(comments);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
pluginCreator.postcss = true;
|
|
49
|
-
var _default = pluginCreator;
|
|
50
|
-
exports.default = _default;
|
|
51
|
-
module.exports = exports.default;
|