How to change a CSS preprocessor in an Angular project

featured_image

Angular can maintain any of the major CSS preprocessors. By default the new project is created with the css support, but even after its creation, you can easily switch to sass or less. You can also adjust the preprocessor globally, for all new projects.

What we are going to build

Sass allows us to use special features in stylesheets: variables, mixins and nested rules. In the following examples we are going to change the project’s styles to .scss. However, you can choose .sass extension if you like.

Requirements

Check out your version of Angular, the following guide will work for projects generated with Angular CLI v6 or higher. I’m working on:

Change the CSS preprocessor in an existing project

The information about processing styles is kept in the angular.json file (previously angular-cli.json). To update it correctly, run in the main folder of your angular project (where the file resides):

Use the command rather than editing angular.json manually. You can see that the changes has been added automatically:

You have to update the extension in all already existing css files: src/styles.scss, src/app/app.component.scss, etc.

Make sure that all components are looking for styles in the updated files:

Verify the paths used for building the project:

Create a new project with the sass preprocessor

If you are creating a fresh project and already know the preprocessor to be used, you can specify it directly during a project creation:

The angular.json has been created with all required configuration for handling the sass:

The src/styles.scss file has been already generated and the app.component contains the right path for its styleUrls.  The configuration is finished.

Change CSS preprocessor globally for all new projects

Check out the global configuration

You can establish and read the global settings with the ng config –global command. To find out which settings are being applied to a new project by default, run the command and analyze the output:

Set sass as the default preprocessor for all new projects

We want to adjust the "schematics" : {}  configuration to build every new project with the sass support. There are several official schematics maintained by the Angular team. As stated in this discussion, when creating a new project, the schematics for ng-new is being applied. So run in the command line:

Check out the updated global config:

Create a new project with:

You can see that the project is utilizing the sass:

The src/styles.scss file has been already generated and the app.component contains the right path for its styleUrls.  The configuration is finished.

Troubleshooting

The global configuration is ignored when creating a new project

In the official Angular CLI documentation for major CSS preprocessors you can find a command for setting a CSS preprocessor on an existing project:

It attaches to the end of the angular.json file:

I found some recommendation to use the same command – but with -g option for setting a CSS preprocessor universally:

It won’t work as the component schematics is not utilized during the creation of a new project – you have to set the ng-new schematics.

Secondly, for the ng-new schematics, you have to set the style – not the styleext property.

defaults.styleExt – Value cannot be found

Your project implements an Angular version in which the defaults.styleExt property does not exist. You have to set either:

  • the component.styleext property – for changing an existing project;
  • the ng-new.style property – for setting the global config for all new projects.

Invalid JSON character: “s” at 0:0

It was a bug and has been already resolved, try updating your Angular version.

In case of other errors

  1. If you edited the angular.json file in an existing project manually, revert those changes and run the command:
  2. Make sure that:
    • all already existing files have updated extension – from .css to .scss (e.g. src/styles.scss, src/app/app.component.scss);
    • all components are pointing to the updated filenames:
    • verify the paths used for building the project:

Photo by Anthony Tran on StockSnap

Leave a Reply

Your email address will not be published.