Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

uncss

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

uncss - npm Package Compare versions

Comparing version 0.7.2 to 0.7.3

52

lib/lib.js

@@ -1,17 +0,16 @@

/*jslint node: true, plusplus: true, stupid: true*/
'use strict';
"use strict";
var async = require('async'),
child_process = require('child_process'),
fs = require('fs'),
path = require('path'),
var async = require('async'),
fs = require('fs'),
path = require('path'),
request = require('request'),
spawn = require('child_process').spawn,
phantom = require('phantomjs'),
_ = require('underscore');
_ = require('underscore');
/**
* Run a page through phantomjs
* @param {String} Filename The name of the HTML page
* @param {Function} Callback
* @param {String} filename The name of the HTML page
* @param {Number} timeout How much to wait for JS evaluation
* @param {Function} callback
* @return {String} The contents of the files, as seen by Phantom

@@ -29,3 +28,3 @@ */

page = child_process.spawn(phantom.path, childArgs);
page = spawn(phantom.path, childArgs);
page.stdout.setEncoding('utf8');

@@ -39,3 +38,10 @@ page.stderr.setEncoding('utf8');

page.stderr.on('data', function (data) {
error += data;
// Ignore PhantomJS 1.9.2 OSX errors
var bufferStr = data + '',
isNot192OSXError = bufferStr.indexOf('WARNING: Method userSpaceScaleFactor') === -1,
isNotPerformanceNote = bufferStr.indexOf('CoreText performance note:') === -1;
if (isNot192OSXError && isNotPerformanceNote) {
error += bufferStr;
}
});

@@ -49,3 +55,3 @@

// (no need to exit the process)
console.error("PhantomJS: ", error);
console.error('PhantomJS: ', error);
callback(buffer);

@@ -58,7 +64,7 @@ }

* Given an array of filenames, return an array of the files' contents,
* only if the filename matches a regex
* @param {Array} files An array of the filenames to read
* @return {Array} Array Of the files' contents
* only if the filename matches a regex
* @param {Array} files An array of the filenames to read
* @param {Function} callback
*/
function mapReadFiles(files, cb) {
function mapReadFiles(files, callback) {
async.map(files, function (filename, done) {

@@ -68,3 +74,3 @@ if (filename.match(/^http/)) {

filename,
{ headers: {"User-Agent": "UnCSS"} },
{ headers: {'User-Agent': 'UnCSS'} },
function (err, res, body) {

@@ -81,6 +87,5 @@ if (err) {

}
console.log('mapReadFiles Error: could not find: ' + path.join(process.cwd(), filename));
process.exit();
return done('mapReadFiles Error: could not find: ' + path.join(process.cwd(), filename));
}
}, cb);
}, callback);
}

@@ -90,4 +95,5 @@

* Extract stylesheets' hrefs from dom
* @param {Array} dom List of DOMs loaded by cheerio
* @return {Array} array of hrefs
* @param {Array} dom List of DOMs loaded by cheerio
* @param {Object} options Options, as passed to UnCSS
* @return {Array} Array of hrefs
*/

@@ -94,0 +100,0 @@ function extract_stylesheets(dom, options) {

@@ -1,2 +0,2 @@

/*jslint node: true*/
/* global phantom */
(function () {

@@ -3,0 +3,0 @@ 'use strict';

@@ -1,3 +0,2 @@

/*jslint node: true */
"use strict";
'use strict';

@@ -30,7 +29,5 @@ var async = require('async'),

if (options.stylesheets && options.stylesheets.length > 0) {
stylesheets = options.stylesheets.map(function (sheet) {
return path.join(options.csspath, sheet);
});
/* Simulate the behavior below */
stylesheets = [options.stylesheets];
} else {
/* Extract the stylesheets from the HTML */

@@ -40,2 +37,3 @@ stylesheets = doms.map(function (html) {

});
}
if (_.flatten(stylesheets).length !== 0) {

@@ -69,3 +67,3 @@ /* Only run this if we found links to stylesheets (there may be none...)

} else {
p += u.pathname.substr(0, u.pathname.lastIndexOf('/') + 1) + el;
p += path.join(u.pathname, el);
}

@@ -90,5 +88,7 @@ } else {

}
}
stylesheets = utility.mapReadFiles(stylesheets, function (err, stylesheets) {
if (err) {
throw err;
}
/* If we specified a raw string of CSS, add it to the stylesheets array */

@@ -99,3 +99,3 @@ if (options.raw) {

} else {
throw "TypeError: options.raw - expected a string";
throw 'TypeError: options.raw - expected a string';
}

@@ -121,3 +121,3 @@ }

*/
var css_str = stylesheets.join(" \n");
var css_str = stylesheets.join(' \n');
var parsed = css.parse(css_str);

@@ -124,0 +124,0 @@ var used_css = utility.filterUnusedRules(doms, parsed.stylesheet, options.ignore);

{
"name": "uncss",
"version": "0.7.2",
"version": "0.7.3",
"description": "Remove unused CSS styles",

@@ -36,3 +36,3 @@ "main": "lib/uncss.js",

"phantomjs": "~1.9.2-6",
"request": "~2.31.0"
"request": "~2.33.0"
},

@@ -39,0 +39,0 @@ "devDependencies": {

@@ -5,3 +5,4 @@ # UnCSS #

Remove unused styles from CSS
UnCSS is a tool that removes unused CSS from your stylesheets.
It works across multiple files and supports Javascript-injected CSS.

@@ -22,2 +23,3 @@ ## Installation: ##

ignore: ['#added_at_runtime', /test\-[0-9]+/],
media: ['(min-width: 700px) handheld and (orientation: landscape)'],
csspath: "../public/css/",

@@ -45,36 +47,44 @@ raw: 'h1 { color: green }',

### At build-time ###
UnCSS can also be used in conjunction with other javascript build systems, such as [Grunt](https://github.com/gruntjs/grunt) or [Gulp](https://github.com/gulpjs/gulp)!
Thanks to @addyosmani for creating:
- [grunt-uncss](https://github.com/addyosmani/grunt-uncss)
- [gulp-uncss-task](https://github.com/addyosmani/gulp-uncss-task)
and to @ben-eb for creating:
- [gulp-uncss](https://github.com/ben-eb/gulp-uncss)
### From the command line: ###
Usage: uncss [options] <file ...>
e.g. uncss http://getbootstrap.com/examples/jumbotron/ > stylesheet.css
Usage: ```uncss [options] <file ...>```
e.g. ```uncss --ignore .donotwant,#nope http://getbootstrap.com/examples/jumbotron/```
Options:
-h, --help output usage information
-V, --version output the version number
-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
-r, --raw <string> Pass in a raw string of CSS
-t, --timeout <milliseconds> Wait for JS evaluation
-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
### At build-time ###
UnCSS can also be used in conjunction with other javascript build systems, thanks to @addyosmani for creating:
- __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.
- [grunt-uncss](https://github.com/addyosmani/grunt-uncss)
- [gulp-uncss-task](https://github.com/addyosmani/gulp-uncss-task)
- __media__ (Array): By default UnCSS processes only stylesheets with media query "_all_", "_screen_", and those without one. Specify here which others to include.
#### Options in depth: ####
- __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.
- __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.
- __urls__ [Array]: Array of URLs to load with Phantom (on top of the files already passed if any).
- __timeout__ [Number]: Specify how long to wait for the JS to be loaded.
- __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): use these stylesheets instead of those extracted from the html files.
- __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.
- __urls__ (Array): array of URLs to load with Phantom (on top of the files already passed if any).
- __timeout__ (Number): specify how long to wait for the JS to be loaded.
## License ##
Copyright (c) 2013 Giacomo Martino. See the LICENSE file for license rights and limitations (MIT).
### Features planned: ###
- Fully support all CSS selectors
- Support @-rules

@@ -1,3 +0,1 @@

/* jshint node: true */
/* global describe, it, before, beforeEach, after, afterEach */
'use strict';

@@ -4,0 +2,0 @@

@@ -5,6 +5,7 @@ var expect = require('chai').expect,

describe("Compile the CSS of an html page passed by path", function() {
it("should compile two stylesheets into one and keep the media query", function(done) {
describe("Compile the CSS of an html page passed by path", function () {
'use strict';
it("Should compile two stylesheets into one and keep the media query", function (done) {
this.timeout(10000);
uncss(["tests/input/testpage.html"], function(output) {
uncss(["tests/input/testpage.html"], function (output) {
expect(output).to.exist;

@@ -11,0 +12,0 @@ fs.writeFile(__dirname+"/output/testpage.compiled.css", output, done);

@@ -1,3 +0,1 @@

/* jshint node: true */
/* global describe, it, before, beforeEach, after, afterEach */
'use strict';

@@ -29,12 +27,2 @@

describe('Selectors', function () {
/* Wait until uncss finished doing its thing before running our tests */
var check = function (done) {
if (rawcss) {
done();
} else {
setTimeout(function () {
check(done);
}, 500);
}
};

@@ -44,4 +32,4 @@ before(function (done) {

rawcss = output;
done();
});
check(done);
});

@@ -53,3 +41,3 @@

tests.forEach(function (test) {
it('should not output unused ' + test.split('.')[0], function () {
it('Should not output unused ' + test.split('.')[0], function () {
expect(rawcss).to.not.include.string(rfs('unused/' + test));

@@ -63,3 +51,3 @@ });

tests.forEach(function (test) {
it('should output expected ' + test.split('.')[0], function () {
it('Should output expected ' + test.split('.')[0], function () {
expect(rawcss).to.include.string(rfs('expected/' + test));

@@ -66,0 +54,0 @@ });

var expect = require('chai').expect,
fs = require('fs'),
uncss = require('../lib/uncss');
uncss = require('../lib/uncss'),
/* Local */
gh_path = __dirname + '/output/gh-pages/stylesheets/stylesheet.css',
prev_run;
describe('Compile the CSS of an html page passed by url', function () {
describe('Compile the CSS of an html page passed by url (May take a while)', function () {
'use strict';
/* Used to check that all the requests to gh-pages generate the same CSS.
* Expected to fail once if the gh-page is updated.
*/
before(function (done) {
fs.readFile(gh_path, 'utf-8', function (err, stylesheet) {
if (err) {
throw err;
}
prev_run = stylesheet;
done();
});
});
it('Accepts an array of urls', function (done) {
this.timeout(15000);
uncss(['http://getbootstrap.com/examples/jumbotron/'], function (output) {
expect(output).to.exist;
expect(output).to.have.length.above(2);
fs.writeFile(__dirname + '/output/bootstrap/jumbotron.compiled.css', output, done);

@@ -15,10 +32,33 @@ });

it('Deal with CSS files linked with absolute url', function (done) {
this.timeout(10000);
it('Deals with CSS files linked with absolute url', function (done) {
this.timeout(15000);
uncss(['http://giakki.github.io/uncss/'], function (output) {
expect(output).to.exist;
fs.writeFile(__dirname + '/output/gh-pages/stylesheets/stylesheet.css', output, done);
expect(output).to.equal(prev_run);
prev_run = output;
done();
});
});
it('Deals with relative options.stylesheets when using urls', function (done) {
this.timeout(15000);
uncss(['http://giakki.github.io/uncss/'], { stylesheets: ['stylesheets/stylesheet.css'] }, function (output) {
expect(output).to.equal(prev_run);
prev_run = output;
done();
});
});
it('Deals with absolute options.stylesheets when using urls', function (done) {
this.timeout(15000);
uncss(['http://giakki.github.io/uncss/'], { stylesheets: ['/uncss/stylesheets/stylesheet.css'] }, function (output) {
expect(output).to.equal(prev_run);
prev_run = output;
done();
});
});
after(function (done) {
fs.writeFile(__dirname + '/output/bootstrap/jumbotron.compiled.css', prev_run, done);
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc