Skip to content

Commit

Permalink
Use correct configKey for ember-cli-babel specific options.
Browse files Browse the repository at this point in the history
Newer versions of ember-cli-babel will begin deprecating passing
any options in `babel` config hash that are not destined for `babel`
itself (this includes `compileModules` and `includePolyfill`).

This detects the newer version and uses the appropriate (non-deprecated)
configuration property.

It also moves `findAddonByName` into a shared utility method (to ensure
both project and addon use the same mechanism).
  • Loading branch information
rwjblue committed Dec 6, 2016
1 parent 43fe7f3 commit fc8bf2b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 35 deletions.
16 changes: 13 additions & 3 deletions lib/models/addon.js
Expand Up @@ -28,6 +28,7 @@ var walkSync = require('walk-sync');
var ensurePosixPath = require('ensure-posix-path');
var defaultsDeep = require('ember-cli-lodash-subset').defaultsDeep;
var Babel = require('broccoli-babel-transpiler');
var findAddonByName = require('../utilities/find-addon-by-name');

function warn(message) {
if (this.ui) {
Expand Down Expand Up @@ -141,16 +142,25 @@ var Addon = CoreObject.extend({
throw new SilentError('An addon must define a `name` property.');
}

// future versions of ember-cli-babel will be moving the location for its
// own configuration options out of `babel` and will be issuing a deprecation
// if used in the older way
//
// see: https://github.com/babel/ember-cli-babel/pull/105
var emberCLIBabelInstance = findAddonByName(this.addons, 'ember-cli-babel');
var emberCLIBabelConfigKey = (emberCLIBabelInstance && emberCLIBabelInstance.configKey) || 'babel';

this.options = defaultsDeep(this.options, {
babel: {
// TODO: coordinate with @turbo87 on exactly how to do
// this without issuing a deprecation
compileModules: true,
modules: 'amdStrict',
moduleIds: true,
resolveModuleSource: require('amd-name-resolver').moduleResolve
}
});

this.options[emberCLIBabelConfigKey] = defaultsDeep(this.options[emberCLIBabelConfigKey], {
compileModules: true
});
},

hintingEnabled: function() {
Expand Down
9 changes: 2 additions & 7 deletions lib/models/project.js
Expand Up @@ -14,6 +14,7 @@ var logger = require('heimdalljs-logger')('ember-cli:project');
var nodeModulesPath = require('node-modules-path');
var versionUtils = require('../utilities/version-utils');
var emberCLIVersion = versionUtils.emberCLIVersion;
var findAddonByName = require('../utilities/find-addon-by-name');

/**
The Project model is tied to your package.json. It is instantiated
Expand Down Expand Up @@ -524,13 +525,7 @@ Project.prototype.reloadAddons = function() {
Project.prototype.findAddonByName = function(name) {
this.initializeAddons();

function matchAddon(name, addon) {
return name === addon.name || (addon.pkg && name === addon.pkg.name);
}

return _.find(this.addons, function(addon) {
return matchAddon(name, addon);
});
return findAddonByName(this.addons, name);
};

/**
Expand Down
13 changes: 13 additions & 0 deletions lib/utilities/find-addon-by-name.js
@@ -0,0 +1,13 @@
'use strict';

var find = require('ember-cli-lodash-subset').find;

module.exports = function findAddonByName(addons, name) {
function matchAddon(name, addon) {
return name === addon.name || (addon.pkg && name === addon.pkg.name);
}

return find(addons, function(addon) {
return matchAddon(name, addon);
});
};
29 changes: 4 additions & 25 deletions tests/unit/models/project-test.js
Expand Up @@ -436,35 +436,14 @@ describe('models/project.js', function() {
td.verify(project.initializeAddons(), {ignoreExtraArgs: true});
});

it('should return the foo addon from name', function() {
var addon = project.findAddonByName('foo');
it('generally should work and defer to findAddonByName utlity', function() {
var addon;
addon = project.findAddonByName('foo');
expect(addon.name).to.equal('foo', 'should have found the foo addon');
});

it('should return the foo-bar addon from name when a foo also exists', function() {
var addon = project.findAddonByName('foo-bar');
expect(addon.name).to.equal('foo-bar', 'should have found the foo-bar addon');
});

it('should return the bar-pkg addon from package name', function() {
var addon = project.findAddonByName('bar-pkg');
addon = project.findAddonByName('bar-pkg');
expect(addon.pkg.name).to.equal('bar-pkg', 'should have found the bar-pkg addon');
});

it('should return undefined if addon doesn\'t exist', function() {
var addon = project.findAddonByName('not-an-addon');
expect(addon).to.equal(undefined, 'not found addon should be undefined');
});

it('should not return an addon that is a substring of requested name', function() {
var addon = project.findAddonByName('foo-ba');
expect(addon).to.equal(undefined, 'foo-ba should not be found');
});

it('should not guess addon name from string with slashes', function() {
var addon = project.findAddonByName('qux/foo');
expect(addon).to.equal(undefined, 'should not have found the foo addon');
})
});

describe('bowerDirectory', function() {
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/utilities/find-addon-by-name-test.js
@@ -0,0 +1,49 @@
'use strict';

var expect = require('chai').expect;
var findAddonByName = require('../../../lib/utilities/find-addon-by-name');

describe('findAddonByName', function() {
var addons;
beforeEach(function() {
addons = [{
name: 'foo',
pkg: { name: 'foo' }
}, {
pkg: { name: 'bar-pkg' }
}, {
name: 'foo-bar',
pkg: { name: 'foo-bar' }
}];
});

it('should return the foo addon from name', function() {
var addon = findAddonByName(addons, 'foo');
expect(addon.name).to.equal('foo', 'should have found the foo addon');
});

it('should return the foo-bar addon from name when a foo also exists', function() {
var addon = findAddonByName(addons, 'foo-bar');
expect(addon.name).to.equal('foo-bar', 'should have found the foo-bar addon');
});

it('should return the bar-pkg addon from package name', function() {
var addon = findAddonByName(addons, 'bar-pkg');
expect(addon.pkg.name).to.equal('bar-pkg', 'should have found the bar-pkg addon');
});

it('should return undefined if addon doesn\'t exist', function() {
var addon = findAddonByName(addons, 'not-an-addon');
expect(addon).to.equal(undefined, 'not found addon should be undefined');
});

it('should not return an addon that is a substring of requested name', function() {
var addon = findAddonByName(addons, 'foo-ba');
expect(addon).to.equal(undefined, 'foo-ba should not be found');
});

it('should not guess addon name from string with slashes', function() {
var addon = findAddonByName(addons, 'qux/foo');
expect(addon).to.equal(undefined, 'should not have found the foo addon');
})
});

0 comments on commit fc8bf2b

Please sign in to comment.