forked from TrySound/rollup-plugin-terser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (54 loc) · 1.72 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
const { codeFrameColumns } = require("@babel/code-frame");
const Worker = require("jest-worker").default;
const { generate } = require("escodegen");
const lave = require("lave");
function terser(userOptions = {}) {
if (userOptions.sourceMap != null) {
throw Error("sourceMap option is removed, use sourcemap instead");
}
return {
name: "terser",
renderChunk(code, chunk, outputOptions) {
if (!this.worker) {
this.worker = new Worker(require.resolve("./transform.js"), {
numWorkers: userOptions.numWorkers
});
this.numOfBundles = 0;
}
this.numOfBundles++;
// TODO rewrite with object spread after node6 drop
const normalizedOptions = Object.assign({}, userOptions, {
sourceMap: userOptions.sourcemap !== false,
module: outputOptions.format === "es" || outputOptions.format === "esm"
});
for (let key of ["sourcemap", "numWorkers"]) {
if (normalizedOptions.hasOwnProperty(key)) {
delete normalizedOptions[key];
}
}
const serializedOptions = lave(normalizedOptions, {
generate,
format: "expression"
});
const result = this.worker
.transform(code, serializedOptions)
.catch(error => {
const { message, line, col: column } = error;
console.error(
codeFrameColumns(code, { start: { line, column } }, { message })
);
throw error;
});
const handler = () => {
this.numOfBundles--;
if (this.numOfBundles === 0) {
this.worker.end();
this.worker = 0;
}
};
result.then(handler, handler);
return result;
}
};
}
exports.terser = terser;