Skip to content

Commit 510915e

Browse files
authored
Merge pull request #64 from mikrostew/jobs-override
Bugfix: JOBS environment variable should override concurrency
2 parents b7137fb + 49bc43c commit 510915e

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-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: 3 additions & 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 });
@@ -118,8 +117,10 @@ UglifyWriter.prototype.build = function () {
118117
UglifyWriter.prototype.processFile = function(inFile, outFile, relativePath, outDir) {
119118
// don't run this in the workerpool if concurrency is disabled (can set JOBS <= 1)
120119
if (this.async && this.concurrency > 1) {
120+
debug('running in workerpool, concurrency=%d', this.concurrency);
121121
// each of these arguments is a string, which can be sent to the worker process as-is
122122
return this.pool.exec('processFileParallel', [inFile, outFile, relativePath, outDir, silent, this.options]);
123123
}
124+
debug('not running in workerpool');
124125
return processFile(inFile, outFile, relativePath, outDir, silent, this.options);
125126
};

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)