From 33d8c7d509e46f7c8ac6f68d2237c0b63022100d Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Tue, 22 May 2018 10:58:07 -0700 Subject: [PATCH] refactor `throwUnlessParallelizable` option internals --- index.js | 8 +++++++- lib/parallel-api.js | 19 ++++++++++++------- tests/test.js | 18 +++++++++--------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 79dd017..d5745f3 100644 --- a/index.js +++ b/index.js @@ -42,7 +42,10 @@ 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 +81,10 @@ Babel.prototype.baseDir = function() { }; Babel.prototype.transform = function(string, options) { - return transformString(string, options); + debugger; + return transformString(string, options, { + throwUnlessParallelizable: this.throwUnlessParallelizable + }); }; /* diff --git a/lib/parallel-api.js b/lib/parallel-api.js index 90a011c..8c8f811 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,18 @@ function serializeOptions(options) { return serializableOptions; } -function transformString(string, options) { - const isParallelizable = transformIsParallelizable(options); +function transformString(string, babelOptions, buildOptions) { + const isParallelizable = transformIsParallelizable(babelOptions); + debugger; - 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 +153,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..ceb74a7 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1333,28 +1333,28 @@ describe('workerpool', function() { }); describe('throwUnlessParallelizable', function() { + afterEach(function() { + return terminateWorkerPool(); + }); + 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, { })).to.not.throw(); }); }); });