Ember CLI ES6 Modules: Import Path to Application Root

I find that the import path in ES6 modules is very confusing when used in the Ember CLI. For example, if I want to import a model into my application, I do something like this:

import User from '../../../../../models/user'; 

This is a trial and error exercise, as it’s hard to easily understand how deep in the folder tree I use it. Even worse, if I reorganize my files, everything breaks.

Thus, I can use the absolute path as follows:

 import User from 'app-name/models/user'; 

I prefer not to recode the application name in the path, because it can change.

Is there an abbreviation for the root of the application?

./ does not work because ./ implies the current path.

 import User from './models/user'; 
+6
source share
1 answer

Unfortunately, there is no way to programmatically name an ES6 import, at least in Ember, so you cannot use ENV.modulePrefix .

However, there is a workaround. Whenever you want to change the module prefix, run this GNU sed command from ZSH inside the Ember root.

 sed -i 's/previousName/newName/g' **/* 
+1
source

Source: https://habr.com/ru/post/983914/


All Articles