Skip to content

Commit ab4bf74

Browse files
committed
Add emberjs/rfcs#176 support by default.
This replaces the new modules with globals usage when the Ember version in use does not include its own modules. As discussed in a recent ember-cli team meeting.
1 parent bf126fe commit ab4bf74

File tree

4 files changed

+749
-42
lines changed

4 files changed

+749
-42
lines changed

index.js

+19
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ module.exports = {
208208
options.plugins = [].concat(
209209
userPlugins,
210210
this._getDebugMacroPlugins(config),
211+
this._getEmberModulesAPIPolyfill(config),
211212
shouldCompileModules && this._getModulesPlugin(),
212213
this._getPresetEnvPlugins(addonProvidedConfig),
213214
userPostTransformPlugins
@@ -251,6 +252,18 @@ module.exports = {
251252
return [[DebugMacros, options]];
252253
},
253254

255+
_getEmberModulesAPIPolyfill(config) {
256+
let addonOptions = config['ember-cli-babel'] || {};
257+
258+
if (addonOptions.disableEmberModulesAPIPolyfill) { return; }
259+
260+
if (this._emberVersionRequiresModulesAPIPolyfill()) {
261+
const ModulesAPIPolyfill = require('babel-plugin-ember-modules-api-polyfill');
262+
263+
return [[ModulesAPIPolyfill]];
264+
}
265+
},
266+
254267
_getPresetEnvPlugins(config) {
255268
let options = config.options;
256269

@@ -329,4 +342,10 @@ module.exports = {
329342
}
330343
},
331344

345+
_emberVersionRequiresModulesAPIPolyfill() {
346+
// once a version of Ember ships with the
347+
// emberjs/rfcs#176 modules natively this will
348+
// be updated to detect that and return false
349+
return true;
350+
}
332351
};

node-tests/addon-test.js

+41
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,47 @@ describe('ember-cli-babel', function() {
7171
});
7272
}));
7373

74+
describe('ember modules API polyfill', function() {
75+
it("can opt-out via ember-cli-babel.disableEmberModulesAPIPolyfill", co.wrap(function* () {
76+
input.write({
77+
"foo.js": `import Component from '@ember/component';`
78+
});
79+
80+
subject = this.addon.transpileTree(input.path(), {
81+
'ember-cli-babel': {
82+
disableEmberModulesAPIPolyfill: true
83+
}
84+
});
85+
86+
output = createBuilder(subject);
87+
88+
yield output.build();
89+
90+
expect(
91+
output.read()
92+
).to.deep.equal({
93+
"foo.js": `define('foo', ['@ember/component'], function (_component) {\n 'use strict';\n});`
94+
});
95+
}));
96+
97+
it("should replace imports by default", co.wrap(function* () {
98+
input.write({
99+
"foo.js": `import Component from '@ember/component';`
100+
});
101+
102+
subject = this.addon.transpileTree(input.path());
103+
output = createBuilder(subject);
104+
105+
yield output.build();
106+
107+
expect(
108+
output.read()
109+
).to.deep.equal({
110+
"foo.js": `define('foo', [], function () {\n 'use strict';\n\n var Component = Ember.Component;\n});`
111+
});
112+
}));
113+
});
114+
74115
describe('debug macros', function() {
75116
it("can opt-out via ember-cli-babel.disableDebugTooling", co.wrap(function* () {
76117
process.env.EMBER_ENV = 'development';

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"amd-name-resolver": "0.0.6",
3939
"babel-plugin-debug-macros": "^0.1.10",
40+
"babel-plugin-ember-modules-api-polyfill": "^1.0.0",
4041
"babel-plugin-transform-es2015-modules-amd": "^6.24.0",
4142
"babel-polyfill": "^6.16.0",
4243
"babel-preset-env": "^1.5.1",

0 commit comments

Comments
 (0)