Skip to content

Commit

Permalink
fix: use os.availableParallelism() for parallelism when it is available
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 6, 2025
1 parent f148aa1 commit b07feeb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ Type: `Boolean|Number`
Default: `true`

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

> ℹ️ Parallelization can speed up your build significantly and is therefore **highly recommended**.
> If a parallelization is enabled, the packages in `minimizerOptions` must be required via strings (`packageName` or `require.resolve(packageName)`). Read more in [`minimizerOptions`](#minimizeroptions)
Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,14 @@ class CssMinimizerPlugin {
static getAvailableNumberOfCores(parallel) {
// In some cases cpus() returns undefined
// https://github.com/nodejs/node/issues/19022
const cpus = os.cpus() || { length: 1 };
const cpus =
typeof os.availableParallelism === "function"
? { length: os.availableParallelism() }
: os.cpus() || { length: 1 };

return parallel === true
return parallel === true || typeof parallel === "undefined"
? cpus.length - 1
: Math.min(Number(parallel) || 0, cpus.length - 1);
: Math.min(parallel || 0, cpus.length - 1);
}

/**
Expand Down Expand Up @@ -681,7 +684,6 @@ class CssMinimizerPlugin {
getWorker && numberOfAssetsForMinify > 0
? /** @type {number} */ (numberOfWorkers)
: scheduledTasks.length;

await throttleAll(limit, scheduledTasks);

if (initializedWorker) {
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/parallel-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ exports[`parallel option should match snapshot for the "true" value: errors 1`]

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

exports[`parallel option should match snapshot for the "undefined" value: assets 1`] = `
{
"four.css": "body{color:red}a{color:blue}",
"one.css": "body{color:red}a{color:blue}",
"three.css": "body{color:red}a{color:blue}",
"two.css": "body{color:red}a{color:blue}",
}
`;

exports[`parallel option should match snapshot for the "undefined" value: errors 1`] = `[]`;

exports[`parallel option should match snapshot for the "undefined" value: warnings 1`] = `[]`;

exports[`parallel option should match snapshot when a value is not specify: assets 1`] = `
{
"four.css": "body{color:red}a{color:blue}",
Expand Down
37 changes: 34 additions & 3 deletions test/parallel-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {

jest.mock("os", () => {
const actualOs = jest.requireActual("os");
const isAvailableParallelism =
typeof actualOs.availableParallelism !== "undefined";

const mocked = {
availableParallelism: isAvailableParallelism ? jest.fn(() => 4) : undefined,
cpus: jest.fn(() => {
return { length: 4 };
}),
Expand Down Expand Up @@ -52,6 +55,14 @@ jest.mock("jest-worker", () => {

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

const getParallelism = () => {
if (typeof os.availableParallelism !== "undefined") {
return os.availableParallelism();
}

return os.cpus().length;
};

describe("parallel option", () => {
let compiler;

Expand All @@ -76,7 +87,7 @@ describe("parallel option", () => {
expect(Worker).toHaveBeenCalledTimes(1);
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
enableWorkerThreads: ENABLE_WORKER_THREADS,
numWorkers: os.cpus().length - 1,
numWorkers: getParallelism() - 1,
});
expect(workerTransform).toHaveBeenCalledTimes(
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
Expand Down Expand Up @@ -108,7 +119,27 @@ describe("parallel option", () => {
expect(Worker).toHaveBeenCalledTimes(1);
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
enableWorkerThreads: ENABLE_WORKER_THREADS,
numWorkers: Math.min(4, os.cpus().length - 1),
numWorkers: Math.min(4, getParallelism() - 1),
});
expect(workerTransform).toHaveBeenCalledTimes(
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
);
expect(workerEnd).toHaveBeenCalledTimes(1);

expect(readAssets(compiler, stats, /\.css$/)).toMatchSnapshot("assets");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
});

it('should match snapshot for the "undefined" value', async () => {
new CssMinimizerPlugin({ parallel: undefined }).apply(compiler);

const stats = await compile(compiler);

expect(Worker).toHaveBeenCalledTimes(1);
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
enableWorkerThreads: ENABLE_WORKER_THREADS,
numWorkers: Math.min(4, getParallelism() - 1),
});
expect(workerTransform).toHaveBeenCalledTimes(
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
Expand Down Expand Up @@ -152,7 +183,7 @@ describe("parallel option", () => {
expect(Worker).toHaveBeenCalledTimes(1);
expect(Worker).toHaveBeenLastCalledWith(workerPath, {
enableWorkerThreads: ENABLE_WORKER_THREADS,
numWorkers: Math.min(1, os.cpus().length - 1),
numWorkers: Math.min(1, getParallelism() - 1),
});
expect(workerTransform).toHaveBeenCalledTimes(
Object.keys(readAssets(compiler, stats, /\.css$/)).length,
Expand Down

0 comments on commit b07feeb

Please sign in to comment.