You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 #865 and also greatly reduce the need for #900.
There are also tsconfig's baseUrl and paths options. This would only apply to typescript files but allows editors (e.g., vscode) to process and follow the mapping.
To make the current version of vscode pick up the settings, you'll need to add a workspace settings file with "typescript.tsdk": "./node_modules/typescript/lib" (make sure it points to your actual typescript location).
"baseUrl": ".",
"paths": {
"@app/*": ["app/*"]
}
UPDATE:
The above additions to tsconfig and the inclusion of the TsConfigPathsPlugin in the CLI's web pack config work well so far. It definitely simplifies and improves readability of typescript imports.
Reacted by Andrew Koltsov, Baruch Velez, Hendrik Schuster, Max Shepel, Chris Rodriguez, Jan Peer Stöcklmair, ronen1malka, Frank Goortani, Makla, Athif Shaffy and 1 more
Another thing to be aware of with aliases is tests. Just because webpack knows @app as an Ali's doesn't mean mocha or karma etc. will know how to resolve it
I have basically @rolandoldengarm's issue, but there's a case that might be worth testing for someone: npm link.
When I attempted anything using symbolic links it seemed that webpack would resolve the real path instead of the alias. Combined with 'tsconfig:paths' not working yet, I cannot fathom solution to 'develop using modules' that's supportable long term.
Mostly want to call this out because at the moment this is listed as 'nice to have', and I really can't imagine being able to use the tool in this state long term.
I tried npm link, obviously. But also a few direct symbolic links - the craziest failure was when I symbolically linked a module that depends on another. Consider B depends on A: With both A and B npm install'd, everything works fine. However, removing the install of B and instead creating a symbolic link so that it looks like the source is under the 'app' directory, (and modifying the app.module.ts accordingly) fails, because it can't find module A.
On the chance this might be of interest to someone here I added some scripts to my repos to help demo it. If not interested, no worries.
@rolandoldengarm I am now deeply curious: what do you mean when you referenced system-config? Are you using system-config in combination with the webpack version of the CLI? If so, I'd love to hear more...
As for symlinks... this is unquestionably a design failure of Node. I have a few horrible, evil ideas about how to make the ' --preserve-symlinks' flag propagate through Node's call chain, but nothing I'd be proud of. And certainly nothing that would 'just work' when I clone my repository onto another machine, for example. It might be possible that there is something relatively simple the CLI team could do to enable symlink'd directories to work properly with node/webpack... but a) I doubt it and b) why bother? Leveraging tsconfig:paths just makes so much more sense.
If I use something like import { AppComponent } from 'app/app.component' in app.module.ts then tooling doesn't work and editor complains about TS2307: Cannot find module 'app/app.component'.
then the editor reports an error inside app.component.ts:
Angular: Component 'AppComponent' is not included in a module and will not be available inside a template. Consider adding it to an NgModule declaration.
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:
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.
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.
Reacted by Otávio de Campos, Eyal Lapid and Sergio H.
Activity
sirajc commentedon Jul 28, 2016
Have seen similar thing in ng-bootstrap. The demo project inside refers to the main project using @ng-bootstrap and it is using webpack
TheLarkInn commentedon Jul 28, 2016
In terms of webpack implementation would we be taking this @app property and mapping them to the webpack alias for module resolution @filipesilva
filipesilva commentedon Jul 28, 2016
Something important that came up: editor tooling would never be able to find
@app. You'd always have errors on your editor.clydin commentedon Jul 28, 2016
There are also tsconfig's baseUrl and paths options. This would only apply to typescript files but allows editors (e.g., vscode) to process and follow the mapping.
To make the current version of vscode pick up the settings, you'll need to add a workspace settings file with
"typescript.tsdk": "./node_modules/typescript/lib"(make sure it points to your actual typescript location).UPDATE:
The above additions to tsconfig and the inclusion of the TsConfigPathsPlugin in the CLI's web pack config work well so far. It definitely simplifies and improves readability of typescript imports.
masaanli commentedon Aug 6, 2016
Can't wait on this one :) Also struggling with FontAwesome Fonts implementation. Did a CDN for now...
chapati23 commentedon Aug 18, 2016
Another thing to be aware of with aliases is tests. Just because webpack knows @app as an Ali's doesn't mean mocha or karma etc. will know how to resolve it
ggranum commentedon Sep 5, 2016
I have basically @rolandoldengarm's issue, but there's a case that might be worth testing for someone: npm link.
When I attempted anything using symbolic links it seemed that webpack would resolve the real path instead of the alias. Combined with 'tsconfig:paths' not working yet, I cannot fathom solution to 'develop using modules' that's supportable long term.
Mostly want to call this out because at the moment this is listed as 'nice to have', and I really can't imagine being able to use the tool in this state long term.
I tried npm link, obviously. But also a few direct symbolic links - the craziest failure was when I symbolically linked a module that depends on another. Consider B depends on A: With both A and B
npm install'd, everything works fine. However, removing the install of B and instead creating a symbolic link so that it looks like the source is under the 'app' directory, (and modifying the app.module.ts accordingly) fails, because it can't find module A.On the chance this might be of interest to someone here I added some scripts to my repos to help demo it. If not interested, no worries.
The repositories I'm working on are https://github.com/ggranum/revector and https://github.com/ggranum/revector-demo
Specifically, if you clone both of those into the same parent and run
Everything should work. Now stop the server and run:
and you should reproduce the errors I'm seeing [though note that the sed command in the bash scripts may break on non-macs] :
There's a bash script to create npm links as well (but run the uninstall-revector.sh script first).
If nothing else, hopefully this post serves to point out how hard it will be to write modular code in external projects without this feature.
Thanks much
ggranum commentedon Sep 6, 2016
@rolandoldengarm I am now deeply curious: what do you mean when you referenced system-config? Are you using system-config in combination with the webpack version of the CLI? If so, I'd love to hear more...
As for symlinks... this is unquestionably a design failure of Node. I have a few horrible, evil ideas about how to make the ' --preserve-symlinks' flag propagate through Node's call chain, but nothing I'd be proud of. And certainly nothing that would 'just work' when I clone my repository onto another machine, for example. It might be possible that there is something relatively simple the CLI team could do to enable symlink'd directories to work properly with node/webpack... but a) I doubt it and b) why bother? Leveraging tsconfig:paths just makes so much more sense.
31 remaining items
Zeioth commentedon Aug 21, 2017
I don't get how something so simple can be so hard to achieve.
Fortunately this guide worked for me.
In tsconfig.json
in webpack.config.js
Now let's imagine you have the next project structue.
If you wanna import the file example 2 from example1, from now on you can use
atte-backman commentedon Oct 9, 2017
... and it should be all good.
ppozniak commentedon Oct 13, 2017
Here's how I handled it:
in
tsconfig.jsonExample:
import { ShoppingItem } from "@models/shopping-item.model";darkbasic commentedon Oct 17, 2017
They removed the above mentioned solution with
awesome-typescript-loaderand they're now usingAotPlugin: f9a7c01#diff-3c07f218821da114292666210f358440If I use something like
import { AppComponent } from 'app/app.component'inapp.module.tsthen tooling doesn't work and editor complains aboutTS2307: Cannot find module 'app/app.component'.If I put
into
tsconfig.jsonthen the editor reports an error inside
app.component.ts:darkbasic commentedon Oct 17, 2017
@abner @filipesilva does the new
AotPluginsolution work for you?Is it possible to reopen this issue?
I also tried to eject the config and use Webpack's
aliasbut it didn't work either.jongunter commentedon Oct 19, 2017
Seems to work for me if all 3 (
app,specand the one in the root of the project for my IDE) of mytsconfigfiles have the following lines so they use the@appalias:darkbasic commentedon Oct 20, 2017
Inside
app.component.tsthe editor still reports:Try to use something like
import { AppComponent } from 'app/app.component'inapp.module.ts.tihomir-kit commentedon Nov 8, 2017
For me this worked fine (
tsconfig.json, nottsconfig.app.json):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.
Xapuu commentedon Dec 7, 2017
Here is my solution
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.
sergey-morenets commentedon Jan 10, 2018
Works for me this way (in tsconfig.json only):
And import it:
import {User} from '@domain/User';otaviodecampos commentedon Feb 7, 2018
@Xapuu worked for me too. thanks
nweldev commentedon Apr 19, 2018
@clydin / @filipesilva How do you think we should handle this in a CLI v6 project with libraries ? Especially when following angular/devkit#730 (comment).
stephaneeybert commentedon Jul 15, 2019
For a library type project, how to have these paths mappings seen by a client application ?
angular-automatic-lock-bot commentedon Sep 9, 2019
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.