Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 701
droidbot Avatar asked Jan 28 '15 13:01

droidbot


1 Answers

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.

like image 92
GJK Avatar answered Nov 15 '22 07:11

GJK