diff --git a/index.js b/index.js index 5693c567..b503cfb7 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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'); @@ -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() { @@ -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; diff --git a/node-tests/addon-test.js b/node-tests/addon-test.js index 9c39721e..0cc30c45 100644 --- a/node-tests/addon-test.js +++ b/node-tests/addon-test.js @@ -623,13 +623,13 @@ 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() { @@ -637,7 +637,18 @@ describe('ember-cli-babel', function() { 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() { @@ -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() { @@ -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() { @@ -685,7 +696,7 @@ describe('ember-cli-babel', function() { } }); - expect(this.addon._shouldIncludeHelpers()).to.be.false; + expect(this.addon._shouldIncludeHelpers({})).to.be.false; }); }) });