Skip to content

Commit

Permalink
Merge pull request emberjs#140 from babel/refactor-throw-unless-
Browse files Browse the repository at this point in the history
refactor `throwUnlessParallelizable` option internals
  • Loading branch information
stefanpenner authored May 22, 2018
2 parents ce7806c + 33d8c7d commit 7bee87b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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
});
};

/*
Expand Down
19 changes: 12 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,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');
Expand All @@ -148,7 +153,7 @@ function transformString(string, options) {
}

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

0 comments on commit 7bee87b

Please sign in to comment.