@@ -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+
3444module . 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
341375module . exports . baseDir = ( ) => path . resolve ( __dirname , '..' ) ;
376+
377+ module . exports . uniqueishGlobalName = EmberGlobalImportName ;
0 commit comments