Skip to content

Commit

Permalink
refactor throwUnlessParallelizable option internals
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed May 22, 2018
1 parent ce7806c commit c194f92
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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
});
};

/*
Expand Down
18 changes: 11 additions & 7 deletions lib/parallel-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -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');
Expand All @@ -148,7 +152,7 @@ function transformString(string, options) {
}

return new Promise(resolve => {
resolve(transpiler.transform(string, deserializeOptions(options)));
resolve(transpiler.transform(string, deserializeOptions(babelOptions)));
});
}
}
Expand Down
15 changes: 6 additions & 9 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit c194f92

Please sign in to comment.