Skip to content

Commit

Permalink
fix(terser): always make at least 1 worker thread (#1594)
Browse files Browse the repository at this point in the history
  • Loading branch information
easrng committed Oct 8, 2023
1 parent 841a039 commit bfacac4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/terser/src/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
20 changes: 20 additions & 0 deletions packages/terser/test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const os = require('os');

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

Expand Down Expand Up @@ -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;
}
});

0 comments on commit bfacac4

Please sign in to comment.