Skip to content

Commit 6e2f2f8

Browse files
NullVoxPopuliRobert Jackson
authored andcommitted
Add option to preserve import Ember from 'ember';
1 parent afdd503 commit 6e2f2f8

2 files changed

Lines changed: 71 additions & 4 deletions

File tree

__tests__/index-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,37 @@ describe('options', () => {
241241
expect(actual).toEqual(`var _x = Ember.assert;var _y = Ember.inspect;`);
242242
});
243243
});
244+
245+
describe('useEmberModule', () => {
246+
it(`adds the ember import when used in sub-modules`, () => {
247+
let input = `import Component from '@ember/component';export default class extends Component {}`;
248+
let actual = transform(input, [[Plugin, { useEmberModule: true }]]);
249+
let expected = `import ${Plugin.uniqueishGlobalName} from 'ember';\nexport default class extends Ember.Component {}`;
250+
251+
expect(actual).toEqual(expected);
252+
});
253+
254+
it(`keeps the ember import`, () => {
255+
let input = `import Ember from 'ember';let x = Ember;`;
256+
let actual = transform(input, [[Plugin, { useEmberModule: true }]]);
257+
258+
expect(actual).toEqual(input);
259+
});
260+
261+
it(`keeps the ember import when renamed`, () => {
262+
let input = `import BestFramework from 'ember';let x = BestFramework;`;
263+
let actual = transform(input, [[Plugin, { useEmberModule: true }]]);
264+
265+
expect(actual).toEqual(input);
266+
});
267+
268+
it(`import then export`, () => {
269+
let input = `import mbr from 'ember';export const Ember = mbr;`;
270+
let actual = transform(input, [[Plugin, { useEmberModule: true }]]);
271+
272+
expect(actual).toEqual(input);
273+
});
274+
});
244275
});
245276

246277
describe(`import from 'ember'`, () => {

src/index.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ function isDecorator(moduleName, importName) {
3131
}
3232
}
3333

34+
// what are the odds someone else defines this?
35+
const EmberGlobalImportName = '____EMBER_GLOBAL____';
36+
37+
function emberImport(t) {
38+
return t.importDeclaration(
39+
[t.importDefaultSpecifier(t.identifier(EmberGlobalImportName))],
40+
t.stringLiteral('ember')
41+
);
42+
}
43+
3444
module.exports = function (babel) {
3545
const t = babel.types;
3646

@@ -84,8 +94,25 @@ module.exports = function (babel) {
8494
return {
8595
name: 'ember-modules-api-polyfill',
8696
visitor: {
97+
Program(path, state) {
98+
let options = state.opts || {};
99+
let useEmberModule = options.useEmberModule || false;
100+
101+
if (!useEmberModule) return;
102+
103+
let hasEmberImport = path
104+
.get('body')
105+
.filter((n) => n.type === 'ImportDeclaration')
106+
.find((n) => n.get('source').get('value').node === 'ember');
107+
108+
if (!hasEmberImport) {
109+
path.unshiftContainer('body', emberImport(t));
110+
}
111+
},
87112
ImportDeclaration(path, state) {
88-
let ignore = (state.opts && state.opts.ignore) || [];
113+
let options = state.opts || {};
114+
let ignore = options.ignore || [];
115+
let useEmberModule = options.useEmberModule || false;
89116
let node = path.node;
90117
let declarations = [];
91118
let removals = [];
@@ -105,10 +132,17 @@ module.exports = function (babel) {
105132

106133
if (specifierPath) {
107134
let local = specifierPath.node.local;
108-
if (local.name !== 'Ember') {
109-
path.scope.rename(local.name, 'Ember');
135+
136+
if (useEmberModule) {
137+
if (local.name === 'Ember') {
138+
path.scope.rename(EmberGlobalImportName);
139+
}
140+
} else {
141+
if (local.name !== 'Ember') {
142+
path.scope.rename(local.name, 'Ember');
143+
}
144+
removals.push(specifierPath);
110145
}
111-
removals.push(specifierPath);
112146
} else {
113147
// import 'ember';
114148
path.remove();
@@ -339,3 +373,5 @@ module.exports = function (babel) {
339373
// Provide the path to the package's base directory for caching with broccoli
340374
// Ref: https://github.com/babel/broccoli-babel-transpiler#caching
341375
module.exports.baseDir = () => path.resolve(__dirname, '..');
376+
377+
module.exports.uniqueishGlobalName = EmberGlobalImportName;

0 commit comments

Comments
 (0)