Skip to content

Commit

Permalink
Merge pull request emberjs#193 from nlfurniss/babel-options
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue authored Oct 13, 2020
2 parents 5d9b3a1 + c68fb21 commit 6e2f4c5
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 273 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ let featureFlagPlugin = applyFeatureFlags({
});

let scriptTree = esTranspiler(inputTree, {
plugins: [
featureFlagPlugin
]
babel: {
plugins: [
featureFlagPlugin
]
}
});
```

Expand Down Expand Up @@ -206,10 +208,12 @@ let somePlugin = {
});

let scriptTree = esTranspiler(inputTree, {
plugins: [
'transform-strict-mode', // plugins that are given as strings will automatically be parallelized
somePlugin
]
babel: {
plugins: [
'transform-strict-mode', // plugins that are given as strings will automatically be parallelized
somePlugin
]
}
});
```

Expand Down Expand Up @@ -243,7 +247,9 @@ let somePlugin = {
});

let scriptTree = esTranspiler(inputTree, {
plugins: [ somePlugin ]
babel: {
plugins: [ somePlugin ]
}
});
```

Expand Down Expand Up @@ -279,7 +285,9 @@ let somePlugin = {
});

let scriptTree = esTranspiler(inputTree, {
plugins: [ somePlugin ]
babeel: {
plugins: [ somePlugin ]
}
});
```

Expand Down
27 changes: 5 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

const Filter = require('broccoli-persistent-filter');
const clone = require('clone');
const path = require('path');
const mergeTrees = require('broccoli-merge-trees');
const funnel = require('broccoli-funnel');
const { transformString } = require('./lib/parallel-api');
const { transformIsParallelizable } = require('./lib/parallel-api');
const optionsHash = require('./lib/options-hash');
Expand Down Expand Up @@ -38,35 +35,21 @@ module.exports = class Babel extends Filter {

super(inputTree, options);

delete options.persist;
delete options.async;
delete options.annotation;
delete options.description;

this._optionsHash = null;
this.console = options.console || console;
this.throwUnlessParallelizable = options.throwUnlessParallelizable;

delete options.console;
delete options.throwUnlessParallelizable;

this.inputTree = inputTree;
this.options = options;
this.extensions = this.options.filterExtensions || ['js'];
this.targetExtension = 'js';
this.extensionsRegex = getExtensionsRegex(this.extensions);
this.name = 'broccoli-babel-transpiler';


if (this.options.helperWhiteList) {
this.helperWhiteList = this.options.helperWhiteList;
}

// Note, Babel does not support this option so we must save it then
// delete it from the options hash
delete this.options.helperWhiteList;

let { isParallelizable, errors } = transformIsParallelizable(options);
let { isParallelizable, errors } = transformIsParallelizable(options.babel);

heimdall.statsFor('babel').isParallelizable = isParallelizable;

Expand Down Expand Up @@ -107,13 +90,13 @@ module.exports = class Babel extends Filter {

let options = this.copyOptions();

options.filename = options.sourceFileName = relativePath;
options.babel.filename = options.babel.sourceFileName = relativePath;

if (options.moduleId === true) {
options.moduleId = replaceExtensions(this.extensionsRegex, options.filename);
if (options.babel.moduleId === true) {
options.babel.moduleId = replaceExtensions(this.extensionsRegex, options.babel.filename);
}

let optionsObj = { 'babel' : options, 'cacheKey' : this._optionsHash};
let optionsObj = { babel: options.babel, cacheKey: this._optionsHash};
return this.transform(string, optionsObj)
.then(transpiled => {
if (this.helperWhiteList) {
Expand Down
15 changes: 7 additions & 8 deletions lib/options-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ module.exports = function optionsHash(options, console) {
const hash = { plugins: [] };

for (let key in options) {
if (key === 'plugins') {
continue;
}
const value = options[key];
hash[key] = (typeof value === 'function') ? (value + '') : value;
}

if (options.plugins) {
if (!Array.isArray(options.plugins)) {
throw new TypeError('broccoli-babel-transpiler: babel options.plugins must either be omitted or an array');
const babelPlugins = options.babel.plugins;

if (babelPlugins) {
if (!Array.isArray(babelPlugins)) {
throw new TypeError('broccoli-babel-transpiler: babel options.babel.plugins must either be omitted or an array');
}

const cacheableItems = [];

for (let i = 0; i < options.plugins.length; i++) {
const plugin = options.plugins[i];
for (let i = 0; i < babelPlugins.length; i++) {
const plugin = babelPlugins[i];

if (isAbsolutePathPlugin(plugin)) {
const pluginPath = typeof plugin === 'string' ? plugin : plugin[0];
Expand Down
14 changes: 7 additions & 7 deletions lib/parallel-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ function pluginsAreParallelizable(plugins) {
};
}

function callbacksAreParallelizable(options) {
const callbacks = Object.keys(options).filter(key => typeof options[key] === 'function');
function callbacksAreParallelizable(babelOptions) {
const callbacks = Object.keys(babelOptions).filter(key => typeof babelOptions[key] === 'function');
let isParallelizable = true;
const errors = [];

for (let i = 0; i < callbacks.length; i++) {
let callback = options[callbacks[i]];
let callback = babelOptions[callbacks[i]];
if (implementsParallelAPI(callback) === false) {
isParallelizable = false;
errors.push(humanizePlugin(callback))
Expand All @@ -96,12 +96,12 @@ function callbacksAreParallelizable(options) {
return { isParallelizable, errors };
}

function transformIsParallelizable(options) {
const plugins = pluginsAreParallelizable(options.plugins);
const callbacks = callbacksAreParallelizable(options);
function transformIsParallelizable(babelOptions) {
const plugins = pluginsAreParallelizable(babelOptions.plugins);
const callbacks = callbacksAreParallelizable(babelOptions);

return {
isParallelizable: isSerializable(options),
isParallelizable: isSerializable(babelOptions),
errors: [].concat(plugins.errors, callbacks.errors)
};
}
Expand Down
22 changes: 12 additions & 10 deletions tests/test-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ describe('large operations', function() {

it('handles thousands of files', function () {
return babel(inputTreePath, {
inputSourceMap:false,
sourceMap: false,
plugins: [
{
_parallelBabel: {
requireFile: fixtureFullPath('transform-strict-mode-parallel'),
}
},
'@babel/transform-block-scoping'
]
babel: {
inputSourceMap:false,
sourceMap: false,
plugins: [
{
_parallelBabel: {
requireFile: fixtureFullPath('transform-strict-mode-parallel'),
}
},
'@babel/transform-block-scoping'
]
}
}).then(results => {
let outputPath = results.directory;

Expand Down
Loading

0 comments on commit 6e2f4c5

Please sign in to comment.