Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with ember-auto-import. #392

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1137,7 +1137,7 @@ describe('ember-cli-babel', function() {
});
});

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

it('disables reading `.babelrc`', function() {
Expand Down Expand Up @@ -1195,14 +1195,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