3 * https://github.com/addyosmani/grunt-uncss
\r
5 * Copyright (c) 2016 Addy Osmani
\r
6 * Licensed under the MIT license.
\r
11 var uncss = require( 'uncss' ),
\r
12 chalk = require( 'chalk' ),
\r
13 maxmin = require( 'maxmin' ),
\r
14 async = require( 'async' );
\r
16 module.exports = function ( grunt ) {
\r
17 grunt.registerMultiTask( 'uncss', 'Remove unused CSS', function () {
\r
19 var done = this.async(),
\r
20 options = this.options({
\r
24 function processFile ( file, done ) {
\r
26 var src = file.src.filter(function ( filepath ) {
\r
27 if ( /^https?:\/\//.test(filepath) ) {
\r
28 // This is a remote file: leave it in src array for uncss to handle.
\r
30 } else if ( !grunt.file.exists( filepath ) ) {
\r
31 // Warn on and remove invalid local source files (if nonull was set).
\r
32 grunt.log.warn( 'Source file ' + chalk.cyan( filepath ) + ' not found.' );
\r
39 if ( src.length === 0 && file.src.length === 0 ) {
\r
40 grunt.fail.warn( 'Destination (' + file.dest + ') not written because src files were empty.' );
\r
44 uncss( src, options, function ( error, output, report ) {
\r
49 grunt.file.write( file.dest, output );
\r
50 grunt.log.writeln('File ' + chalk.cyan( file.dest ) + ' created: ' + maxmin( report.original, output, options.report === 'gzip' ) );
\r
51 if (typeof(options.reportFile) !== 'undefined' && options.reportFile.length > 0) {
\r
52 grunt.file.write(options.reportFile, JSON.stringify(report));
\r
57 var err = new Error( 'Uncss failed.' );
\r
59 err.message += ', ' + e.msg + '.';
\r
62 grunt.log.warn( 'Uncssing source "' + src + '" failed.' );
\r
63 grunt.fail.warn( err );
\r
68 if ( this.files.length === 1 ) {
\r
69 processFile( this.files[0], done );
\r
71 // Processing multiple files must be done sequentially
\r
72 // until https://github.com/giakki/uncss/issues/136 is resolved.
\r
73 async.eachSeries( this.files, processFile, done );
\r