-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathcssmin.js
More file actions
62 lines (53 loc) · 1.63 KB
/
cssmin.js
File metadata and controls
62 lines (53 loc) · 1.63 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
62
/*
* grunt-contrib-cssmin
* http://gruntjs.com/
*
* Copyright (c) 2014 Tim Branyen, contributors
* Licensed under the MIT license.
*/
'use strict';
var path = require('path');
var CleanCSS = require('clean-css');
var chalk = require('chalk');
var maxmin = require('maxmin');
module.exports = function(grunt) {
var minify = function(source, options) {
try {
return new CleanCSS(options).minify(source);
} catch (err) {
grunt.log.error(err);
grunt.fail.warn('CSS minification failed.');
}
};
grunt.registerMultiTask('cssmin', 'Minify CSS', function() {
var options = this.options({
report: 'min'
});
this.files.forEach(function(file) {
var valid = file.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 ' + chalk.cyan(filepath) + ' not found.');
return false;
} else {
return true;
}
});
var max = '';
var min = valid.map(function(file) {
var src = grunt.file.read(file);
max += src;
options.relativeTo = path.dirname(file);
return minify(src, options);
}).join('');
if (min.length === 0) {
return grunt.log.warn('Destination not written because minified CSS was empty.');
}
if (options.banner) {
min = options.banner + grunt.util.linefeed + min;
}
grunt.file.write(file.dest, min);
grunt.log.writeln('File ' + chalk.cyan(file.dest) + ' created: ' + maxmin(max, min, options.report === 'gzip'));
});
});
};