-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (63 loc) · 1.86 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Module dependencies.
*/
var convert = require('convert-source-map');
var extend = require('extend');
var debug = require('debug')('duo-uglify');
var Uglify = require('uglify-js');
/**
* Returns an uglify _alternate_ plugin (operates on the entire build for a JS
* entry file)
*
* @param {Object} o
* @returns {Function}
*/
module.exports = function (o) {
debug('initializing', o);
return alternate(function uglify(build, entry) {
if (entry.type !== 'js') return;
var results = minify(entry.duo.sourceMap(), build);
build.code = results.code;
build.map = results.map;
});
function minify(sourceMap, build) {
if (!sourceMap) return none(build);
else if (sourceMap === 'inline') return inline(build);
else return external(build);
}
function none(build) {
debug('no source-map');
return Uglify.minify(build.code, extend({ fromString: true }, o));
}
function inline(build) {
debug('inline source-map');
var src = convert.removeComments(build.code);
var map = convert.fromSource(build.code).toObject();
var results = Uglify.minify(src, extend({
fromString: true,
inSourceMap: map,
outSourceMap: 'inline'
}, o));
// restore the correct source map comment
results.code = convert.removeMapFileComments(results.code);
results.code += '\n' + convert.fromJSON(results.map).toComment();
delete results.map;
return results;
}
function external(build) {
debug('external source-map');
var src = build.code;
var map = convert.fromJSON(build.map).toObject();
var comment = build.code.match(convert.mapFileCommentRegex).pop();
var mapFile = comment.split('=').pop();
return Uglify.minify(src, extend({
fromString: true,
inSourceMap: map,
outSourceMap: mapFile
}, o));
}
};
function alternate(fn) {
fn.alternate = true;
return fn;
}