Skip to content

Commit

Permalink
Merge pull request #392 from babel/make-buildBabelOptions-backwards-c…
Browse files Browse the repository at this point in the history
…ompat

Fix compatibility with ember-auto-import.
  • Loading branch information
rwjblue committed Mar 18, 2021
2 parents be2a598 + 7267b2c commit cc65ea5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ interface EmberCLIBabel {
/**
Used to generate the options that will ultimately be passed to babel itself.
*/
buildBabelOptions(config?: EmberCLIBabelConfig): Opaque;
buildBabelOptions(type: 'babel' | 'broccoli', config?: EmberCLIBabelConfig): Opaque;

/**
Supports easier transpilation of non-standard input paths (e.g. to transpile
Expand All @@ -407,7 +407,7 @@ interface EmberCLIBabel {
let babelAddon = this.addons.find(addon => addon.name === 'ember-cli-babel');

// create the babel options to use elsewhere based on the config above
let options = babelAddon.buildBabelOptions(config)
let options = babelAddon.buildBabelOptions('babel', config)

// now you can pass these options off to babel or broccoli-babel-transpiler
require('babel-core').transform('something', options);
Expand Down
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,25 @@ module.exports = {
}
},

buildBabelOptions(_config) {
buildBabelOptions(configOrType, _config) {
let resultType;

if (typeof configOrType !== 'string') {
_config = configOrType;
resultType = 'broccoli';
} else if (configOrType === 'broccoli') {
resultType = 'broccoli';
} else if (configOrType === 'babel') {
resultType = 'babel';
}

let config = _config || this._getAddonOptions();

const customAddonConfig = config['ember-cli-babel'];
const shouldUseBabelConfigFile = customAddonConfig && customAddonConfig.useBabelConfig;

let options;

if (shouldUseBabelConfigFile) {
let babelConfig = babel.loadPartialConfig({
root: this.parent.root,
Expand All @@ -69,9 +82,16 @@ module.exports = {

// If the babel config file is found, then pass the path into the options for the transpiler
// parse and leverage the same.
return { configFile: babelConfig.config };
options = { configFile: babelConfig.config };
} else {
options = getBabelOptions(config, this);
}

if (resultType === 'babel') {
return options;
} else {
return getBabelOptions(config, this);
// legacy codepath
return Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), options);
}
},

Expand Down Expand Up @@ -128,7 +148,7 @@ module.exports = {
let config = _config || this._getAddonOptions();
let description = `000${++count}`.slice(-3);
let postDebugTree = this._debugTree(inputTree, `${description}:input`);
let options = Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), this.buildBabelOptions(config));
let options = Object.assign({}, this._buildBroccoliBabelTranspilerOptions(config), this.buildBabelOptions('babel', config));
let output;

const customAddonConfig = config['ember-cli-babel'];
Expand Down
49 changes: 47 additions & 2 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ describe('ember-cli-babel', function() {
});
});

describe('buildBabelOptions', function() {
describe('_buildBroccoliBabelTranspilerOptions', function() {
this.timeout(0);

it('disables reading `.babelrc`', function() {
Expand Down Expand Up @@ -1215,14 +1215,59 @@ describe('ember-cli-babel', function() {

expect(result.babelrc).to.be.false;
});
});

describe('buildBabelOptions', function() {
this.timeout(0);

it('returns broccoli-babel-transpiler options by default', function() {
let result = this.addon.buildBabelOptions();

expect(result.moduleIds).to.be.true;
expect(result.annotation).to.be;
expect(result.babelrc).to.be.false;
expect(result.configFile).to.be.false;
});

it('returns broccoli-babel-transpiler options when asked for', function() {
let result = this.addon.buildBabelOptions('broccoli');

expect(result.moduleIds).to.be.true;
expect(result.annotation).to.be;
expect(result.babelrc).to.be.false;
expect(result.configFile).to.be.false;
});

it('returns broccoli-babel-transpiler options with customizations when provided', function() {
let result = this.addon.buildBabelOptions('broccoli', {
'ember-cli-babel': {
annotation: 'hello!!!',
}
});

expect(result.annotation).to.equal('hello!!!');
expect(result.moduleIds).to.be.true;
expect(result.annotation).to.be;
expect(result.babelrc).to.be.false;
expect(result.configFile).to.be.false;
});

it('returns babel options when asked for', function() {
let result = this.addon.buildBabelOptions('babel');

expect('moduleIds' in result).to.be.false;
expect('annotation' in result).to.be.false;
expect('babelrc' in result).to.be.false;
expect('configFile' in result).to.be.false;
});

it('does not include all provided options', function() {
let babelOptions = { blah: true };
let options = {
babel: babelOptions
};

let result = this.addon.buildBabelOptions(options);
let result = this.addon.buildBabelOptions('babel', options);
expect(result.blah).to.be.undefined;
});

Expand Down

0 comments on commit cc65ea5

Please sign in to comment.