angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Add project source dir as a module

Open filipesilva opened this issue 10 years ago • 50 comments

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.

filipesilva avatar Jul 27 '16 16:07 filipesilva

@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.

darkbasic avatar Oct 17 '17 13:10 darkbasic

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/*" ]
    }
  }

jongunter avatar Oct 19 '17 21:10 jongunter

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.

darkbasic avatar Oct 20 '17 10:10 darkbasic

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.

tihomir-kit avatar Nov 08 '17 22:11 tihomir-kit

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.

Xapuu avatar Dec 07 '17 16:12 Xapuu

Works for me this way (in tsconfig.json only):

"paths": {
      "@domain/*": [
        "./app/domain/*"
      ]
    }

And import it:

import {User} from '@domain/User';

sergey-morenets avatar Jan 10 '18 15:01 sergey-morenets

@Xapuu worked for me too. thanks

otaviodecampos avatar Feb 07 '18 19:02 otaviodecampos

@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.

nweldev avatar Apr 19 '18 15:04 nweldev

For a library type project, how to have these paths mappings seen by a client application ?

stephaneeybert avatar Jul 15 '19 12:07 stephaneeybert

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.