Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terser): always make at least 1 worker thread #1604

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"@ava/babel": "2.0.0",
"@rollup/plugin-typescript": "^9.0.1",
"@types/conventional-commits-parser": "^3.0.2",
"@types/node": "14.18.30",
"@types/node": "20.8.4",
"@types/semver": "^7.3.7",
"@types/source-map-support": "^0.5.4",
"@types/yargs-parser": "^20.2.1",
Expand Down
11 changes: 9 additions & 2 deletions packages/terser/src/worker-pool.ts
@@ -1,6 +1,6 @@
import { AsyncResource } from 'async_hooks';
import { Worker } from 'worker_threads';
import { cpus } from 'os';
import os from 'os';
import { EventEmitter } from 'events';

import serializeJavascript from 'serialize-javascript';
Expand Down Expand Up @@ -40,7 +40,14 @@ export class WorkerPool extends EventEmitter {
constructor(options: WorkerPoolOptions) {
super();

this.maxInstances = options.maxWorkers || cpus().length;
if (options.maxWorkers) {
this.maxInstances = options.maxWorkers;
} else if (typeof os.availableParallelism === 'function') {
this.maxInstances = os.availableParallelism();
} else {
// cpus() can return [] on some platforms, and we always need at least 1 worker
this.maxInstances = os.cpus().length || 1;
}
this.filePath = options.filePath;

this.on(freeWorker, () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/terser/test/test.js
@@ -1,3 +1,5 @@
const os = require('os');

const test = require('ava');
const { rollup } = require('rollup');

Expand Down Expand Up @@ -356,3 +358,23 @@ test.serial('terser preserve vars in nameCache when provided', async (t) => {
}
});
});

test.serial('minify when os.cpus().length === 0', async (t) => {
const originalAvailableParallelism = os.availableParallelism;
const originalCpus = os.cpus;
os.cpus = () => [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe overwriting os.cpus in this way is breaking Node 20.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@easrng could you please take a look?

try {
const bundle = await rollup({
input: 'test/fixtures/unminified.js',
plugins: [terser()]
});
const result = await bundle.generate({ format: 'cjs' });
t.is(result.output.length, 1);
const [output] = result.output;
t.is(output.code, '"use strict";window.a=5,window.a<3&&console.log(4);\n');
t.falsy(output.map);
} finally {
os.availableParallelism = originalAvailableParallelism;
os.cpus = originalCpus;
}
});
22 changes: 16 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.