Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stylePreprocessorOptions for libraries, where should I put in in angular.json?

Tags:

angular

I'm trying to use stylePreprocessorOptions in order to include a path to my variables folders for a library project like so :

"stylePreprocessorOptions": {
  "includePaths": [
    "styles/variables"
  ]
}

Then I use @import 'colors'; within the scss file. But it doesnt work while doing ng serve :

@import 'colors';
       ^
      Can't find stylesheet to import.

Here is the full library in angular.json :

        "ui-table": {
            "root": "libs/ui/table",
            "sourceRoot": "libs/ui/table/src",
            "projectType": "library",
            "prefix": "xxx",
            "architect": {
                "build": {
                    "builder": "@angular-devkit/build-ng-packagr:build",
                    "options": {
                        "tsConfig": "libs/ui/table/tsconfig.lib.json",
                        "project": "libs/ui/table/ng-package.json",
                        "stylePreprocessorOptions": {
                            "includePaths": [
                                "styles/variables"
                            ]
                        }
                    }
                },
                "lint": {
                    "builder": "@angular-devkit/build-angular:tslint",
                    "options": {
                        "tsConfig": ["libs/ui/table/tsconfig.lib.json"],
                        "exclude": ["**/node_modules/**"]
                    }
                }
            },
            "schematics": {
                "@nrwl/schematics:component": {
                    "styleext": "scss"
                }
            }
        },

How can I use stylePreprocessorOptions in a library in Angular?

Thanks!

like image 707
GreatHawkeye Avatar asked Nov 26 '19 14:11

GreatHawkeye


People also ask

How do I import styles in angular json?

You can add more global styles via the styles option inside your project's build target options in angular. json . These will be loaded exactly as if you had added them in a <link> tag inside index.

What does angular json contain?

A file named angular. json at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults for build and development tools provided by the Angular CLI. Path values given in the configuration are relative to the root workspace folder.


2 Answers

Not in angular.json.

Include the path to your scss folder with styleIncludePaths in the librairies' ng-package.json

"lib": {
    "entryFile": "src/public-api.ts",
    ...
    "styleIncludePaths": [
        "relative/path/to/scss/folder"
    ]
    ...
}

finally import the files from the referenced scss folder without prefix path as follows

@import "filename";
like image 97
Zainu Avatar answered Oct 16 '22 17:10

Zainu


The accepted answer did not work for me using Angular v9.

But this did:

{
  "ngPackage": {
    "lib": {
      "styleIncludePaths": ["./src/assets/styles"]
    }
  }
}

More details here: https://github.com/ng-packagr/ng-packagr/blob/master/docs/style-include-paths.md

like image 42
Kildareflare Avatar answered Oct 16 '22 17:10

Kildareflare