25

I'm trying to setup Sass in my Angular 2 project. Basically as I understand there are two ways to create an angular 2 project

1) Using angular-cli (https://github.com/angular/angular-cli)

I referred answer mentioned in https://stackoverflow.com/a/41541042/2868352 & I could successfully use scss files in angular 2 project, everything worked great but I couldn't locate the generated css file from scss file in project folder. Could anyone explain the reason why no css file was generated but still it worked?

2) Using quickstart seed (https://angular.io/guide/quickstart)

I couldn't get any information about how to set up sass in quickstart project. Does anyone have any idea about using sass in the quickstart project provided by angular?

Thanks in advance!

Evan Wieland
  • 1,445
  • 1
  • 20
  • 36
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • 1
    `angular-cli` uses webpack which does not create css files until it comes to production build where it might generate some depending on the configuration. For a `quickstart` just add node-sass call in the npm `start` command and it will work automatically – smnbbrv Feb 24 '17 at 08:47
  • @smnbbrv Thanks for the explanation.Using sass in angular-cli is clear now. But for quickstart, adding node-sass call in the npm start command doesn't seem to be working. scss styles are not applied. Do I need to do anything extra? – Abhishek V Feb 24 '17 at 11:12

3 Answers3

42

[Check edited part at end of this answer in case you are using angular cli]

Explaining how to use sass in 'quickstart seed'(https://angular.io/guide/quickstart) (https://angular.io/guide/setup#download)

Please follow these simple steps:

Step 1: Setup the quickstart seed

Use the below commands to setup

npm install
npm start

you will see 'Hello Angular' on browser.

Step 2: Install node-sass and sass-loader

Use the commands mentioned below to install

npm i node-sass -S
npm i sass-loader -S

Now you can see both of these added in your 'dependencies' inside 'package.json' file.

Step 3: Create 2 folders for Sass code and Css code

Create two folders with any name in "quickstart-master" folder. In this case for example: "sass_folder" and "css_folder". Now create a demo file 'demo.sass' and put it inside 'sass_folder'. You can put a simple sass code in this .sass file. It will look like this:

  $font-stack:    Helvetica, sans-serif
  $primary-color: #000

  body
    font: 100% $font-stack
    color: $primary-color

Step 4: Make changes in 'package.json' file

Add scripts to Build and Watch Sass code present in "sass_folder". After compilation, The resulting css code should be stored in "css_folder". After changes the "Scripts" in 'package.json' file should look like this:

"scripts": {
     "build": "tsc -p src/",
     "build:watch": "tsc -p src/ -w",
     "build:e2e": "tsc -p e2e/",
     "serve": "lite-server -c=bs-config.json",
     "serve:e2e": "lite-server -c=bs-config.e2e.json",
     "prestart": "npm run build",
     "start": "concurrently \"npm run build:watch\" \"npm run serve\" \"npm run watch:sass\"",
     "pree2e": "npm run build:e2e",
     "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
     "preprotractor": "webdriver-manager update",
     "protractor": "protractor protractor.config.js",
     "pretest": "npm run build",
     "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
     "pretest:once": "npm run build",
     "test:once": "karma start karma.conf.js --single-run",
     "lint": "tslint ./src/**/*.ts -t verbose",
     "build:sass": "node-sass sass_folder/ -o css_folder",
     "watch:sass": "npm run build:sass && node-sass sass_folder/ -wo css_folder/"
  }

Have a look at 'start', 'build:sass' and 'watch:sass' only.

Step 5: Run the application

Now you can run the app by using below command:

npm start

You will see the compiled css code in "css_folder" with the same file name 'demo.css'. It will look like this (In this case):

body {
  font: 100% Helvetica, sans-serif;
  color: #000; }

Now if you make any change in .sass file it will be reflected to .css file dynamically as the script is watching the code.

If it shows error, Close the .css file when you make any change in .sass file.

Note: For scss code you can follow the same steps. You just have to put .scss file in "sass_folder" in this case.

[edited] In case you want to use Angular CLI:

At the time of creation of new Angular project use below mentioned cmnds:

For sass:

ng new Demo_Project --style=sass

For scss:

ng new Demo_Project --style=scss

To change the existing style:

ng set defaults.styleExt scss

After this you can use Cli normally.

Evan Wieland
  • 1,445
  • 1
  • 20
  • 36
AmanDeepSharma
  • 2,138
  • 4
  • 20
  • 34
  • 1
    Thanks for the detailed steps. Now I should link generated css files in css_folder to html file right? I mean like this - `` ? – Abhishek V Feb 24 '17 at 12:47
  • 3
    Instead of linking to html file you can also add the path in 'styleUrls' of @Component in .ts component file. – AmanDeepSharma Feb 25 '17 at 20:10
  • 3
    It'd be good to know how to run this using Angular CLI's default serve command `ng serve` rather than `npm start` – Danny Bullis May 30 '17 at 20:56
  • 2
    @DannyBullis I have edited the answer, check out at the end of answer in case you are using angular cli – AmanDeepSharma Jun 27 '17 at 07:04
  • You should add, that you actually have to type `npm run watch:sass` to watch the changes. This answer misleadingly sounds like it would be done just by running `npm start`. But that's not the case. You can even see, that the **build:watch** script never calls **watch:sass** – JHolub Aug 03 '17 at 13:02
  • If you add \"npm run watch:sass \" to the **start** script it works just fine – JHolub Aug 03 '17 at 13:22
  • @JHolub \"npm run watch:sass\" is already added in the start script – AmanDeepSharma Aug 14 '17 at 06:17
  • @AmanDeepSharma that's weird. It didn't work on my Mac and it didn't work on my girlfriends Windows laptop. And I had to manually add it. I have no idea what went wrong. – JHolub Aug 17 '17 at 21:18
  • just a style point. Since this is for learning should't we stick to basics and not use the npm command line shortcuts? – Manuel Hernandez Oct 26 '17 at 19:04
  • that scripts bit is all wrong. especially if you're using parcel. and running windows. – tatsu Feb 05 '18 at 14:45
5

I can explain you the first one.

If you are using ng serverthe compiled files are saved in a hidden folder in your project.

If you are using ng build, you can see your compiled files in the /dist folder. In this folder you can found your general styles in the file styles.[hashversion].css, but local component styles are included inside main.[hashversion].js by Webpack.

Angular-cli uses webpack, and if you want to learn more about, see Webpack Docs

UPDATE

In the second case, you have to compile sass manually. In the app folder un have a app.component.ts that will be compiled in the same folder to app.component.js by Typescript Compiler. So you have to do the same with sass. Import the CSS file in the component.

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`,
  stylesUrl: ['app/app.component.css']
})
export class AppComponent  { name = 'Angular'; }

Noticed that you cannot use relative path cause everything will be requested from root directory.

Create an app.component.sass and put your styles inside.

Then execute the sass compiler that compiles the app.component.sass to app.component.css

Run the server and it will work.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Serginho
  • 7,291
  • 2
  • 27
  • 52
1

There are two main scenarios here.

  1. When creating a new Angular CLI project, use CLI command

    ng new my-angular-app --style=scss

  2. When an Angular CLI project has already been set up, use CLI command

    ng set default.styleExt scss

In case 2, you need to manually convert existing .css files. You can use also use sass or less instead of scss

Check out the full article here.

Using SCSS/SASS/LESS in Angular CLI Project

Jayesh Panchal
  • 419
  • 4
  • 5