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

Ensure external helpers are not used when modules are not transpiled. #267

Merged
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
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,16 @@ module.exports = {
}
},

_shouldIncludeHelpers() {
let customAddonOptions = this.parent && this.parent.options && this.parent.options['ember-cli-babel'];

if (customAddonOptions && 'includeExternalHelpers' in customAddonOptions) {
throw new Error('includeExternalHelpers is not supported in addon configurations, it is an app-wide configuration option');
}

_shouldIncludeHelpers(options) {
let appOptions = this._getAppOptions();
let customOptions = appOptions['ember-cli-babel'];

let shouldIncludeHelpers = false;

if (customOptions && 'includeExternalHelpers' in customOptions) {
if (!this._shouldCompileModules(options)) {
// we cannot use external helpers if we are not transpiling modules
return false;
} else if (customOptions && 'includeExternalHelpers' in customOptions) {
shouldIncludeHelpers = customOptions.includeExternalHelpers === true;
} else {
// Check the project to see if we should include helpers based on heuristics.
Expand Down Expand Up @@ -147,7 +144,10 @@ module.exports = {
treeForAddon() {
// Helpers are a global config, so only the root application should bother
// generating and including the file.
if (!(this.parent === this.project && this._shouldIncludeHelpers())) return;
let isRootBabel = this.parent === this.project;
let shouldIncludeHelpers = isRootBabel && this._shouldIncludeHelpers(this._getAppOptions());

if (!shouldIncludeHelpers) { return; }

const path = require('path');
const Funnel = require('broccoli-funnel');
Expand Down Expand Up @@ -206,7 +206,18 @@ module.exports = {
},

_getAddonOptions() {
return (this.parent && this.parent.options) || (this.app && this.app.options) || {};
let parentOptions = this.parent && this.parent.options;
let appOptions = this.app && this.app.options;

if (parentOptions) {
let customAddonOptions = parentOptions['ember-cli-babel'];

if (customAddonOptions && 'includeExternalHelpers' in customAddonOptions) {
throw new Error('includeExternalHelpers is not supported in addon configurations, it is an app-wide configuration option');
}
}

return parentOptions || appOptions || {};
},

_getAppOptions() {
Expand Down Expand Up @@ -250,7 +261,7 @@ module.exports = {
_getBabelOptions(config) {
let addonProvidedConfig = this._getAddonProvidedConfig(config);
let shouldCompileModules = this._shouldCompileModules(config);
let shouldIncludeHelpers = this._shouldIncludeHelpers();
let shouldIncludeHelpers = this._shouldIncludeHelpers(config);

let emberCLIBabelConfig = config['ember-cli-babel'];
let shouldRunPresetEnv = true;
Expand Down
25 changes: 18 additions & 7 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,21 +623,32 @@ describe('ember-cli-babel', function() {
});

it('should return false without any includeExternalHelpers option set', function() {
expect(this.addon._shouldIncludeHelpers()).to.be.false;
expect(this.addon._shouldIncludeHelpers({})).to.be.false;
});

it('should throw an error with ember-cli-babel.includeExternalHelpers = true in parent', function() {
this.addon.parent.options = { 'ember-cli-babel': { includeExternalHelpers: true } };

expect(this.addon._shouldIncludeHelpers).to.throw;
expect(() => this.addon._shouldIncludeHelpers({})).to.throw;
});

it('should return true with ember-cli-babel.includeExternalHelpers = true in app and ember-cli-version is high enough', function() {
this.addon.pkg = { version: '7.3.0-beta.1' };

this.addon.app.options = { 'ember-cli-babel': { includeExternalHelpers: true } };

expect(this.addon._shouldIncludeHelpers()).to.be.true;
expect(this.addon._shouldIncludeHelpers({})).to.be.true;
});

it('should return false when compileModules is false', function() {
this.addon.pkg = { version: '7.3.0-beta.1' };

this.addon.app.options = { 'ember-cli-babel': { includeExternalHelpers: true } };

// precond
expect(this.addon._shouldIncludeHelpers({})).to.be.true;

expect(this.addon._shouldIncludeHelpers({ 'ember-cli-babel': { compileModules: false } })).to.be.false;
});

it('should return false with ember-cli-babel.includeExternalHelpers = true in app and write warn line if ember-cli-version is not high enough', function() {
Expand All @@ -650,13 +661,13 @@ describe('ember-cli-babel', function() {

this.addon.app.options = { 'ember-cli-babel': { includeExternalHelpers: true } };

expect(this.addon._shouldIncludeHelpers()).to.be.false;
expect(this.addon._shouldIncludeHelpers({})).to.be.false;
});

it('should return false with ember-cli-babel.includeExternalHelpers = false in host', function() {
this.addon.app.options = { 'ember-cli-babel': { includeExternalHelpers: false } };

expect(this.addon._shouldIncludeHelpers()).to.be.false;
expect(this.addon._shouldIncludeHelpers({})).to.be.false;
});

describe('autodetection', function() {
Expand All @@ -668,7 +679,7 @@ describe('ember-cli-babel', function() {
}
});

expect(this.addon._shouldIncludeHelpers()).to.be.true;
expect(this.addon._shouldIncludeHelpers({})).to.be.true;
});

it('should return false if @ember-decorators/babel-transforms exists and write warn line if ember-cli-version is not high enough', function() {
Expand All @@ -685,7 +696,7 @@ describe('ember-cli-babel', function() {
}
});

expect(this.addon._shouldIncludeHelpers()).to.be.false;
expect(this.addon._shouldIncludeHelpers({})).to.be.false;
});
})
});
Expand Down