6

I created a scss app and associated a library. I want to create components with .scss files inside this library. But when I create a component inside the library, angular assigns it a css file automatically.

I created the application (named vega-libraries) with the scss option but not the library (polaris-common)

Here is a screenshot of the project with the tree view,I tried to change the css file of totoComponent in toto.scss as you can see in the screenshot.

enter image description here

But when I do that, it generates an error of this type:

Error: The loader "C:/Users/........toto/toto.component.css" didn't return a string.

How can I create components in my library with .scss files that work ?

Thank you for your help

EDIT :

 "polaris-common": {
      "projectType": "library",
      "root": "projects/polaris-common",
      "sourceRoot": "projects/polaris-common/src",
      "prefix": "lib",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:ng-packagr",
          "options": {
            "project": "projects/polaris-common/ng-package.json"
          },
          "configurations": {
            "production": {
              "tsConfig": "projects/polaris-common/tsconfig.lib.prod.json"
            },
            "development": {
              "tsConfig": "projects/polaris-common/tsconfig.lib.json"
            }
          },
          "defaultConfiguration": "production"
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "projects/polaris-common/src/test.ts",
            "tsConfig": "projects/polaris-common/tsconfig.spec.json",
            "karmaConfig": "projects/polaris-common/karma.conf.js"
          }
        }
      }
    }
  },
  "defaultProject": "vega-libraries"
}
2
  • what does the snippet of code in your angular.json look like for the lib? Commented Aug 3, 2021 at 17:23
  • Thanks for your nswer, I've edited my post with the snippet of angulr.json Commented Aug 3, 2021 at 17:28

2 Answers 2

15

This is for Angular 12. In prior versions it might be different. To set up scss as the default style format when doing ng create component, in angular.json under your project, add schematics:

"projectType": "library",
"schematics": {
    "@schematics/angular:component": {
        "style": "scss"
    },

In an existing component, change styleUrls to reflect the path to your renamed scss file instead of css

// toto.component.ts

@Component({
  styleUrls: ['./toto.component.scss']
})
Sign up to request clarification or add additional context in comments.

1 Comment

can confirm, still works in angular 15 (feb 2023)
8
ng g c "filename" --style=scss

This is the best way I found actually.

1 Comment

I like this answer the most. I've referenced it more than once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.