Comparing version 0.2.5 to 0.2.6
#!/usr/bin/env node | ||
var uncss = require('../lib/uncss.js'), | ||
program = require('commander'), | ||
fs = require('fs'), | ||
utility = require('../lib/utility.js'), | ||
options, | ||
callback; | ||
argv, | ||
callback, | ||
compress, | ||
files, | ||
options = {}, | ||
usage = '[options] <file.html file.css ...>'; | ||
/* Parse command line options */ | ||
program | ||
.version('0.3.0') | ||
.usage('[options] <file.html file.css ...>') | ||
.option('-m, --minify', 'Minify css output') | ||
.option('-o, --outfile <file>', 'Redirect output to <file>') | ||
.parse(process.argv); | ||
argv = process.argv.splice(2); | ||
if (program.args.length === 0) { | ||
program.help(); | ||
files = argv.filter(function (str) { | ||
return (/\.css$/).test(str) || | ||
(/\.html$/).test(str) | ||
}); | ||
if (files.length === 0 || | ||
utility.isInCommandLine(argv, '--help')) { | ||
utility.showHelp(); | ||
} | ||
options = { | ||
minify: program.minify | ||
}; | ||
if (program.outfile) { | ||
callback = function (uncss) { | ||
fs.writeFile(program.outfile, uncss, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('uncss: wrote %s', program.outfile); | ||
}); | ||
}; | ||
if (utility.isInCommandLine(argv, '--compress')) { | ||
options.compress = true; | ||
} else { | ||
callback = function (uncss) { | ||
console.log(uncss); | ||
}; | ||
options.compress = false; | ||
} | ||
uncss(program.args, options, callback); | ||
uncss(files, options, function (uncss) { | ||
console.log(uncss); | ||
}); |
@@ -1,9 +0,7 @@ | ||
/*jslint node: true, plusplus: true, stupid: true */ | ||
/*jslint node: true */ | ||
(function () { | ||
"use strict"; | ||
var compressor = require('csso'), | ||
fs = require('fs'), | ||
var css = require('css'), | ||
htmlparser = require('htmlparser'), | ||
parse_css = require('css-parse'), | ||
soupselect = require('soupselect').select, | ||
utility = require('./utility.js'), | ||
@@ -15,9 +13,2 @@ /* Member variables */ | ||
function rule_to_css(decl) { | ||
return decl.map(function (d) { | ||
return '\t' + d.property + ': ' + d.value + ';'; | ||
}).join('\n') + '\n'; | ||
} | ||
/* The bulk of the work happens here */ | ||
function on_htmlparse_complete(error, dom) { | ||
@@ -27,59 +18,8 @@ if (error) { | ||
} else { | ||
/* HTML parsed successfully, proceed with css | ||
* Two steps: remove the unused selectors from the selector array | ||
* filter the rules with no selectors (i.e. the rule was not used) | ||
*/ | ||
var i, | ||
minify, | ||
uncss = '', | ||
used_rules = parsed_css.stylesheet.rules.map(function (rule) { | ||
/* This is the syntax of css-parse | ||
* rule = { | ||
* "selectors": [ ... ], | ||
* "declarations": [ ... ] | ||
* } | ||
* | ||
*/ | ||
if (rule.selectors) { | ||
var used_selectors = rule.selectors.filter(function (selector) { | ||
return (soupselect(dom, selector).length > 0); | ||
}); | ||
/* Case 1: | ||
* rule = { | ||
* "selectors": ['a', 'c'], | ||
* "declarations": [ ... ] | ||
* } | ||
* | ||
* Case 2: | ||
* rule = { | ||
* "selectors": [], | ||
* "declarations": [ ... ] | ||
* } | ||
*/ | ||
if (used_selectors.length > 0) { | ||
/* Case 1 */ | ||
return { | ||
'selectors': used_selectors, | ||
'declarations': rule.declarations | ||
}; | ||
} | ||
} | ||
/* Case 2, or rule isn't a selector array (for example, a comment) */ | ||
return {}; | ||
/* HTML parsed successfully, proceed with the removal of unused rules. */ | ||
var uncss, used_css; | ||
}).filter(function (rule) { | ||
return rule.selectors !== undefined; | ||
}); | ||
used_css = { stylesheet: utility.filterUnusedRules(dom, parsed_css.stylesheet) }; | ||
for (i = 0; i < used_rules.length; i++) { | ||
uncss += used_rules[i].selectors.join(', ') + | ||
' {\n' + | ||
rule_to_css(used_rules[i].declarations) + | ||
'}\n'; | ||
} | ||
/* Compress CSS */ | ||
minify = options.minify || false; | ||
if (minify) { | ||
uncss = compressor.justDoIt(uncss); | ||
} | ||
uncss = css.stringify(used_css, { compress: options.compress || false }); | ||
callback(uncss); | ||
@@ -112,26 +52,11 @@ } | ||
*/ | ||
css_files = files.filter(function (file) { | ||
return (/\.css$/).test(file); | ||
}).map(function (file) { | ||
if (fs.existsSync(file)) { | ||
return fs.readFileSync(file); | ||
} | ||
/* File does not exist */ | ||
throw 'Error: Couldn\'t open "' + file + '"'; | ||
}); | ||
html_files = files.filter(function (file) { | ||
return (/\.html$/).test(file); | ||
}).map(function (file) { | ||
if (fs.existsSync(file)) { | ||
return fs.readFileSync(file); | ||
} | ||
/* File does not exist */ | ||
throw 'Error: Couldn\'t open "' + file + '"'; | ||
}); | ||
css_files = utility.mapReadFile(files, /\.css$/); | ||
html_files = utility.mapReadFile(files, /\.html$/); | ||
/* Concatenate all the stylesheets, and pass them to the css parser. */ | ||
parsed_css = parse_css(css_files.join('\n')); | ||
parsed_css = css.parse(css_files.join('\n')); | ||
/* Init the html parser, and feed it the concatenated html files | ||
* (it works even if it isn't valid html) | ||
*/ | ||
parser_handler = new htmlparser.DefaultHandler(on_htmlparse_complete); | ||
parser_handler = new htmlparser.DefaultHandler(on_htmlparse_complete, { verbose: false, ignoreWhitespace: true }); | ||
parser = new htmlparser.Parser(parser_handler); | ||
@@ -138,0 +63,0 @@ parser.parseComplete(html_files.join('\n')); |
{ | ||
"name": "uncss", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Remove unused CSS styles", | ||
@@ -30,5 +30,3 @@ "main": "lib/uncss.js", | ||
"dependencies": { | ||
"commander": ">= 1.1.1", | ||
"css-parse": ">= 1.2.0", | ||
"csso": ">= 1.3.7", | ||
"css": ">= 1.1.0", | ||
"htmlparser": ">= 1.7.6", | ||
@@ -35,0 +33,0 @@ "soupselect": ">= 0.2.0" |
@@ -18,6 +18,4 @@ # UnCSS # | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-m, --minify Minify css output | ||
-o, --outfile <file> Redirect output to <file> | ||
-h, --help output this message | ||
-c, --compress compress css | ||
@@ -24,0 +22,0 @@ ### Within node: ### |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
1
155185
6453
44
+ Addedcss@>= 1.1.0
+ Addedcss@3.0.0(transitive)
+ Addedsource-map-resolve@0.6.0(transitive)
- Removedcommander@>= 1.1.1
- Removedcss-parse@>= 1.2.0
- Removedcsso@>= 1.3.7
- Removedcommander@12.1.0(transitive)
- Removedcss@2.2.4(transitive)
- Removedcss-parse@2.0.0(transitive)
- Removedcss-tree@2.2.1(transitive)
- Removedcsso@5.0.5(transitive)
- Removedmdn-data@2.0.28(transitive)
- Removedresolve-url@0.2.1(transitive)
- Removedsource-map-js@1.2.1(transitive)
- Removedsource-map-resolve@0.5.3(transitive)
- Removedsource-map-url@0.4.1(transitive)
- Removedurix@0.1.0(transitive)