Search code examples
ember.jsember-cli

How does import statements work in ember-cli?


Am quite new to emberjs and ember-cli.

And I have always been wondering how a statement like this works:

import Ember from 'ember'

Does 'ember build' look up for 'ember' in node_modules?

I understand statements like this with relative paths:

import ENV from './config/environment'

but not the ones referred without a path.

This question raises in connection with Could not find module ember-validations, in an effort to find its root cause.


Solution

  • The sort answer is that Ember-CLI registers the global objects directly with the module system. Take a look at the code here. While it's wrapped in a little helper code, they essentially do this:

    define('ember', [], function() {
        return {
            'default': window.Ember,
        };
    });
    

    Then, Ember-CLI converts your import statement during compilation:

    import Ember from 'ember';
    

    Gets converted to:

    var Ember = require('ember')['default'];
    

    Keep in mind that this is how it's done when using a transpiler to use AMD modules. I'm not 100% sure how that code would work if we were using a native ES6 implementation, although I know that the syntax supports this kind of thing.