Sebastián Rojas
Sebastián Rojas

Reputation: 2891

How to include SASS styles in an angular 8 library

I had an angular library for sharing some components, directives and more between two projects.

Recently I tried to include in the library some styles to reuse in the two projects but I can achieve to ng build include it in the bundle.

¿Any clue?

Upvotes: 1

Views: 2083

Answers (2)

s.schleu
s.schleu

Reputation: 1361

Update for Angular 9:

In the library, next to the lib-directory, create a styles-directory and then add it as assets to ng-rollout.yaml like so:

"assets": [
  "styles/**/*.scss"
]

This will copy all SCSS-files found recursively in the styles-directory into a styles directory in the output directory of your library.

Upvotes: 0

Sebastián Rojas
Sebastián Rojas

Reputation: 2891

The only way I found until now is to run an npm script to copy sass files on dist folder (using copyfiles) before package the .tgz for internal use.

Here my package.json:

{
...
     "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e",
        "uiutils_build": "ng build my-library",
        "uiutils_styles": "copyfiles -f projects/my-library/src/scss/*.scss dist/@my-namespace/my-library/scss",
        "uiutils_pack": "cd dist/@my-namespace/my-library && npm pack",
        "uiutils_copy": "copyfiles -f dist/@my-namespace/my-library/*.tgz ../infraestructure.tourbitz.com/packages/@my-namespace/my-library",
        "uiutils_package": "npm run uiutils_build && npm run uiutils_styles && npm run uiutils_pack && npm run uiutils_copy"
      },
...
}

the important script is uiutils_styles.

¿How to use?

In my style files, I included the styles:

@import "~@my-namespace/ui-uitils/scss/my_style.scss";

And for registering global styles I did on the project section of the angular.json file:

{
... 
"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
"styles": [
              "node_modules/@my-namespace/my-library/scss/my_style.scss",
              "src/styles.scss"
            ],
...
},
...
}

Upvotes: 1

Related Questions