Skip to content

Commit

Permalink
feat(terser): use os.availableParallelism if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
easrng authored and shellscape committed Oct 15, 2023
1 parent c4cefd9 commit 665bd2d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
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
12 changes: 9 additions & 3 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,8 +40,14 @@ export class WorkerPool extends EventEmitter {
constructor(options: WorkerPoolOptions) {
super();

// cpus() can return [] on some platforms, and we always need at least 1 worker
this.maxInstances = options.maxWorkers || cpus().length || 1;
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: 16 additions & 6 deletions pnpm-lock.yaml

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

0 comments on commit 665bd2d

Please sign in to comment.