POV
POV

Reputation: 12025

How to short path to file in Angular?

In some CSS files I have import:

@import "../../../theme.scss"
@import "../../theme.scss"
@import "../../../../../../theme.scss"
@import "../theme.scss"

How to use relative path as absolute path everywhere for all cases:

@import "theme.scss"

My full angulr.json code is:

"reon-core": {
      "projectType": "library",
      "root": "projects/reon-core",
      "sourceRoot": "projects/reon-core/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-ng-packagr:build",
          "options": {
            "stylePreprocessorOptions": {
              "includePaths": ["./projects/reon-core/src/lib/tssource/"]
            },
            "tsConfig": "projects/reon-core/tsconfig.lib.json",
            "project": "projects/reon-core/ng-package.json"
          }
        }

....It returns Data path "" should NOT have additional properties(stylePreprocessorOptions).;

Upvotes: 3

Views: 7714

Answers (5)

Murat Yıldız
Murat Yıldız

Reputation: 12050

If the shortened path does not work, try to build the project or restart the code editor e.g. VS Code.

tsconfig.json:

{
    "CompilerOptions": {
        "baseUrl": "src",
        "paths": {
            "@core/*": ["app/core/*"]
            "@services/*": ["app/services/*"]
        }
    }
    # ...
}

posts.component.ts:

import { SeoService } from '@core/seo.service.ts';
import { PostsService } from '@services/posts.service.ts';

Upvotes: 3

Ling Vu
Ling Vu

Reputation: 5181

Just add the path to the stylePreprocessorOptions in the angular.json file:

"architect": {
        "build": {
          ...
          "options": {
            ...
            "stylePreprocessorOptions": {
              "includePaths": [
                "./src/assets/style/theme"
              ]
            },
         },
         "test": {
           ...
           "options": {
             ...
             "stylePreprocessorOptions": {
               "includePaths": [
                 "./src/assets/style/theme"
               ]
             }
           }
         }
}

Then you should be able to include your file like @import 'app-theme.scss';

Update after description changed

I suppose you set the wrong relative path. Your root folder should be reon-core. Therefore you need to set the stylePreprocessorOptions like that:

"includePaths": ["./src/lib/tssource/"]

in build and test

After you set this, you can put in any theme file there and like:

./src/lib/tssource/theme.scss and import it like @import 'theme.scss';

Upvotes: 5

Anton Skybun
Anton Skybun

Reputation: 1649

        "stylePreprocessorOptions": {
          "includePaths": [
            "src/assets/styles",
            "src/assets/styles/theme.scss"
          ]

Edit for your path to style file from src/PATH/theme.scss than after rebuild your project restart I mean you can use it in your style like this @import "theme";

Upvotes: 1

Sameer
Sameer

Reputation: 5188

Assuming your theme.scss is located in src/styles/theme.scss or src/assets/styles/theme.scss.

Add this path to angular.json shown below

"stylePreprocessorOptions": {
   "includePaths": [
     "src/styles",
     "src/assests/styles"
  ]
}

Now you can import it directly using path @import 'theme.scss'

Upvotes: 1

Anton Skybun
Anton Skybun

Reputation: 1649

You can make this by adding to your tsconfig.json {compilerOptions: {path: HERE!!} }

"paths": {
  "core-js/es7/reflect": ["node_modules/core-js/proposals/reflect-metadata"],
  "core-js/es6/*": ["node_modules/core-js/es/*"],
  "@swagger/*": ["src/app/_swagger/*"],
  "@swagger": ["src/app/_swagger/index.ts"],
  "@directives/*": ["src/app/_directives/*"],
  "@services/*": ["src/app/_services/*"],
  "@models/*": ["src/app/_models/*"],
  "@tables/*": ["src/app/tables/*"],
  "@tables_services/*": ["src/app/tables/services/*"],
  "@shared/*": ["src/app/shared/*"],
  "@language/*": ["src/app/_language/*"],
  "moment": [
    "node_modules/moment/min/moment.min.js"
  ]
}

Upvotes: 4

Related Questions