How can an addon find the Ember version of a host app?


#1

I’m writing an addon and I need to know at runtime which version of Ember the host app is using. I’ve got myself in a funny situation. I can think of a few ways to proceed, but I just wanted to do a quick gut-check here.

My addon’s test suite runs against multiple Ember versions with ember-try.

When running tests against 3.1 I need to use

import Ember from 'ember';

console.log(Ember.VERSION);

When running tests against 2.16 I need to use

import { VERSION } from `@ember/version`;

console.log(VERSION);

If I try to use the 3.1 way in 2.16 I get an ember eslint error. It wants me to use the new modules syntax.

If I try to use the 2.16 way in 3.1 I get a module not found error.

I’m working around this now, but I’m wondering how addon authors approach these sort of issues when working with multiple Ember versions? Also, is there an opportunity for me to provide a polyfill here?


#2

What do you need to do with the version? Generally, speaking I’ve only seen this pattern used to detect which version is used in order to have some conditional logic when the version is above or below some specific version. Is that what you are doing?

If so, you really really want to be using the ember-compatibility-helpersaddon. It allows you to have code akin to the following and ensure that users don’t pay for the extra size of code supporting versions they aren’t using:

import { gte } from ‘ember-compatibility-helpers’;

let someFunc;
if (gte('3.2')) {
  someFunc = () => { /* use your imagination */ };
} else if (gte('2.12.0')) {
  someFunc = () => { /* use your imagination */ };
}
export default someFunc;