-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (48 loc) · 1.33 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var through = require('through2');
var gutil = require('gulp-util');
var optimize = require('optimize-js');
var isObject = require('lodash/fp/isObject');
var defaultsDeep = require('lodash/fp/defaultsDeep');
var defaultOptions = defaultsDeep({
sourceMaps: false
});
function setup(opts) {
if (opts && !isObject(opts)) {
gutil.log(gutil.colors.yellow(
'gulp-optimize-j expects an object, non-object provided'
));
opts = {};
}
var options = defaultOptions(opts);
return options;
}
// plugin level function (dealing with files)
function gulpOptimizeJs(opts) {
function transform (file, encoding, callback) {
var options = setup(opts || {});
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(
new gutil.PluginError('gulp-optimize-js', 'Streaming not supported')
);
}
var contents = file.contents.toString('utf8');
try {
var output = optimize(contents, options);
} catch (err) {
return callback(
new gutil.PluginError(
'gulp-optimize-js',
'Unable to optimize. Is this valid JavaScript?'
)
);
}
file.contents = new Buffer(output);
callback(null, file);
}
return through.obj(transform);
}
// exporting the plugin main function
module.exports = gulpOptimizeJs;