Skip to content

Commit

Permalink
Merge pull request #51 from ForEvolve/experiments/creating-my-own-plu…
Browse files Browse the repository at this point in the history
…gin-to-remove-duplicates

Combined dark/light CSS files without duplicated code
  • Loading branch information
Carl-Hugo committed Dec 8, 2021
2 parents ae6c7dc + c1ea3ee commit 6acb739
Show file tree
Hide file tree
Showing 51 changed files with 101,441 additions and 5,210 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ jobs:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/setup-node@v2
with:
node-version: 14
Expand All @@ -56,20 +55,14 @@ jobs:
stamp: package.json
setCommonVars: true
setAllVars: true

- run: |
echo 'NpmPackageVersion: ${{ steps.nbgv.outputs.NpmPackageVersion }}'
- run: npm install
if: github.event_name == 'push'

- run: npm run build
if: github.event_name == 'push'

- run: npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: github.event_name == 'push'

- uses: actions/create-release@v1
env:
Expand All @@ -78,5 +71,4 @@ jobs:
tag_name: v${{ steps.nbgv.outputs.NpmPackageVersion }}
release_name: v${{ steps.nbgv.outputs.NpmPackageVersion }} Release
draft: false
prerelease: false
if: github.event_name == 'push'
prerelease: ${{ github.event_name != 'push' }}
69 changes: 69 additions & 0 deletions clean_non_color_attr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Based on https://github.com/dinhani/gulp-css-remove-attributes/blob/master/index.js
//
const through = require('through2');
const css = require('css');

const PLUGIN_NAME = 'gulp-css-clean-non-color-attributes';
module.exports = function (options) {
const attributesNotToRemove = [
'color',
'background',
'background-color',
'background-image',
'border',
'border-color',
'border-top',
'border-right',
'border-bottom',
'border-left',
'box-shadow',
'outline-color',
'text-shadow',
];

// INPUT
function parseInputCss(inputFile, encoding, options) {
let fileContent = inputFile.contents.toString(encoding);
let parsedCss = css.parse(fileContent, options);
return parsedCss;
}

// PARSING
function removeCssAttributes(parsedCss) {
parsedCss.stylesheet.rules = mapRules(parsedCss.stylesheet.rules);
return parsedCss;
}

function mapRules(rules) {
return rules.map((rule) => {
// only filter rules, the other types are just kept
if (rule.type === 'rule') {
rule.declarations = rule.declarations.filter((declaration) => attributesNotToRemove.includes(declaration.property));
}
if (rule.type === 'media') {
rule.rules = mapRules(rule.rules);
}
return rule;
});
}

// OUTPUT
function outputFinalCss(modifiedCss, options) {
return css.stringify(modifiedCss, options);
}

// MAIN
let transform = function (file, encoding, callback) {
let parsedCss = parseInputCss(file, encoding, options);
let modifiedCss = removeCssAttributes(parsedCss, attributesNotToRemove);
let finalCss = outputFinalCss(modifiedCss, options);
file.contents = Buffer.from(finalCss);

// success
callback(null, file);
};

//
return through.obj(transform);
};
Loading

0 comments on commit 6acb739

Please sign in to comment.