Last active
October 8, 2015 22:46
-
-
Save alexdiliberto/561480a835edc1ee6376 to your computer and use it in GitHub Desktop.
Mini CSS negator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
README.md | |
1. Copy & paste the input file into third-party.css as raw CSS | |
2. $ node css-negator.js | |
3. When finished, the final copy will be in the file named: negated-output.css | |
*/ | |
var css = require('css'); | |
var fs = require('fs'); | |
var colors = require('colors'); | |
var cleanCSS = require('clean-css'); | |
/** | |
`opts` is an array which can be empty, can contain either of the following two string values, or can contain both of the following two string values: | |
"display-none" | |
"visibility-hidden" | |
setting these flags in the array signals the css negater to ignore `display:none` css key/value pairs or ignore `visibility:hidden` css key/value pairs. | |
*/ | |
var opts = [/*"display-none", "visibility-hidden"*/]; | |
console.log("*** Starting! ***".bold.cyan); | |
var inputFile = fs.readFileSync('third-party.css', 'utf8'); | |
console.log(" * Finished reading the input file"); | |
var ast = css.parse(inputFile); | |
ast.stylesheet.rules = ast.stylesheet.rules.map(function(rule) { | |
if (rule.type === 'rule') { | |
rule.declarations = rule.declarations.map(function(declaration) { | |
if (opts.length && ( | |
(~opts.indexOf('display-none') && declaration.property == 'display' && declaration.value == 'none') || | |
(~opts.indexOf('visibility-hidden') && declaration.property == 'visibility' && declaration.value == 'hidden') | |
)) { | |
console.log(' Skipping declaration.'); | |
} else { | |
declaration.value = 'inherit'; | |
} | |
return declaration; | |
}); | |
} | |
return rule; | |
}); | |
var css = css.stringify(ast); | |
/* Only keep the first special comment when minifying */ | |
var minifiedCSS = new cleanCSS({ keepSpecialComments: 1}).minify(css).styles; | |
fs.writeFile("negated-output.css", minifiedCSS, function(err) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log("*** DONE! The file was saved as negated-output.css ***".bold.green); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "mini-css-negator", | |
"version": "1.0.0", | |
"description": "Mini CSS negator", | |
"main": "css-negator.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Alex DiLiberto <npm@alexdiliberto.com> (http://alexdiliberto.com/)", | |
"license": "MIT", | |
"dependencies": { | |
"clean-css": "^3.4.5", | |
"colors": "^1.1.2", | |
"css": "^2.2.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment