-
Notifications
You must be signed in to change notification settings - Fork 10
/
csso.js
executable file
·75 lines (64 loc) · 2.02 KB
/
csso.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* grunt-csso
* http://github.com/t32k/grunt-csso
*
* Copyright (c) 2013 Koji Ishimoto
* Licensed under the MIT license.
*/
'use strict';
module.exports = function (grunt) {
var fs = require('fs');
var path = require('path');
var csso = require('csso');
var chalk = require('chalk');
var maxmin = require('maxmin');
grunt.registerMultiTask('csso', 'Minify CSS files with CSSO.', function () {
var options = this.options({
restructuring: true,
banner: '',
report: false,
debug: false
});
// Process banner.
var banner = grunt.template.process(options.banner);
this.files.forEach(function (file) {
var dest = file.dest || file.src[0];
// 1. check existence
// 2. check file extension
// 3. load and concatenate css files
var original = file.src.filter(function (p) {
if (!fs.existsSync(p)) {
grunt.log.warn('Source file "' + p + '" is not found.');
return false;
} else {
return true;
}
}).filter(function (p) {
if (path.extname(p) !== '.css') {
grunt.log.warn('Source file "' + p + '" is not css.');
return false;
} else {
return true;
}
}).map(function (p) {
return fs.readFileSync(p, {
encoding: 'utf8'
});
}).join(grunt.util.normalizelf(grunt.util.linefeed));
// reverse flag
console.log(options.restructuring);
var proceed = csso.minify(original, { restructuring: options.restructuring, debug: options.debug });
if (proceed.length === 0) {
grunt.log.warn('Destination is not created because minified CSS was empty.');
} else {
// add banner.
proceed = banner + proceed;
grunt.file.write(dest, proceed);
if (options.report) {
var report = maxmin(original, proceed, options.report === 'gzip');
}
grunt.log.writeln('File ' + chalk.cyan(dest) + ' created' + ((report) ? ': ' + report : '.'));
}
});
});
};