Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember CLI ES6 Modules: import path to app root

I find the import path in ES6 modules very confusing when using it in Ember CLI. For example, if I want to import a model deep in my application, I end up doing something like this:

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

This is an exercise of trial and error, as it's hard to easily visualize how deep in the folder tree I'm using this from. Even worse, if I refactor my files, everything breaks.

So alternatively, I can use an absolute path like this:

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

I prefer not to hard-code the app name into the path, because it can change.

Is there a shorthand to specify the app root?

./ doesn't work because ./ implies current path.

import User from './models/user';
like image 269
Johnny Oshika Avatar asked Mar 17 '15 13:03

Johnny Oshika


1 Answers

Unfortunately there is no way to programmatically name ES6 imports at least in Ember so you can't use ENV.modulePrefix.

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

sed -i 's/previousName/newName/g' **/*
like image 121
Pooyan Khosravi Avatar answered Nov 10 '22 14:11

Pooyan Khosravi