Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combined dark/light CSS files without duplicated code #51

Merged
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