From c4cefd9cd1a06673fd43c719fb424673a8df5a3a Mon Sep 17 00:00:00 2001 From: easrng Date: Sat, 7 Oct 2023 21:03:23 -0600 Subject: [PATCH] fix(terser): always make at least 1 worker thread (https://github.com/rollup/plugins/issues/1594) --- packages/terser/src/worker-pool.ts | 3 ++- packages/terser/test/test.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/terser/src/worker-pool.ts b/packages/terser/src/worker-pool.ts index 92d6902c4..d061e5d69 100644 --- a/packages/terser/src/worker-pool.ts +++ b/packages/terser/src/worker-pool.ts @@ -40,7 +40,8 @@ export class WorkerPool extends EventEmitter { constructor(options: WorkerPoolOptions) { super(); - this.maxInstances = options.maxWorkers || cpus().length; + // cpus() can return [] on some platforms, and we always need at least 1 worker + this.maxInstances = options.maxWorkers || cpus().length || 1; this.filePath = options.filePath; this.on(freeWorker, () => { diff --git a/packages/terser/test/test.js b/packages/terser/test/test.js index 316fb2288..9dabacd15 100644 --- a/packages/terser/test/test.js +++ b/packages/terser/test/test.js @@ -1,3 +1,5 @@ +const os = require('os'); + const test = require('ava'); const { rollup } = require('rollup'); @@ -356,3 +358,21 @@ test.serial('terser preserve vars in nameCache when provided', async (t) => { } }); }); + +test.serial('minify when os.cpus().length === 0', async (t) => { + const original = os.cpus; + os.cpus = () => []; + 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.cpus = original; + } +});