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
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
Fix compatibility with ember-auto-import.
[email protected] accidentally broke compatibility with
ember-auto-import when it fixed `buildBabelOptions` to only return
custom babel config **not** broccoli-babel-transpiler config.

This fixes that compatiblity in the case where
`babelAddon.buildBabelOptions()` is called, but still preserves the
ability to get _either_ the broccoli-babel-transpiler config _or_ the
`@babel/core` config.
rwjblue committed Mar 18, 2021

Verified

This commit was signed with the committer’s verified signature. The key has expired.
tvdeyen Thomas von Deyen
commit 7267b2cdc32283c2727e9b3814f5f4eb2e856a36
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
@@ -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);
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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,
@@ -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);
}
},

@@ -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'];
49 changes: 47 additions & 2 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
@@ -1137,7 +1137,7 @@ describe('ember-cli-babel', function() {
});
});

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

it('disables reading `.babelrc`', function() {
@@ -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;
});