Skip to content

Commit 49bc43c

Browse files
committed
fix JOBS override and add tests
1 parent f12ced6 commit 49bc43c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@ var uglified = uglify(input);
2020
// advanced usage
2121
var uglified = uglify(input, {
2222
exclude: [..], // array of globs, to not minify
23-
23+
2424
uglify: {
2525
mangle: false, // defaults to true
2626
compress: false, // defaults to true
2727
sourceMap: false, // defaults to true
2828
//...
29-
}
29+
},
30+
31+
async: true, // run uglify in parallel, defaults to false
32+
concurrency: 3 // number of parallel workers, defaults to number of CPUs - 1
3033
});
3134
```
35+
36+
To disable parallelization:
37+
38+
```
39+
$ JOBS=0
40+
$ JOBS=1
41+
```

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ function UglifyWriter (inputNodes, options) {
4141
});
4242

4343
// consumers of this plugin can opt-in to async and concurrent behavior
44-
// TODO docs in the README
4544
this.async = (this.options.async === true);
46-
this.concurrency = this.options.concurrency || Number(process.env.JOBS) || Math.max(require('os').cpus().length - 1, 1);
45+
this.concurrency = Number(process.env.JOBS) || this.options.concurrency || Math.max(require('os').cpus().length - 1, 1);
4746

4847
// create a worker pool using an external worker script
4948
this.pool = workerpool.pool(path.join(__dirname, 'lib', 'worker.js'), { maxWorkers: this.concurrency });

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,31 @@ let { bar } = Foo.prototype;`,
127127
});
128128
});
129129

130+
describe('concurrency', function() {
131+
afterEach(function() {
132+
delete process.env.JOBS;
133+
});
134+
135+
it('defaults to CPUs-1 workers', async function() {
136+
var testUglify = new uglify(fixturesError, { async: true });
137+
138+
expect(testUglify.concurrency).toEqual(require('os').cpus().length - 1);
139+
});
140+
141+
it('sets concurrency using the option', async function() {
142+
var testUglify = new uglify(fixturesError, { async: true, concurrency: 145 });
143+
144+
expect(testUglify.concurrency).toEqual(145);
145+
});
146+
147+
it('overrides concurrency with JOBS env variable', async function() {
148+
process.env.JOBS = '7';
149+
var testUglify = new uglify(fixturesError, { async: true, concurrency: 145 });
150+
151+
expect(testUglify.concurrency).toEqual(7);
152+
});
153+
});
154+
130155
afterEach(async function() {
131156
if (input) {
132157
await input.dispose();

0 commit comments

Comments
 (0)