forked from galthaus/uglify-js-brunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (53 loc) · 1.68 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
'use strict';
const uglify = require('uglify-js');
const formatError = (error) => {
const err = new Error(`L${error.line}:${error.col} ${error.message}`);
err.name = '';
err.stack = error.stack;
return err;
};
class UglifyJSOptimizer {
constructor(config) {
this.options = Object.assign({}, config.plugins.uglify);
this.options.fromString = true;
this.options.sourceMaps = !!config.sourceMaps;
}
optimize(file) {
const data = file.data;
const path = file.path;
try {
if (this.options.ignored && this.options.ignored.test(file.path)) {
// ignored file path: return non minified
const result = {
data,
// brunch passes in a SourceMapGenerator object, but wants a string back.
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) {
this.options.inSourceMap = file.map.toJSON();
}
this.options.outSourceMap = this.options.sourceMaps ?
`${path}.map` : undefined;
try {
const optimized = uglify.minify(data, this.options);
const result = optimized && this.options.sourceMaps ? {
data: optimized.code,
map: optimized.map,
} : {
data: optimized.code,
};
result.data = result.data.replace(/\n\/\/# sourceMappingURL=\S+$/, '');
return Promise.resolve(result);
} catch (err) {
return Promise.reject(formatError(err));
}
}
}
UglifyJSOptimizer.prototype.brunchPlugin = true;
UglifyJSOptimizer.prototype.type = 'javascript';
module.exports = UglifyJSOptimizer;