Skip to content

Commit 1357994

Browse files
fix: use os.availableParallelism() for parallelism when it is available (#623)
1 parent 84bb19b commit 1357994

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ type parallel = boolean | number;
167167
Default: `true`
168168

169169
Use multi-process parallel running to improve the build speed.
170-
Default number of concurrent runs: `os.cpus().length - 1`.
170+
Default number of concurrent runs: `os.cpus().length - 1` or `os.availableParallelism() - 1` (if this function is supported).
171171

172172
> **Note**
173173
>

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,14 @@ class TerserPlugin {
320320
static getAvailableNumberOfCores(parallel) {
321321
// In some cases cpus() returns undefined
322322
// https://github.com/nodejs/node/issues/19022
323-
const cpus = os.cpus() || { length: 1 };
323+
const cpus =
324+
typeof os.availableParallelism === "function"
325+
? { length: os.availableParallelism() }
326+
: os.cpus() || { length: 1 };
324327

325-
return parallel === true
328+
return parallel === true || typeof parallel === "undefined"
326329
? cpus.length - 1
327-
: Math.min(Number(parallel) || 0, cpus.length - 1);
330+
: Math.min(parallel || 0, cpus.length - 1);
328331
}
329332

330333
/**

test/__snapshots__/parallel-option.test.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ exports[`parallel option should match snapshot for the "true" value: errors 1`]
9090

9191
exports[`parallel option should match snapshot for the "true" value: warnings 1`] = `Array []`;
9292

93+
exports[`parallel option should match snapshot for the "undefined" value: assets 1`] = `
94+
Object {
95+
"four.js": "(()=>{var r={921:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(921)})();",
96+
"one.js": "(()=>{var r={921:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(921)})();",
97+
"three.js": "(()=>{var r={921:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(921)})();",
98+
"two.js": "(()=>{var r={921:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(921)})();",
99+
}
100+
`;
101+
102+
exports[`parallel option should match snapshot for the "undefined" value: errors 1`] = `Array []`;
103+
104+
exports[`parallel option should match snapshot for the "undefined" value: warnings 1`] = `Array []`;
105+
93106
exports[`parallel option should match snapshot when a value is not specify: assets 1`] = `
94107
Object {
95108
"four.js": "(()=>{var r={921:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(921)})();",

test/parallel-option.test.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import {
1515

1616
jest.mock("os", () => {
1717
const actualOs = jest.requireActual("os");
18+
const isAvailableParallelism =
19+
typeof actualOs.availableParallelism !== "undefined";
1820

1921
const mocked = {
22+
availableParallelism: isAvailableParallelism ? jest.fn(() => 4) : undefined,
2023
cpus: jest.fn(() => {
2124
return { length: 4 };
2225
}),
@@ -53,6 +56,14 @@ jest.mock("jest-worker", () => {
5356

5457
const workerPath = require.resolve("../src/minify");
5558

59+
const getParallelism = () => {
60+
if (typeof os.availableParallelism !== "undefined") {
61+
return os.availableParallelism();
62+
}
63+
64+
return os.cpus().length;
65+
};
66+
5667
describe("parallel option", () => {
5768
let compiler;
5869

@@ -77,7 +88,7 @@ describe("parallel option", () => {
7788
expect(Worker).toHaveBeenCalledTimes(1);
7889
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
7990
enableWorkerThreads: ENABLE_WORKER_THREADS,
80-
numWorkers: os.cpus().length - 1,
91+
numWorkers: getParallelism() - 1,
8192
});
8293
expect(workerTransform).toHaveBeenCalledTimes(
8394
Object.keys(stats.compilation.assets).length
@@ -109,7 +120,27 @@ describe("parallel option", () => {
109120
expect(Worker).toHaveBeenCalledTimes(1);
110121
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
111122
enableWorkerThreads: ENABLE_WORKER_THREADS,
112-
numWorkers: Math.min(4, os.cpus().length - 1),
123+
numWorkers: getParallelism() - 1,
124+
});
125+
expect(workerTransform).toHaveBeenCalledTimes(
126+
Object.keys(stats.compilation.assets).length
127+
);
128+
expect(workerEnd).toHaveBeenCalledTimes(1);
129+
130+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
131+
expect(getErrors(stats)).toMatchSnapshot("errors");
132+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
133+
});
134+
135+
it('should match snapshot for the "undefined" value', async () => {
136+
new TerserPlugin({ parallel: undefined }).apply(compiler);
137+
138+
const stats = await compile(compiler);
139+
140+
expect(Worker).toHaveBeenCalledTimes(1);
141+
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
142+
enableWorkerThreads: ENABLE_WORKER_THREADS,
143+
numWorkers: getParallelism() - 1,
113144
});
114145
expect(workerTransform).toHaveBeenCalledTimes(
115146
Object.keys(stats.compilation.assets).length

0 commit comments

Comments
 (0)