Add project source dir as a module
It would be very useful to be able to address components via a relative path starting at the project root. A way to do this is to consider the project root as a special project, as if it was inside of node_modules as far as resolution is concerned.
Such a design greatly reinforces the 'app as a library' idea, and plays nice with most module loading scheme.
A proposed name is @app but configurable via angular-cli.json, where app is the project prefix specified in ng new --prefix app (this exists already and defaults to app).
Example:
// instead of
import { something } from '../../../../../../../shared/something';
// we could do
import { something } from '@app/shared/something';
This would also work for CSS preprocessors.
Such implementation would address https://github.com/angular/angular-cli/issues/865 and also greatly reduce the need for https://github.com/angular/angular-cli/issues/900.
@abner @filipesilva does the new AotPlugin solution work for you?
Is it possible to reopen this issue?
I also tried to eject the config and use Webpack's alias but it didn't work either.
Seems to work for me if all 3 (app, spec and the one in the root of the project for my IDE) of my tsconfig files have the following lines so they use the @app alias:
"compilerOptions": {
"paths": {
"@app/*": [ "app/*" ]
}
}
Inside app.component.ts the editor still reports:
Angular: Component 'AppComponent' is not included in a module and will not be available inside a template. Consider adding it to an NgModule declaration.
Try to use something like import { AppComponent } from 'app/app.component' in app.module.ts.
For me this worked fine (tsconfig.json, not tsconfig.app.json):
"baseUrl": "./",
"paths": {
"~/*": [ "*" ],
"@app/*": [ "src/app/*" ],
"@core/*": [ "src/app/core/*" ],
"@shared/*": [ "src/app/shared/*" ],
"@feature/*": [ "src/app/feature/*" ]
}
the critical part that I was missing at the beginning was * at the end of both keys and relative paths.
VIsual Studio recognized the aliases after I restarted it.
Here is my solution
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@models": [
"./app/models/index.ts"
],
"@environment": [
"./environments/environment.ts"
],
"@services/*": [
"./app/services/*"
]
}
}
}
Be aware that you must restart the CLI each time you make any changes in the tsconfig.json, so that they can take effect, also be aware that although the routes will work, you probably wont have any autocomplete for them.
Works for me this way (in tsconfig.json only):
"paths": {
"@domain/*": [
"./app/domain/*"
]
}
And import it:
import {User} from '@domain/User';
@Xapuu worked for me too. thanks
@clydin / @filipesilva How do you think we should handle this in a CLI v6 project with libraries ? Especially when following https://github.com/angular/devkit/issues/730#issuecomment-382379686.
For a library type project, how to have these paths mappings seen by a client application ?
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.