Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli how to add sass and or bootstrap?

I'm trying out angular-cli and trying to get sass and bootstrap in the project. I've read in the wiki that you can simply do:

ng install sass

Doing this with the current latest (beta.5) gives me an error:

Install failed. Could not find addon with name: sass

How can I go about installing bootstrap and or sass in a way that angular-cli will deal with the index page and systemJS?

like image 556
Thibs Avatar asked May 25 '16 14:05

Thibs


People also ask

How does angular integrate with Sass?

In the Angular CLI, all components are self-contained and so are their Sass files. In order to use a variable from within a component's Sass file, you'll need to import the _variables. scss file. One way to do this is to @import with a relative path from the component.

Can I use CSS and SCSS in angular?

When you are starting to create your angular project via using Angular CLI then angular provides you with options for the CSS and SCSS. With the help of Angular CLI, you can install CSS or SCSS on your project and start working on that in a suitable way.


2 Answers

If you create your project with the angular-cli it comes with a scss preprocessor. If you have styles files you can just change the ext to scss and ng serve will compile them for you.

When you create a project using the cli you can tell it that you want to use scss by

ng new sassy-project --style=sass

If you have an existing project Angular 2 to 5

ng set defaults.styleExt scss

If your project is Angular 6 and 7

ng config schematics.@schematics/angular:component.styleext sass

These tell the cli that when you generate new components with scss styles not css. It doesn't add a compiler or enable the compiler because there is already one there.

Any existing components would need their style file extensions renamed.

Additional documentation https://angular.io/cli

Hope this helps

like image 131
Woot Avatar answered Oct 19 '22 06:10

Woot


NOTE: This answer is out of date due to changes in the Angular CLI, please refer to the answer below from Woot which is more current.

You need to install node-sass via npm i node-sass --save-dev then change your styleExt in angular-cli.json to either sass or scss (or you can set that via ng set defaults.styleExt scss

As for bootstrap, place it under the public directory (I use a sub dir named style) and reference it within index.html

UPDATE: Also if you'd like to have variables files you can add the directory to angular-cli-build.js like this...

return new Angular2App(defaults, {
  vendorNpmFiles: [
    ...
  ],
  sassCompiler: {
    includePaths: [
      'src/app/style' <-- directory for SASS reference files
    ]
  }
});
like image 39
Brocco Avatar answered Oct 19 '22 06:10

Brocco