-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
51 lines (41 loc) · 1.21 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
const { codeFrameColumns } = require("@babel/code-frame");
const Worker = require("jest-worker").default;
const serialize = require("serialize-javascript");
function uglify(userOptions = {}) {
if (userOptions.sourceMap != null) {
throw Error("sourceMap option is removed, use sourcemap instead");
}
const normalizedOptions = Object.assign({}, userOptions, {
sourceMap: userOptions.sourcemap !== false
});
["sourcemap"].forEach(key => {
if (normalizedOptions.hasOwnProperty(key)) {
delete normalizedOptions[key];
}
});
const minifierOptions = serialize(normalizedOptions);
return {
name: "uglify",
renderStart() {
this.worker = new Worker(require.resolve("./transform.js"), {
numWorkers: userOptions.numWorkers
});
},
renderChunk(code) {
return this.worker.transform(code, minifierOptions).catch(error => {
const { message, line, col: column } = error;
console.error(
codeFrameColumns(code, { start: { line, column } }, { message })
);
throw error;
});
},
generateBundle() {
this.worker.end();
},
renderError() {
this.worker.end();
}
};
}
exports.uglify = uglify;