Skip to content

Commit

Permalink
Ensure external helpers are not used when modules are not transpiled.
Browse files Browse the repository at this point in the history
When using something akin to the following somewhat common pattern:

```js
let babel = this.addons.find(addon => addon.name === 'ember-cli-babel');
let tree = babel.transpileTree(input, {
  'ember-cli-babel': {
    compileModules: false,
  }
});
```

The previous logic would still attempt to use external helpers even when
modules themselves are not being transpiled. This meant that the final
output would include untranspiled `import` statements.

This affects common addons such as:

* ember-fetch
* ember-service-worker
* ember-data
* @ember/test-helpers
* ember-angle-bracket-invocation-polyfill
* ember-modifier-manager-polyfill

---

The fix here is to detect that modules will not be compiled, and avoid
usage of the external helpers.

This also moves the validation that parent addon's do not specify
`includeExternalHelpers` in their own config to the shared
`getAddonConfig` method (and makes the logic in that method a bit easier
to grok).
  • Loading branch information
rwjblue committed Feb 12, 2019
1 parent a2c2adb commit cada0c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
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

0 comments on commit cada0c1

Please sign in to comment.