diff --git a/index.js b/index.js index 79dd017..8f8254c 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,9 @@ function Babel(inputTree, _options) { delete options.description; this.console = options.console || console; + this.throwUnlessParallelizable = options.throwUnlessParallelizable; delete options.console; + delete options.throwUnlessParallelizable; this.options = options; this.extensions = this.options.filterExtensions || ['js']; @@ -78,7 +80,9 @@ Babel.prototype.baseDir = function() { }; Babel.prototype.transform = function(string, options) { - return transformString(string, options); + return transformString(string, options, { + throwUnlessParallelizable: this.throwUnlessParallelizable + }); }; /* diff --git a/lib/parallel-api.js b/lib/parallel-api.js index 90a011c..0d84f56 100644 --- a/lib/parallel-api.js +++ b/lib/parallel-api.js @@ -96,8 +96,12 @@ function deserializeOptions(options) { // transform callbacks Object.keys(options).forEach(key => { - if (implementsParallelAPI(options[key])) { - newOptions[key] = buildFromParallelApiInfo(options[key]._parallelBabel); + let value = options[key]; + if (value === undefined) { + return; + } + if (implementsParallelAPI(value)) { + newOptions[key] = buildFromParallelApiInfo(value._parallelBabel); } }); @@ -129,17 +133,17 @@ function serializeOptions(options) { return serializableOptions; } -function transformString(string, options) { - const isParallelizable = transformIsParallelizable(options); +function transformString(string, babelOptions, buildOptions) { + const isParallelizable = transformIsParallelizable(babelOptions); - if (options.throwUnlessParallelizable && !isParallelizable) { + if (buildOptions !== null && typeof buildOptions === 'object' && buildOptions.throwUnlessParallelizable && !isParallelizable) { throw new Error('BroccoliBabelTranspiler was configured to `throwUnlessParallelizable` and was unable to parallelize an plugin. Please see: https://github.com/babel/broccoli-babel-transpiler#parallel-transpilation for more details'); } if (JOBS > 1 && isParallelizable) { let pool = getWorkerPool(); _logger.debug('transformString is parallelizable'); - return pool.exec('transform', [string, serializeOptions(options)]); + return pool.exec('transform', [string, serializeOptions(babelOptions)]); } else { if (JOBS <= 1) { _logger.debug('JOBS <= 1, skipping worker, using main thread'); @@ -148,7 +152,7 @@ function transformString(string, options) { } return new Promise(resolve => { - resolve(transpiler.transform(string, deserializeOptions(options))); + resolve(transpiler.transform(string, deserializeOptions(babelOptions))); }); } } diff --git a/tests/test.js b/tests/test.js index 4b70914..c0225ba 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1334,27 +1334,24 @@ describe('workerpool', function() { describe('throwUnlessParallelizable', function() { it('should throw if throwUnlessParallelizable: true, and one or more plugins could not be parallelized', function() { - options.throwUnlessParallelizable = true; options.plugins = [function() { }]; - expect(() => require(PATH).transformString(stringToTransform, options)).to.throw(/BroccoliBabelTranspiler was configured to `throwUnlessParallelizable` and was unable to parallelize/); + expect(() => require(PATH).transformString(stringToTransform, options, { throwUnlessParallelizable: true })).to.throw(/BroccoliBabelTranspiler was configured to `throwUnlessParallelizable` and was unable to parallelize/); }); it('should NOT throw if throwUnlessParallelizable: true, and all plugins can be parallelized', function() { - options.throwUnlessParallelizable = true; options.plugins = [ { foo: 1 }]; - expect(() => require(PATH).transformString(stringToTransform, options)).to.throw(/BroccoliBabelTranspiler was configured to `throwUnlessParallelizable` and was unable to parallelize/); + expect(() => require(PATH).transformString(stringToTransform, options, { throwUnlessParallelizable: true })).to.throw(/BroccoliBabelTranspiler was configured to `throwUnlessParallelizable` and was unable to parallelize/); }); it('should NOT throw if throwUnlessParallelizable: false, and one or more plugins could not be parallelized', function() { - options.throwUnlessParallelizable = false options.plugins = [function() { }] - expect(() => require(PATH).transformString(stringToTransform, options)).to.not.throw(); + expect(() => require(PATH).transformString(stringToTransform, options, { throwUnlessParallelizable: false })).to.not.throw(); }); it('should NOT throw if throwUnlessParallelizable is unset, and one or more plugins could not be parallelized', function() { - delete options.throwUnlessParallelizable; - options.plugins = [function() { }] - expect(() => require(PATH).transformString(stringToTransform, options)).to.not.throw(); + expect(() => require(PATH).transformString(stringToTransform, options, { throwUnlessParallelizable: undefined })).to.not.throw(); + expect(() => require(PATH).transformString(stringToTransform, options, { throwUnlessParallelizable: null })).to.not.throw(); + expect(() => require(PATH).transformString(stringToTransform, options, { })).to.not.throw(); }); }); });