TaskRunner.js
3.33 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _os = _interopRequireDefault(require("os"));
var _cacache = _interopRequireDefault(require("cacache"));
var _findCacheDir = _interopRequireDefault(require("find-cache-dir"));
var _workerFarm = _interopRequireDefault(require("worker-farm"));
var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
var _isWsl = _interopRequireDefault(require("is-wsl"));
var _minify = _interopRequireDefault(require("./minify"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const worker = require.resolve('./worker');
class TaskRunner {
constructor(options = {}) {
const {
cache,
parallel
} = options;
this.cacheDir = cache === true ? (0, _findCacheDir.default)({
name: 'terser-webpack-plugin'
}) || _os.default.tmpdir() : cache; // In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
const cpus = _os.default.cpus() || {
length: 1
}; // WSL sometimes freezes, error seems to be on the WSL side
// https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
this.maxConcurrentWorkers = _isWsl.default ? 1 : parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1);
}
run(tasks, callback) {
/* istanbul ignore if */
if (!tasks.length) {
callback(null, []);
return;
}
if (this.maxConcurrentWorkers > 1) {
const workerOptions = process.platform === 'win32' ? {
maxConcurrentWorkers: this.maxConcurrentWorkers,
maxConcurrentCallsPerWorker: 1
} : {
maxConcurrentWorkers: this.maxConcurrentWorkers
};
this.workers = (0, _workerFarm.default)(workerOptions, worker);
this.boundWorkers = (options, cb) => {
try {
this.workers((0, _serializeJavascript.default)(options), cb);
} catch (error) {
// worker-farm can fail with ENOMEM or something else
cb(error);
}
};
} else {
this.boundWorkers = (options, cb) => {
try {
cb(null, (0, _minify.default)(options));
} catch (error) {
cb(error);
}
};
}
let toRun = tasks.length;
const results = [];
const step = (index, data) => {
toRun -= 1;
results[index] = data;
if (!toRun) {
callback(null, results);
}
};
tasks.forEach((task, index) => {
const enqueue = () => {
this.boundWorkers(task, (error, data) => {
const result = error ? {
error
} : data;
const done = () => step(index, result);
if (this.cacheDir && !result.error) {
_cacache.default.put(this.cacheDir, (0, _serializeJavascript.default)(task.cacheKeys), JSON.stringify(data)).then(done, done);
} else {
done();
}
});
};
if (this.cacheDir) {
_cacache.default.get(this.cacheDir, (0, _serializeJavascript.default)(task.cacheKeys)).then(({
data
}) => step(index, JSON.parse(data)), enqueue);
} else {
enqueue();
}
});
}
exit() {
if (this.workers) {
_workerFarm.default.end(this.workers);
}
}
}
exports.default = TaskRunner;