-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (31 loc) · 1.11 KB
/
index.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
'use strict';
var through2 = require('through2');
var Minimize = require('minimize');
var gutil = require('gulp-util');
module.exports = function (options) {
options = options || {};
var minimize = new Minimize(options);
return through2.obj(function(file,encoding,callback){
if (!file) {
this.emit('error', new gutil.PluginError('gulp-minimize', 'files can not be empty'));
return callback();
}
if (file.isNull()) {
return callback();
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-minimize', 'streaming not supported'));
return callback();
}
var contents = file.contents.toString('utf-8');
try {
var minifyHtmlString = minimize.parse(contents);
file.contents = new Buffer(minifyHtmlString);
}catch(e){
this.emit('error', new gutil.PluginError('gulp-minimize', e + 'minify html "' + gutil.colors.red(file.path) + '" failed'));
return callback();
}
this.push(file);
callback();
})
};