Skip to content

Commit 4af3941

Browse files
committed
simplify the code
1 parent c13416e commit 4af3941

1 file changed

Lines changed: 30 additions & 29 deletions

File tree

tasks/cssmin.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,56 @@
77
*/
88

99
'use strict';
10+
var path = require('path');
11+
var CleanCSS = require('clean-css');
12+
var chalk = require('chalk');
13+
var maxmin = require('maxmin');
1014

1115
module.exports = function(grunt) {
12-
var path = require('path');
13-
var CleanCSS = require('clean-css');
14-
var chalk = require('chalk');
15-
var maxmin = require('maxmin');
16+
var minify = function(source, options) {
17+
try {
18+
return new CleanCSS(options).minify(source);
19+
} catch (err) {
20+
grunt.log.error(err);
21+
grunt.fail.warn('CSS minification failed.');
22+
}
23+
};
1624

17-
grunt.registerMultiTask('cssmin', 'Minify CSS files', function() {
25+
grunt.registerMultiTask('cssmin', 'Minify CSS', function() {
1826
var options = this.options({
1927
report: 'min'
2028
});
21-
this.files.forEach(function(f) {
22-
var valid = f.src.filter(function(filepath) {
29+
30+
this.files.forEach(function(file) {
31+
var valid = file.src.filter(function(filepath) {
2332
// Warn on and remove invalid source files (if nonull was set).
2433
if (!grunt.file.exists(filepath)) {
25-
grunt.log.warn('Source file "' + filepath + '" not found.');
34+
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.');
2635
return false;
2736
} else {
2837
return true;
2938
}
3039
});
3140

3241
var max = '';
33-
var min = valid.map(function(f) {
34-
var src = grunt.file.read(f);
42+
var min = valid.map(function(file) {
43+
var src = grunt.file.read(file);
3544
max += src;
36-
options.relativeTo = path.dirname(f);
37-
return minifyCSS(src, options);
45+
options.relativeTo = path.dirname(file);
46+
return minify(src, options);
3847
}).join('');
3948

40-
if (min.length < 1) {
41-
grunt.log.warn('Destination not written because minified CSS was empty.');
42-
} else {
43-
if (options.banner) {
44-
min = options.banner + grunt.util.linefeed + min;
45-
}
49+
if (min.length === 0) {
50+
return grunt.log.warn('Destination not written because minified CSS was empty.');
51+
}
4652

47-
grunt.file.write(f.dest, min);
48-
grunt.log.writeln('File ' + chalk.cyan(f.dest) + ' created: ' + maxmin(max, min, options.report === 'gzip'));
53+
if (options.banner) {
54+
min = options.banner + grunt.util.linefeed + min;
4955
}
56+
57+
grunt.file.write(file.dest, min);
58+
59+
grunt.log.writeln('File ' + chalk.cyan(file.dest) + ' created: ' + maxmin(max, min, options.report === 'gzip'));
5060
});
5161
});
52-
53-
var minifyCSS = function(source, options) {
54-
try {
55-
return new CleanCSS(options).minify(source);
56-
} catch (e) {
57-
grunt.log.error(e);
58-
grunt.fail.warn('CSS minification failed.');
59-
}
60-
};
6162
};

0 commit comments

Comments
 (0)