cssmin.js
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* grunt-contrib-cssmin
* http://gruntjs.com/
*
* Copyright (c) 2012 Tim Branyen, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
var helper = require('grunt-lib-contrib').init(grunt);
var path = require('path');
grunt.registerMultiTask('cssmin', 'Minify CSS files', function() {
var options = this.options({
report: false
});
this.files.forEach(function(f) {
var valid = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
});
var max = valid
.map(grunt.file.read)
.join(grunt.util.normalizelf(grunt.util.linefeed));
var min = valid.map(function(f) {
options.relativeTo = path.dirname(f);
return minifyCSS(grunt.file.read(f), options);
})
.join('');
if (min.length < 1) {
grunt.log.warn('Destination not written because minified CSS was empty.');
} else {
if ( options.banner ) {
min = options.banner + grunt.util.linefeed + min;
}
grunt.file.write(f.dest, min);
grunt.log.writeln('File ' + f.dest + ' created.');
if(options.report) {
helper.minMaxInfo(min, max, options.report);
}
}
});
});
var minifyCSS = function(source, options) {
try {
return require('clean-css').process(source, options);
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('css minification failed.');
}
};
};