3

How to turn on sass-loader in Angular?

I try to use @import "theme.scss"; inside*.scss` files.

But I get message;

File to import not found or unreadable: theme.scss.

I dont use webpack, only tsconfig and angular.jon

Where file theme.scss contains variables:

$border-radius: 10px;
$main: rgb(20, 100, 192);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • are you sure the `theme.scss` file lives in the same directory as the `scss` file you're importing it in? – ViqMontana Jan 13 '20 at 11:43
  • No this file is in root. but I need to get this file from everywhere in project –  Jan 13 '20 at 11:43
  • Propably I can set path rull in tsconfig or how? –  Jan 13 '20 at 11:45
  • 1
    try to modify the angular.json projects > projectName > architect > build > options > ````"stylePreprocessorOptions": { "includePaths": [ "the/path" ] },``` – Nicollas Braga Jan 13 '20 at 11:48

1 Answers1

2

Import .scss file into other .scss files

You can specify the .scss inside other .scss files using the relative path of your file inside the assets path.

Your default assets path should be

"assets": [
              "src/favicon.ico",
              "src/assets"
            ]

which is defined in the angular.json file.

Example:

  • You have an app.component.scss
  • Your theme.scss file is in ./src/assets/theme/theme.scss

Then you would import it inside app.component.scss like:

@import '../assets/theme/theme.scss';

Specify the style preprocessor option to shorten the import

If you want to shorten the path like you described, you 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 'theme.scss';

Update after description changed

I suppose you set the wrong relative path. Therefore you need to set the stylePreprocessorOptions like that:

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

in build and test

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

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

Ling Vu
  • 4,740
  • 5
  • 24
  • 45