If you’re a lover of SCSS, you’ll definitely want to make sure to use it in your Angular applications. Luckily the Angular CLI does all the setup for you!
Let’s first walk through the file changes that the Angular CLI handles for us and how you can modify existing projects to switch over to SCSS for styling. I’ll then go over how I like to set up my SCSS files and folder structure when working on Angular projects.
Let’s get into it!
When creating a new Angular app using the ng new app-name
command, the CLI will ask you “Which stylesheet format would you like to use?” — select SCSS.
CLI Default Stylesheet Prompt
When choosing this option when creating a new project, the main things the CLI does is the following:
reference to the
angular.json build/options/styles` sectionstyles.scss
file and adds it to your src
folderThe CLI will configure the testing files and references as well, but we won’t go into those here.
💁🏼 Tip: Run
_ng — version_
in your terminal to check what @angular/cli version you are running, and update to the latest using_npm i -g @angular/cli@latest_
.
If you ever need to switch to the SCSS stylesheet format in an existing project, having the two items above should be all you need to get going!
Updating CLI Config
The other thing you need to do is update your CLI configuration to create SCSS files instead of CSS files when you create new components. There are two ways to do this — using the CLI command or adding the reference manually. To use the command type the following into your terminal:
ng config schematics.@schematics/angular:component.styleext scss
💁🏼 Tip: Sometimes this command modifies the
angular.json
in the wrong place. Make sure it is modified in the top section of your file under your project (usually line 8 or so if you don’t have many configuration changes).
To make the change manually add the following schematics reference into your angular.json
file:
"schematics":{
"@schematics/angular:component": {
"style": "scss" }
},
"schematics":{"@schematics/angular:component":{"style": "scss"}},
Using the SCSS-Scaffold NPM Package
A while back ago I created an Angular Schematic (you can read more about it here) that adds of all of the SCSS folder structure for you with the easy command npm add scss-scaffold
, so you don’t have to
#angular #scss #css #sass