Comparing version 0.7.6 to 0.7.7
@@ -6,4 +6,5 @@ 'use strict'; | ||
path = require('path'), | ||
phantom = require('phantomjs'), | ||
request = require('request'), | ||
phantom = require('phantomjs'), | ||
url = require('url'), | ||
_ = require('underscore'); | ||
@@ -18,3 +19,3 @@ | ||
*/ | ||
function phantom_eval(filename, timeout, callback) { | ||
function phantomEval(filename, timeout, callback) { | ||
var childArgs = [ | ||
@@ -50,2 +51,49 @@ path.join(__dirname, 'phantom-script.js'), | ||
/** | ||
* Parse paths relatives to a source. | ||
* @param {String} source Where the paths originate from | ||
* @param {Array} stylesheets List of paths | ||
* @param {Object} options Options, as passed to UnCSS | ||
* @return {Array} List of paths | ||
*/ | ||
function parsePaths(source, stylesheets, options) { | ||
return stylesheets.map(function (sheet) { | ||
var _url, _path, _protocol; | ||
if (sheet.substr(0, 4) === 'http') { | ||
/* No need to parse, it's already a valid path */ | ||
return sheet; | ||
} | ||
/* Check if we are fetching over http(s) */ | ||
if (source.match(/^http/)) { | ||
_url = url.parse(source); | ||
_protocol = _url.protocol; | ||
} | ||
if (sheet.substr(0, 2) === '//') { | ||
/* Use the same protocol we used for fetching this page. | ||
* Default to http. | ||
*/ | ||
return (_protocol ? _protocol + sheet : 'http:' + sheet); | ||
} | ||
if (_url) { | ||
/* Let the url module handle the parsing */ | ||
_path = url.resolve(source, sheet); | ||
} else { | ||
/* We are fetching local files | ||
* Should probably report an error if we find an absolute path and | ||
* have no htmlroot specified. | ||
*/ | ||
if (sheet[0] === '/' && options.htmlroot) { | ||
_path = path.join(options.htmlroot, sheet); | ||
} else { | ||
_path = path.join(path.dirname(source), options.csspath, sheet); | ||
} | ||
} | ||
return _path; | ||
}); | ||
} | ||
/** | ||
* Given an array of filenames, return an array of the files' contents, | ||
@@ -56,3 +104,3 @@ * only if the filename matches a regex | ||
*/ | ||
function mapReadFiles(files, callback) { | ||
function readStylesheets(files, callback) { | ||
return async.map(files, function (filename, done) { | ||
@@ -151,3 +199,4 @@ if (filename.match(/^http/)) { | ||
return str.match(/(?:[^ :"]+|"[^"]*")+/g)[0]; | ||
}).join(' '); | ||
}) | ||
.join(' '); | ||
/* istanbul ignore next: need examples */ | ||
@@ -214,5 +263,8 @@ try { | ||
module.exports.phantom_eval = phantom_eval; | ||
module.exports.mapReadFiles = mapReadFiles; | ||
module.exports.extractStylesheets = extractStylesheets; | ||
module.exports.filterUnusedRules = filterUnusedRules; | ||
module.exports = { | ||
extractStylesheets : extractStylesheets, | ||
filterUnusedRules : filterUnusedRules, | ||
parsePaths : parsePaths, | ||
readStylesheets : readStylesheets, | ||
phantomEval : phantomEval | ||
}; |
@@ -7,4 +7,2 @@ 'use strict'; | ||
lib = require('./lib.js'), | ||
path = require('path'), | ||
url = require('url'), | ||
_ = require('underscore'); | ||
@@ -36,2 +34,16 @@ | ||
} | ||
/* Ignore specified stylesheets */ | ||
if (options.ignore_sheets.length > 0) { | ||
stylesheets = stylesheets.map(function (arr) { | ||
return arr.filter(function (sheet) { | ||
return _.every(options.ignore_sheets, function (ignore) { | ||
if (_.isRegExp(ignore)) { | ||
return !ignore.test(sheet); | ||
} | ||
return sheet !== ignore; | ||
}); | ||
}); | ||
}); | ||
} | ||
if (_.flatten(stylesheets).length !== 0) { | ||
@@ -46,42 +58,10 @@ /* Only run this if we found links to stylesheets (there may be none...) | ||
*/ | ||
stylesheets = stylesheets.map(function (arr, i) { | ||
return arr.map(function (el) { | ||
var u, p; | ||
if (files[i].match(/^http/)) { | ||
u = url.parse(files[i]); | ||
p = u.protocol + '//' + u.host; | ||
/* If the href to the stylesheet is an absolute path, we | ||
* use it directly. | ||
* If it starts with a / we use the host as the base url | ||
* otherwise we use the current path of the url as the | ||
* base url | ||
*/ | ||
if (el.substr(0, 4) === 'http') { | ||
p = el; | ||
} else if (el.substr(0, 2) === '//') { | ||
p = 'http:' + el; | ||
} else if (el[0] === '/') { | ||
p += el; | ||
} else { | ||
p += path.join(u.pathname.substring(0, u.pathname.lastIndexOf('/')), el); | ||
} | ||
} else { | ||
if (el.substr(0, 2) === '//') { | ||
el = 'http:' + el; | ||
} | ||
if (el[0] === '/' && options.htmlroot) { | ||
p = path.join(options.htmlroot, el); | ||
} else if (el.substr(0, 4) !== 'http') { | ||
p = path.join(path.dirname(files[i]), options.csspath, el); | ||
} else { | ||
p = el; | ||
} | ||
} | ||
return p; | ||
}); | ||
}); | ||
stylesheets = _.flatten(stylesheets); | ||
stylesheets = stylesheets.filter(function (e, i, arr) { | ||
return arr.lastIndexOf(e) === i; | ||
}); | ||
stylesheets = | ||
_.chain(stylesheets) | ||
.map(function (sheets, i) { | ||
return lib.parsePaths(files[i], sheets, options); | ||
}) | ||
.flatten() | ||
.uniq() | ||
.value(); | ||
} else { | ||
@@ -92,3 +72,3 @@ /* Reset the array if we didn't find any link tags */ | ||
stylesheets = lib.mapReadFiles(stylesheets, function (err, stylesheets) { | ||
stylesheets = lib.readStylesheets(stylesheets, function (err, stylesheets) { | ||
if (err) { | ||
@@ -172,7 +152,8 @@ return callback(err); | ||
/* Assign default values to options, unless specified */ | ||
options.csspath = options.csspath || ''; | ||
options.ignore = options.ignore || []; | ||
options.media = options.media || []; | ||
options.timeout = options.timeout || 0; | ||
options.report = options.report || false; | ||
options.csspath = options.csspath || ''; | ||
options.ignore = options.ignore || []; | ||
options.media = options.media || []; | ||
options.timeout = options.timeout || 0; | ||
options.report = options.report || false; | ||
options.ignore_sheets = options.ignore_sheets || []; | ||
@@ -188,6 +169,7 @@ /* If 'files' is a string, it should represent an HTML page. */ | ||
} | ||
return async.map( | ||
return async.mapLimit( | ||
files, | ||
require('os').cpus().length, | ||
function (f, oncomplete) { | ||
return lib.phantom_eval(f, options.timeout, oncomplete); | ||
return lib.phantomEval(f, options.timeout, oncomplete); | ||
}, | ||
@@ -194,0 +176,0 @@ function (err, res) { |
{ | ||
"name": "uncss", | ||
"version": "0.7.6", | ||
"version": "0.7.7", | ||
"author": "Giakki", | ||
@@ -55,6 +55,8 @@ "description": "Remove unused CSS styles", | ||
"grunt-mocha-cov": "~0.2.0", | ||
"load-grunt-tasks": "~0.2.1", | ||
"time-grunt": "~0.2.8", | ||
"mocha-term-cov-reporter": "~0.2.0" | ||
"load-grunt-tasks": "~0.3.0", | ||
"time-grunt": "~0.2.8" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"config": { | ||
@@ -61,0 +63,0 @@ "blanket": { |
@@ -19,3 +19,3 @@ # UnCSS | ||
### Within node: | ||
### Within Node.js: | ||
@@ -32,2 +32,3 @@ ```js | ||
stylesheets: ['lib/bootstrap/dist/css/bootstrap.css', 'src/public/css/main.css'], | ||
ignore_sheets: [/fonts.googleapis/], | ||
urls: ['http://localhost:3000/mypage', '...'] //array of urls | ||
@@ -83,11 +84,12 @@ timeout: 1000, | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-i, --ignore <selector, ...> Do not remove given selectors | ||
-m, --media <media_query, ...> Process additional media queries | ||
-C, --csspath <path> Relative path where the CSS files are located | ||
-s, --stylesheets <file, ...> Specify additional stylesheets to process | ||
-r, --raw <string> Pass in a raw string of CSS | ||
-t, --timeout <milliseconds> Wait for JS evaluation | ||
-H, --htmlroot <folder> Absolute paths' root location | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
-i, --ignore <selector, ...> Do not remove given selectors | ||
-m, --media <media_query, ...> Process additional media queries | ||
-C, --csspath <path> Relative path where the CSS files are located | ||
-s, --stylesheets <file, ...> Specify additional stylesheets to process | ||
-S, --ignore_sheets <selector, ...> Do not include specified stylesheets | ||
-r, --raw <string> Pass in a raw string of CSS | ||
-t, --timeout <milliseconds> Wait for JS evaluation | ||
-H, --htmlroot <folder> Absolute paths' root location | ||
``` | ||
@@ -103,2 +105,4 @@ | ||
- __ignore_sheets__ (Array): do not process these stylesheets, e.g. Google fonts. Accepts strings or regex patterns | ||
- __raw__ (String): give the task a raw string of CSS in addition to the existing stylesheet options; useful in scripting when your CSS hasn't yet been written to disk. | ||
@@ -105,0 +109,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25755
8
438
117
1