Skip to content

Commit b07feeb

Browse files
fix: use os.availableParallelism() for parallelism when it is available
1 parent f148aa1 commit b07feeb

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Type: `Boolean|Number`
177177
Default: `true`
178178

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

182182
> ℹ️ Parallelization can speed up your build significantly and is therefore **highly recommended**.
183183
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). Read more in [`minimizerOptions`](#minimizeroptions)

src/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,14 @@ class CssMinimizerPlugin {
389389
static getAvailableNumberOfCores(parallel) {
390390
// In some cases cpus() returns undefined
391391
// https://github.com/nodejs/node/issues/19022
392-
const cpus = os.cpus() || { length: 1 };
392+
const cpus =
393+
typeof os.availableParallelism === "function"
394+
? { length: os.availableParallelism() }
395+
: os.cpus() || { length: 1 };
393396

394-
return parallel === true
397+
return parallel === true || typeof parallel === "undefined"
395398
? cpus.length - 1
396-
: Math.min(Number(parallel) || 0, cpus.length - 1);
399+
: Math.min(parallel || 0, cpus.length - 1);
397400
}
398401

399402
/**
@@ -681,7 +684,6 @@ class CssMinimizerPlugin {
681684
getWorker && numberOfAssetsForMinify > 0
682685
? /** @type {number} */ (numberOfWorkers)
683686
: scheduledTasks.length;
684-
685687
await throttleAll(limit, scheduledTasks);
686688

687689
if (initializedWorker) {

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

+13
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`] = `[]`;
9292

93+
exports[`parallel option should match snapshot for the "undefined" value: assets 1`] = `
94+
{
95+
"four.css": "body{color:red}a{color:blue}",
96+
"one.css": "body{color:red}a{color:blue}",
97+
"three.css": "body{color:red}a{color:blue}",
98+
"two.css": "body{color:red}a{color:blue}",
99+
}
100+
`;
101+
102+
exports[`parallel option should match snapshot for the "undefined" value: errors 1`] = `[]`;
103+
104+
exports[`parallel option should match snapshot for the "undefined" value: warnings 1`] = `[]`;
105+
93106
exports[`parallel option should match snapshot when a value is not specify: assets 1`] = `
94107
{
95108
"four.css": "body{color:red}a{color:blue}",

test/parallel-option.test.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import {
1414

1515
jest.mock("os", () => {
1616
const actualOs = jest.requireActual("os");
17+
const isAvailableParallelism =
18+
typeof actualOs.availableParallelism !== "undefined";
1719

1820
const mocked = {
21+
availableParallelism: isAvailableParallelism ? jest.fn(() => 4) : undefined,
1922
cpus: jest.fn(() => {
2023
return { length: 4 };
2124
}),
@@ -52,6 +55,14 @@ jest.mock("jest-worker", () => {
5255

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

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

@@ -76,7 +87,7 @@ describe("parallel option", () => {
7687
expect(Worker).toHaveBeenCalledTimes(1);
7788
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
7889
enableWorkerThreads: ENABLE_WORKER_THREADS,
79-
numWorkers: os.cpus().length - 1,
90+
numWorkers: getParallelism() - 1,
8091
});
8192
expect(workerTransform).toHaveBeenCalledTimes(
8293
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
@@ -108,7 +119,27 @@ describe("parallel option", () => {
108119
expect(Worker).toHaveBeenCalledTimes(1);
109120
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
110121
enableWorkerThreads: ENABLE_WORKER_THREADS,
111-
numWorkers: Math.min(4, os.cpus().length - 1),
122+
numWorkers: Math.min(4, getParallelism() - 1),
123+
});
124+
expect(workerTransform).toHaveBeenCalledTimes(
125+
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
126+
);
127+
expect(workerEnd).toHaveBeenCalledTimes(1);
128+
129+
expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot("assets");
130+
expect(getErrors(stats)).toMatchSnapshot("errors");
131+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
132+
});
133+
134+
it('should match snapshot for the "undefined" value', async () => {
135+
new CssMinimizerPlugin({ parallel: undefined }).apply(compiler);
136+
137+
const stats = await compile(compiler);
138+
139+
expect(Worker).toHaveBeenCalledTimes(1);
140+
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
141+
enableWorkerThreads: ENABLE_WORKER_THREADS,
142+
numWorkers: Math.min(4, getParallelism() - 1),
112143
});
113144
expect(workerTransform).toHaveBeenCalledTimes(
114145
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
@@ -152,7 +183,7 @@ describe("parallel option", () => {
152183
expect(Worker).toHaveBeenCalledTimes(1);
153184
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
154185
enableWorkerThreads: ENABLE_WORKER_THREADS,
155-
numWorkers: Math.min(1, os.cpus().length - 1),
186+
numWorkers: Math.min(1, getParallelism() - 1),
156187
});
157188
expect(workerTransform).toHaveBeenCalledTimes(
158189
Object.keys(readAssets(compiler, stats, /\.css$/)).length,

0 commit comments

Comments
 (0)