Changing front

This commit is contained in:
2023-01-16 17:44:37 +01:00
parent 0b8a93b256
commit 4fe4be7730
48586 changed files with 4725790 additions and 17464 deletions

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2016 Christian Murphy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,148 @@
# Postcss combine duplicated selectors
<!-- current project status -->
[![npm](https://img.shields.io/npm/v/postcss-combine-duplicated-selectors.svg)](https://www.npmjs.com/package/postcss-combine-duplicated-selectors)
[![build status](https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors/workflows/CI/badge.svg)](https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors/actions)
[![dependency status](https://david-dm.org/ChristianMurphy/postcss-combine-duplicated-selectors.svg)](https://david-dm.org/ChristianMurphy/postcss-combine-duplicated-selectors)
[![devDependency status](https://david-dm.org/ChristianMurphy/postcss-combine-duplicated-selectors/dev-status.svg)](https://david-dm.org/ChristianMurphy/postcss-combine-duplicated-selectors?type=dev)
Automatically detects and combines duplicated css selectors so you don't have to
:smile:
## Usage
### Requirements
In order to use this you will need to have [postcss](https://github.com/postcss/postcss) installed. Depending on whether or not you want to use the CLI you need to install [postcss-cli](https://github.com/postcss/postcss-cli).
```bash
npm install --save-dev postcss postcss-combine-duplicated-selectors
# or
yarn add --dev postcss postcss-combine-duplicated-selectors
```
### Using PostCSS JS API
```js
'use strict';
const fs = require('fs');
const postcss = require('postcss');
const css = fs.readFileSync('src/app.css');
postcss([require('postcss-combine-duplicated-selectors')])
.process(css, {from: 'src/app.css', to: 'app.css'})
.then((result) => {
fs.writeFileSync('app.css', result.css);
if (result.map) fs.writeFileSync('app.css.map', result.map);
});
```
### Using PostCSS CLI
```sh
postcss style.css --use postcss-combine-duplicated-selectors --output newcss.css
```
## Example
Input
```css
.module {
color: green;
}
.another-module {
color: blue;
}
.module {
background: red;
}
.another-module {
background: yellow;
}
```
Output
```css
.module {
color: green;
background: red;
}
.another-module {
color: blue;
background: yellow;
}
```
### Duplicated Properties
Duplicated properties can optionally be combined.
Set the `removeDuplicatedProperties` option to `true` to enable.
```js
const postcss = require('postcss');
const combineSelectors = require('postcss-combine-duplicated-selectors');
postcss([combineSelectors({removeDuplicatedProperties: true})]);
```
When enabled the following css
```css
.a {
height: 10px;
background: orange;
background: rgba(255, 165, 0, 0.5);
}
```
will combine into
```css
.a {
height: 10px;
background: rgba(255, 165, 0, 0.5);
}
```
In order to limit this to only combining properties when the values are equal, set the `removeDuplicatedValues` option to `true` instead. This could clean up duplicated properties, but allow for conscious duplicates such as fallbacks for custom properties.
```js
const postcss = require('postcss');
const combineSelectors = require('postcss-combine-duplicated-selectors');
postcss([combineSelectors({removeDuplicatedValues: true})]);
```
This will transform the following css
```css
.a {
height: 10px;
}
.a {
width: 20px;
background: var(--custom-color);
background: rgba(255, 165, 0, 0.5);
}
```
into
```css
.a {
height: 10px;
width: 20px;
background: var(--custom-color);
background: rgba(255, 165, 0, 0.5);
}
```
### Media Queries
If you have code with media queries, pass code through [_postcss-combine-media-query_](https://github.com/SassNinja/postcss-combine-media-query) or [_css-mquery-packer_](https://github.com/n19htz/css-mquery-packer) before _postcss-combine-duplicated-selectors_ to ensure optimal results.

View File

@@ -0,0 +1,123 @@
{
"name": "postcss-combine-duplicated-selectors",
"version": "10.0.3",
"description": "automatically keep css selectors unique",
"main": "src/index.js",
"types": "types/index.d.ts",
"files": [
"src",
"types/index.d.ts"
],
"scripts": {
"commit": "commit",
"test": "run-s test:*",
"test:unit": "ava",
"test:lint-js": "eslint --ext md,js .",
"test:lint-md": "remark *.md -q --no-stdout",
"test:types": "dtslint types",
"commitlint": "commitlint --from HEAD~1"
},
"keywords": [
"postcss-plugin",
"selector"
],
"author": {
"name": "Christian Murphy",
"email": "christian.murphy.42@gmail.com",
"url": "https://github.com/ChristianMurphy"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors.git"
},
"homepage": "https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors",
"bugs": {
"url": "https://github.com/ChristianMurphy/postcss-combine-duplicated-selectors/issues"
},
"license": "MIT",
"peerDependencies": {
"postcss": "^8.1.0"
},
"dependencies": {
"postcss-selector-parser": "^6.0.4"
},
"devDependencies": {
"@commitlint/cli": "12.1.1",
"@commitlint/config-conventional": "12.1.1",
"@commitlint/prompt-cli": "12.1.1",
"ava": "3.15.0",
"dtslint": "4.0.9",
"eslint": "7.26.0",
"eslint-config-google": "0.14.0",
"eslint-plugin-ava": "12.0.0",
"eslint-plugin-markdown": "1.0.2",
"husky": "5.2.0",
"npm-run-all": "4.1.5",
"postcss": "8.2.15",
"postcss-less": "4.0.1",
"postcss-nested": "5.0.5",
"postcss-scss": "3.0.5",
"remark-cli": "9.0.0",
"remark-preset-lint-consistent": "4.0.0",
"remark-preset-lint-recommended": "5.0.0",
"remark-validate-links": "10.0.4",
"typescript": "4.2.4"
},
"engines": {
"node": "^10.0.0 || ^12.0.0 || >=14.0.0"
},
"eslintConfig": {
"root": true,
"parserOptions": {
"ecmaVersion": 8
},
"env": {
"es6": true,
"node": true
},
"plugins": [
"markdown"
],
"extends": [
"eslint:recommended",
"google"
],
"rules": {
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-template": "error"
}
},
"remarkConfig": {
"plugins": [
"preset-lint-recommended",
"preset-lint-consistent",
"validate-links"
]
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"renovate": {
"extends": [
"config:base"
],
"automerge": true,
"major": {
"automerge": false
},
"lockFileMaintenance": {
"enabled": true
},
"semanticPrefix": "chore:",
"semanticCommitScope": ""
},
"husky": {
"hooks": {
"pre-commit": "npm test",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}

View File

@@ -0,0 +1,142 @@
const parser = require('postcss-selector-parser');
const {name} = require('../package.json');
/**
* Ensure that attributes with different quotes match.
* @param {Object} selector - postcss selector node
*/
function normalizeAttributes(selector) {
selector.walkAttributes((node) => {
if (node.value) {
// remove quotes
node.value = node.value.replace(/'|\\'|"|\\"/g, '');
}
});
}
/**
* Sort class and id groups alphabetically
* @param {Object} selector - postcss selector node
*/
function sortGroups(selector) {
selector.each((subSelector) => {
subSelector.nodes.sort((a, b) => {
// different types cannot be sorted
if (a.type !== b.type) {
return 0;
}
// sort alphabetically
return a.value < b.value ? -1 : 1;
});
});
selector.sort((a, b) => (a.nodes.join('') < b.nodes.join('') ? -1 : 1));
}
/**
* Remove duplicated properties
* @param {Object} selector - postcss selector node
* @param {Boolean} exact
*/
function removeDupProperties(selector, exact) {
// Remove duplicated properties from bottom to top ()
for (let actIndex = selector.nodes.length - 1; actIndex >= 1; actIndex--) {
for (let befIndex = actIndex - 1; befIndex >= 0; befIndex--) {
if (selector.nodes[actIndex].prop === selector.nodes[befIndex].prop) {
if (
!exact ||
(exact &&
selector.nodes[actIndex].value === selector.nodes[befIndex].value)
) {
selector.nodes[befIndex].remove();
actIndex--;
}
}
}
}
}
const uniformStyle = parser((selector) => {
normalizeAttributes(selector);
sortGroups(selector);
});
const defaultOptions = {
removeDuplicatedProperties: false,
};
module.exports = (options) => {
options = Object.assign({}, defaultOptions, options);
return {
postcssPlugin: name,
prepare() {
// Create a map to store maps
const mapTable = new Map();
// root map to store root selectors
mapTable.set('root', new Map());
return {
Rule: (rule) => {
let map;
// Check selector parent for any at rule
if (rule.parent.type === 'atrule') {
// Use name and query params as the key
const query =
rule.parent.name.toLowerCase() +
rule.parent.params.replace(/\s+/g, '');
// See if this query key is already in the map table
map = mapTable.has(query) ? // If it is use it
mapTable.get(query) : // if not set it and get it
mapTable.set(query, new Map()).get(query);
} else {
// Otherwise we are dealing with a selector in the root
map = mapTable.get('root');
}
// create a uniform selector
const selector = uniformStyle.processSync(rule.selector, {
lossless: false,
});
if (map.has(selector)) {
// store original rule as destination
const destination = map.get(selector);
// check if node has already been processed
if (destination === rule) return;
// move declarations to original rule
while (rule.nodes.length > 0) {
destination.append(rule.nodes[0]);
}
// remove duplicated rule
rule.remove();
if (
options.removeDuplicatedProperties ||
options.removeDuplicatedValues
) {
removeDupProperties(
destination,
options.removeDuplicatedValues,
);
}
} else {
if (
options.removeDuplicatedProperties ||
options.removeDuplicatedValues
) {
removeDupProperties(rule, options.removeDuplicatedValues);
}
// add new selector to symbol table
map.set(selector, rule);
}
},
};
},
};
};
module.exports.postcss = true;

View File

@@ -0,0 +1,38 @@
// TypeScript Version: 4.0
import { PluginCreator } from "postcss";
declare namespace postcssCombineDuplicatedSelectors {
/**
* Options for postcss-combine-duplicated selectors
*/
type Options =
| {
removeDuplicatedValues?: false;
removeDuplicatedProperties?: false;
}
| {
removeDuplicatedProperties: true;
removeDuplicatedValues?: false;
}
| {
removeDuplicatedProperties?: false;
removeDuplicatedValues: true;
};
/**
* Plugin provides a creator with specific options supported
*/
type Plugin = PluginCreator<Options>;
}
/**
* Automatically detects and combines duplicated css selectors
*
* @example
* ```typescript
* postcss([postcssCombineDuplicatedSelectors()]);
* ```
*/
declare const postcssCombineDuplicatedSelectors: postcssCombineDuplicatedSelectors.Plugin;
export = postcssCombineDuplicatedSelectors;