Comparing version 0.5.5 to 0.6.0
@@ -17,2 +17,3 @@ #!/usr/bin/env node | ||
.option('-s, --stylesheets <file, ...>', 'Specify additional stylesheets to process') | ||
.option('-t, --timeout <milliseconds>', 'Wait for JS evaluation') | ||
.parse(process.argv); | ||
@@ -19,0 +20,0 @@ |
@@ -5,5 +5,47 @@ /*jslint node: true, plusplus: true, stupid: true */ | ||
var fs = require('fs'); | ||
var async = require('async'), | ||
child_process = require('child_process'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
phantom = require('phantomjs'); | ||
/** | ||
* Run a page through phantomjs | ||
* @param {String} Filename The name of the HTML page | ||
* @param {Function} Callback | ||
* @return {String} The contents of the files, as seen by Phantom | ||
*/ | ||
function phantom_eval(filename, timeout, callback) { | ||
var childArgs = [ | ||
path.join(__dirname, 'phantom-script.js'), | ||
filename, | ||
timeout | ||
], | ||
page, | ||
buffer = '', | ||
error = ''; | ||
page = child_process.spawn(phantom.path, childArgs); | ||
page.stdout.setEncoding('utf8'); | ||
page.stderr.setEncoding('utf8'); | ||
page.stdout.on('data', function (data) { | ||
buffer += data; | ||
}); | ||
page.stderr.on('data', function (data) { | ||
error += data; | ||
}); | ||
page.on('close', function (code) { | ||
if (code === 0 && error === '') { | ||
callback(buffer); | ||
} else { | ||
console.log(error); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
/** | ||
* Given an array of filenames, return an array of the files' contents, | ||
@@ -54,3 +96,3 @@ * only if the filename matches a regex | ||
return selectors.filter(function (selector) { | ||
var match, i, temp; | ||
var match, i, j, temp; | ||
/* Don't process @-rules (for now?) */ | ||
@@ -63,2 +105,10 @@ if (selector[0] === '@') { | ||
} | ||
// Issue #7 check regex ignores | ||
for (j = 0; j < ignore.length; ++j) { | ||
// if ignore is RegExp and matches selector ... | ||
if (toString.call(ignore[j]) === '[object RegExp]' && ignore[j].test(selector)){ | ||
return true; | ||
} | ||
} | ||
/* For each DOM, match the selector */ | ||
@@ -138,4 +188,5 @@ for (i = 0; i < doms.length; ++i) { | ||
module.exports.phantom_eval = phantom_eval; | ||
module.exports.mapReadFiles = mapReadFiles; | ||
module.exports.extract_stylesheets = extract_stylesheets; | ||
module.exports.filterUnusedRules = filterUnusedRules; |
/*jslint node: true */ | ||
"use strict"; | ||
var css = require('css'), | ||
var async = require('async'), | ||
css = require('css'), | ||
csso = require('csso'), | ||
@@ -12,37 +13,13 @@ cheerio = require('cheerio'), | ||
/** | ||
* Main exposed function | ||
* @param {Array} files array of filenames | ||
* @param {Object} opt options | ||
* @param {Function} cb callback | ||
* @return {String} uncss'd css | ||
* This function does all the heavy-lifting | ||
* @param {[type]} files List of HTML files | ||
* @param {[type]} doms List of DOMS loaded by PhantomJS | ||
* @param {[type]} options Options passed to the program | ||
* @param {Function} callback | ||
*/ | ||
function uncss(files, opt, cb) { | ||
var callback, | ||
stylesheets, | ||
doms, | ||
options, | ||
function uncss(files, doms, options, callback) { | ||
var stylesheets, | ||
parsed_css, | ||
used_css; | ||
/* If 'files' is a string, it should represent an HTML page. */ | ||
if (typeof files === 'string') { | ||
doms = [files]; | ||
} else { | ||
doms = utility.mapReadFiles(files); | ||
} | ||
if (typeof opt === 'function') { | ||
/* There were no options, | ||
* this argument is really the callback | ||
*/ | ||
options = {}; | ||
callback = opt; | ||
} else if (typeof opt === 'object' && typeof cb === 'function') { | ||
options = opt; | ||
callback = cb; | ||
} else { | ||
throw 'TypeError: expected a callback'; | ||
} | ||
options.csspath = (typeof options.csspath === 'string') ? options.csspath : ''; | ||
/* Parse the HTML. */ | ||
@@ -102,2 +79,50 @@ doms = doms.map(function (dom) { | ||
module.exports = uncss; | ||
/** | ||
* Main exposed function. | ||
* Here we check the options and callback, then run the files through PhantomJS. | ||
* @param {Array} files array of filenames | ||
* @param {Object} opt options | ||
* @param {Function} cb callback | ||
*/ | ||
function init(files, opt, cb) { | ||
var callback, | ||
doms, | ||
options; | ||
/* If 'files' is a string, it should represent an HTML page. */ | ||
if (typeof files === 'string') { | ||
doms = [files]; | ||
} else { | ||
doms = utility.mapReadFiles(files); | ||
} | ||
if (typeof opt === 'function') { | ||
/* There were no options, | ||
* this argument is really the callback | ||
*/ | ||
options = {}; | ||
callback = opt; | ||
} else if (typeof opt === 'object' && typeof cb === 'function') { | ||
options = opt; | ||
callback = cb; | ||
} else { | ||
throw 'TypeError: expected a callback'; | ||
} | ||
options.csspath = (typeof options.csspath === 'string') ? options.csspath : ''; | ||
async.map( | ||
doms, | ||
function (f, oncomplete) { | ||
return utility.phantom_eval(f, options.timeout, oncomplete); | ||
}, | ||
function (res) { | ||
if (typeof res !== 'Array') { | ||
res = [res]; | ||
} | ||
uncss(files, doms, options, callback); | ||
} | ||
); | ||
} | ||
// TODO: This might be counterintuitive | ||
module.exports = init; |
{ | ||
"name": "uncss", | ||
"version": "0.5.5", | ||
"version": "0.6.0", | ||
"description": "Remove unused CSS styles", | ||
@@ -34,4 +34,6 @@ "main": "lib/uncss.js", | ||
"cheerio": "~0.12.4", | ||
"underscore": "~1.5.2" | ||
"underscore": "~1.5.2", | ||
"async": "~0.2.9", | ||
"phantomjs": "~1.9.2-5" | ||
} | ||
} |
@@ -18,7 +18,11 @@ # UnCSS # | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-c, --compress Compress CSS output | ||
-i, --ignore=<selector ...> Do not remove given selectors | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-c, --compress Compress CSS output | ||
-i, --ignore <selector, ...> Do not remove given selectors | ||
-C, --csspath <path> Relative path where the CSS files are located | ||
-s, --stylesheets <file, ...> Specify additional stylesheets to process | ||
-t, --timeout <milliseconds> Wait for JS evaluation | ||
### Within node: ### | ||
@@ -30,6 +34,7 @@ | ||
options = { | ||
ignore: ['#added_at_runtime', '.created_by_jQuery'], | ||
compress: true, | ||
csspath: "../public/css/", // path where the CSS files are related to the html files. By default, uncss uses the path specified in the <link rel="stylesheet" href="path/to/file.css"> | ||
stylesheets: ["lib/bootstrap/dist/css/bootstrap.css", "src/public/css/main.css"] // Force the list of stylesheets to optimize using a path relative to the `Gruntfile.js`. Otherwise, it extracts the stylesheets from the html files | ||
ignore: ['#added_at_runtime', /test\-[0-9]+/], | ||
csspath: "../public/css/", | ||
stylesheets: ["lib/bootstrap/dist/css/bootstrap.css", "src/public/css/main.css"], | ||
timeout: 1000 | ||
}; | ||
@@ -52,2 +57,9 @@ | ||
#### Options in depth: #### | ||
- __compress__ [Boolean]: Whether the CSS output should be compressed. | ||
- __ignore__ [Array]: provide a list of selectors that should not be removed by UnCSS. For example, styles added by user interaction with the page (hover, click), since those are not detectable by UnCSS yet. Both literal names and regex patterns are recognized. | ||
- __csspath__ [String]: Path where the CSS files are related to the html files. By default, UnCSS uses the path specified in the <link rel="stylesheet" href="path/to/file.css"\> | ||
- __stylesheets__ [Array]: Force the list of stylesheets to optimize using a path relative to the `Gruntfile.js`. Otherwise, it extracts the stylesheets from the html files. | ||
- __timeout__ [Number]: Specify how long to wait for the JS to be loaded. | ||
### grunt-uncss ### | ||
@@ -54,0 +66,0 @@ If you are looking for the grunt plugin, head over to [grunt-uncss](https://github.com/addyosmani/grunt-uncss), created by @addyosmani |
Sorry, the diff of this file is not supported yet
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
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
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
138233
14
1204
70
7
1
+ Addedasync@~0.2.9
+ Addedphantomjs@~1.9.2-5
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasync@0.2.102.6.4(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbl@1.0.3(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedcaseless@0.11.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconcat-stream@1.5.0(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddebug@0.7.4(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextract-zip@1.5.0(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfd-slicer@1.0.1(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@1.0.1(transitive)
+ Addedfs-extra@0.26.7(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhar-validator@2.0.6(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhasha@2.2.0(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsonfile@2.4.0(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedkew@0.7.0(transitive)
+ Addedklaw@1.3.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@0.0.8(transitive)
+ Addedmkdirp@0.5.0(transitive)
+ Addednode-uuid@1.4.8(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpend@1.2.0(transitive)
+ Addedphantomjs@1.9.20(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedprocess-nextick-args@1.0.7(transitive)
+ Addedprogress@1.1.8(transitive)
+ Addedqs@5.2.1(transitive)
+ Addedreadable-stream@2.0.6(transitive)
+ Addedrequest@2.67.0(transitive)
+ Addedrequest-progress@2.0.1(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedthrottleit@1.0.1(transitive)
+ Addedtough-cookie@2.2.2(transitive)
+ Addedtunnel-agent@0.4.3(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addedtypedarray@0.0.7(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedwhich@1.2.14(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedxtend@4.0.2(transitive)
+ Addedyauzl@2.4.1(transitive)