-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (46 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
'use strict';
const uglify = require('uglify-es');
const formatError = function(error) {
const err = new Error(`L${error.line}:${error.col} ${error.message}`);
err.name = '';
err.stack = error.stack;
return err;
};
class UglifyESOptimizer {
constructor(config) {
this.options = Object.assign({
sourceMap: !!config.sourceMaps,
}, config.plugins.uglify);
}
optimize(file) {
const options = Object.assign({}, this.options);
try {
if (this.options.ignored && this.options.ignored.test(file.path)) {
const result = {
data: file.data,
map: file.map ? file.map.toString() : null,
};
return Promise.resolve(result);
}
} catch (e) {
return Promise.reject(`error checking ignored files to uglify ${e}`);
}
if (file.map) {
options.sourceMap = {
content: JSON.stringify(file.map),
url: `${file.path}.map`,
};
}
delete options.ignored;
const res = uglify.minify(file.data, options);
if (res.error) throw formatError(res.error);
if (!res.map) return {data: res.code};
return {
data: res.code.replace(/\/\/# sourceMappingURL=\S+$/, ''),
map: res.map,
};
}
}
UglifyESOptimizer.prototype.brunchPlugin = true;
UglifyESOptimizer.prototype.type = 'javascript';
module.exports = UglifyESOptimizer;