This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminifier.js
113 lines (82 loc) · 3.02 KB
/
minifier.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var through = require('through2');
var butternut = require('butternut');
var applySourceMap = require('vinyl-sourcemaps-apply');
var isObject = require('lodash/fp/isObject');
var zipObject = require('lodash/fp/zipObject');
var map = require('lodash/fp/map');
var prop = require('lodash/fp/prop');
var _ = require('lodash/fp/placeholder');
var defaultsDeep = require('lodash/fp/defaultsDeep');
var log = require('./lib/log');
var createError = require('./lib/create-error');
var GulpButternutError = require('./lib/gulp-butternut-error');
var reSourceMapComment = /\n\/\/# sourceMappingURL=.+?$/;
var defaultOptions = defaultsDeep({
allowDangerousEval: false,
sourceMap: true,
check: false
});
function trycatch(fn, handle) {
try {
return fn();
} catch (err) {
return handle(err);
}
}
function setup(opts) {
if (opts && !isObject(opts)) {
log.warn('gulp-butternut expects an object, non-object provided');
opts = {};
}
var options = defaultOptions(opts);
// if (options.preserveComments === 'all') {
// options.output.comments = true;
// } else if (options.preserveComments === 'some') {
// // Preserve comments with directives or that start with a bang (!)
// options.output.comments = /^!|@preserve|@license|@cc_on/i;
// } else if (options.preserveComments === 'license') {
// options.output.comments = saveLicense;
// } else if (typeof options.preserveComments === 'function') {
// options.output.comments = options.preserveComments;
// }
return options;
}
module.exports = function(opts, butternut) {
function minify(file, encoding, callback) {
var options = setup(opts || {});
var sources;
if (file.isNull()) {
return callback(null, file);
}
if (file.isStream()) {
return callback(createError(file, 'Streaming not supported', null));
}
// if (file.sourceMap) {
// if (file.sourceMap.mappings) {
// options.inSourceMap = file.sourceMap;
// }
// options.outSourceMap = file.relative;
// sources = zipObject(
// file.sourceMap.sources,
// file.sourceMap.sourcesContent
// );
// }
var mangled = trycatch(function() {
var source = String(file.contents);
var m = butternut.squash(source, options);
m.code = new Buffer(m.code.replace(reSourceMapComment, ''));
return m;
}, createError(file, 'unable to minify JavaScript'));
if (mangled instanceof GulpButternutError) {
return callback(mangled);
}
file.contents = mangled.code;
// if (file.sourceMap) {
// var sourceMap = JSON.parse(mangled.map);
// sourceMap.sourcesContent = map(prop(_, sources), sourceMap.sources);
// applySourceMap(file, sourceMap);
// }
callback(null, file);
}
return through.obj(minify);
};