1

Is there a way to see the CSS that corresponds to the SCSS when we are using SCSS as the preprocessor for Angular?

There is an answer here: When using angular with scss, how can I see the translated css?

And it mentions using the --extract-css option, however when I try that it looks like it has been deprecated:

sandbox/project $ ng build --extract-css
Unknown option: '--extract-css'

Thoughts?

4
  • 1
    Do you mind if I ask why do you need to see the translated css? Commented May 31, 2022 at 1:49
  • I'm going through the Angular Material source code and trying to understand it and it would be a lot easier if I could import snippets and generate the corresponding CSS ... Commented May 31, 2022 at 1:54
  • maybe converter will help you here jsonformatter.org/scss-to-css Commented May 31, 2022 at 3:54
  • It has to be in an Angular CLI environment, because it allows me to see various imports from Angular Material ... Commented May 31, 2022 at 4:30

2 Answers 2

1

Styles in Angular Build Files

In your build files the styles of components will actually be compiled in the main.js file. You can find it in the network tab of your browsers developertools.

You will also see a file called styles.css, but this will only contain your global styles. This is because of Angulars view-encapsulation of styles per component. The behavior of angular may change if you change the view-encapsulation strategy as explained here to:

  • Emulated (default)
  • ShadowDOM
  • None

I would not recommend doing that though.

However, if you want you can compile your sass files into css using the command line tool you can install as explained on the official sass website.

You can also just use online sass converters like thisone.

If you are just interested in the global styles here's a reference to How you can switch the format from scss to css in your browser.

Example

app.component.scss

p {
  background-color: orange;
}

styles.scss

@import 'default';

p {
   color: red;

   &:hover {
      color: blue;
   }
}

default.scss

h1 {
  color: teal;
}

Result in Build

styles.css:

h1 {
  color: teal;
}

p {
  color: red;
}

p:hover {
  color: blue;
}

main.js:

AppComponent.ɵfac = function AppComponent_Factory(t) { return new (t || AppComponent)(); };
AppComponent.ɵcmp = /*@__PURE__*/ _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵdefineComponent"]({ type: AppComponent, selectors: [["app-root"]], decls: 1, vars: 0, template: function AppComponent_Template(rf, ctx) { if (rf & 1) {
        _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelement"](0, "lib-my-lib");
    } }, directives: [my_lib__WEBPACK_IMPORTED_MODULE_1__.MyLibComponent], styles: ["p[_ngcontent-%COMP%] {\n  background-color: orange;\n}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImFwcC5jb21wb25lbnQuc2NzcyIsIi4uXFwuLlxcLi5cXC4uXFxBbmd1bGFyJTIwUHJvamVjdHNcXGxpYi1leGFtcGxlXFxzcmNcXGFwcFxcYXBwLmNvbXBvbmVudC5zY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0VBQ0Usd0JBQUE7QUNDRiIsImZpbGUiOiJhcHAuY29tcG9uZW50LnNjc3MiLCJzb3VyY2VzQ29udGVudCI6WyJwIHtcclxuICBiYWNrZ3JvdW5kLWNvbG9yOiBvcmFuZ2U7XHJcbn1cclxuIiwicCB7XG4gIGJhY2tncm91bmQtY29sb3I6IG9yYW5nZTtcbn0iXX0= */"] });

*Note the orange background-color in the last line.

Sign up to request clarification or add additional context in comments.

19 Comments

HI. I tried the suggestion, but unfortunately I don't see any CSS in the main.js file. Also tried the SASS tooling, but it gets confused when using certain angular imports. I think Angular has develop additional import capabilities ...
The css will not look like regular css but js. Try looking for some keywords of your css in the main.js file. You will not be able to make too much sense out of it though. The imports are native to sass and the command line tool should recognize and even fetch them. (It might not be able to for some reason. I don't know what you're trying to import and where from.) Try commenting the imports that don't work, and create a dummy second sass and import that, then use the tool and you'll see. I'm not sure about online sources or anything, but it works fine for me on my own files.
As an experiment I tried putting some SASS in styles.scss and running the app. The styles show up in the Chrome Developer tools ... however main.js is still empty ...
Might be nice to reference the other question here as I found that elaboration super helpful. stackoverflow.com/questions/72446771/…
I added it to the answer as well so it isn't buried in these comments.
|
-1

This is just a complement to the accepted answer. I wrote it up in a medium article, as it was not immediately obvious as styles.scss is opened first when selecting elements in Chrome Developer Tooling, but styles.css is in the tab right next to it.

https://fireflysemantics.medium.com/viewing-generated-global-css-for-angular-sass-projects-857a6887ff0b

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.