PatrickJS-starter icon indicating copy to clipboard operation
PatrickJS-starter copied to clipboard

how to use sass files

Open gmchaturvedi1 opened this issue 11 years ago • 65 comments

Kindly suggest how to use sass files for creating application

gmchaturvedi1 avatar Dec 01 '15 12:12 gmchaturvedi1

Currently sass/scss support is not implemented in the project. But webpack makes it easy to do so.

Install sass-loader via npm and register the loader for *.scss files. Add the following somewhere in the module loaders, e.g. below this line:

    {
        test: /\.scss$/,
        exclude: /node_modules/,
        loader: 'style-loader!css-loader!sass-loader'
      },

The sass-loader transformes your scss files into valid css code, which are then consumed by the css-loader and style-loader. For more details see also: https://github.com/jtangelder/sass-loader

dotcs avatar Dec 05 '15 10:12 dotcs

Still don't get it, @dotcs can you provide an example? I have a 'styles' folder under app. Something like this app |-styles |--main.scss

After installing sass-loader and put above config code at webpack.config. I don't see main.scss gets mapped to css and be loaded in browser.

lanocturne avatar Dec 08 '15 21:12 lanocturne

In webpack:

{
        test: /\.scss$/,
        exclude: /node_modules/,
        loader: 'raw-loader!sass-loader'
}

Then in your component:

@Component({
  styles: [ require('./filename.scss') ],
})

Angular2 expects a string in the styles array, because it modifies the string to encapsulate the styling. Using style-loader/css-loader loads the css and inject to the html, without encapsulating, and doesn't return a string, raw-loader just load the file as string and pass it to sass-loader, which also return a string.

asafyish avatar Dec 16 '15 07:12 asafyish

What about global styles, ones not tied to components?

Using loaders: ["style", "css", "sass"] works but throws errors preventing the app to run.

liampmccabe avatar Dec 29 '15 16:12 liampmccabe

Please use sass to raw and avoid style-loader unless you want global style support. There's another way to support global styles and still maintain the raw loader. By removing encapsulation in the top-level component and requiring the CSS that should be global.

PatrickJS avatar Dec 31 '15 03:12 PatrickJS

If anyone else was a little confused to what @gdi2290 suggested:

import {ViewEncapsulation} from 'angular2/core';

@Component({
  selector: 'app',
  styles: [
    require('bootstrap.scss')
  ],
  encapsulation: ViewEncapsulation.None,
  template: ``
})
class App {}

liampmccabe avatar Jan 02 '16 16:01 liampmccabe

Opened #199 to consolidate these issues.

manavsehgal avatar Jan 05 '16 01:01 manavsehgal

@liampmccabe thanks for the heads-up. I was confused :wink:

gonzofish avatar Feb 01 '16 15:02 gonzofish

I created a scss wiki page if anyone wants to update it to be more clear https://github.com/AngularClass/angular2-webpack-starter/wiki/How-to-include-SCSS-in-components

Tipe CMS

PatrickJS avatar Feb 01 '16 16:02 PatrickJS

@gdi2290 - Is it possible to override bootstrap variables along with the implementation from your wiki?

dvh91 avatar Feb 02 '16 18:02 dvh91

@gdi2290 your wiki page works fine,

if you import in your sass file other css files you have the problem with the relative URLs in your CSS files. The avoid that do this

https://github.com/bholloway/resolve-url-loader#

in webpack.config.js do this

// SASS loader
{ test: /\.scss$/, 
  exclude: /node_modules/,  
  loaders: ['raw-loader', 'sass-loader', 'resolve-url']
}

@gdi2290 I don't like at all load my scss file in top level component by styles and encapsulation. Isn't there a possibility to load the compiled CSS file in the index.html file?

thx Michael

mburger81 avatar Feb 10 '16 12:02 mburger81

@mburger81 you can import it as you normally would using the embedded stylesheets(see https://webpack.github.io/docs/stylesheets.html) then in your app.ts you would have

require("./app.scss");

@Component({
  selector: 'app',
  template:``
})

Tipe CMS

PatrickJS avatar Feb 10 '16 21:02 PatrickJS

There are cases where you need global styles and also enjoy the benefits of encapsulated styles in components. I have adopted a policy of naming component sass files so they end with component.scss. Webpack can then choose the right loader chain like this:

{ test: /^(?!.*component).*\.scss$/, loaders: ['style', 'css', 'resolve-url', 'sass'] },
{ test: /\.component\.scss$/, loaders: ['raw', 'resolve-url', 'sass'] },

Of course, be sure to create a common or global entry point containing your global styles and load it in before your bundle containing angular so the css can be applied in the correct order.

akeating avatar Apr 03 '16 15:04 akeating

@akeating Can you explain what you mean, in a different way? I don't quite understand what the benefit of having both of those loaders is. I would like to use global styles as well as encapsulated styles too. Also, can you give an example of your .scss file names to help clarify? Thanks.

IAMtheIAM avatar May 11 '16 22:05 IAMtheIAM

Hey guys, my sass is working perfectly with my components, but I can't make it work with karma. what should I do?

MFateen avatar May 12 '16 10:05 MFateen

@mburger81 that doesn't works, and 'resolve-url' should go before sass-loader, webpack loaders are loaded from right to left, so resolve-url fail with sass rules (however after change it to the correct way it doesn't work), the builded css leaves the url like the sass and file-loader (I use it) don't copy the assets.

sliker avatar May 24 '16 05:05 sliker

I have sass working. I have a bootstrap.scss that I load as a global style, and then other styles for each component. The only issue now is that when I put variables in my component style sheets that I define in my global style they don't work.

Any ideas about how to get it all to work?

Thanks!

Scott

scottmahr avatar Jun 15 '16 19:06 scottmahr

@scottmahr perhaps break your variables into a separate common file _variables.scss and import it as needed at the top of your component similarly to the way bootstrap does (obviously the path will be different):

// Core variables and mixins
@import "variables";
@import "mixins";

akeating avatar Jun 16 '16 04:06 akeating

raw!sass pipe works fine for me but when I use UglifyJS plugin it breaks the styles... I have opened a question on stack overflow, you can see more details there.

slavik57 avatar Jul 15 '16 17:07 slavik57

@akeating I've tried using your proposal for encapsulated styles in components

{ test: /\.component\.scss$/, loaders: ['raw', 'resolve-url', 'sass'] }

However, it's not resolving my url() paths. The final CSS simply has the same path as defined in the component's sass code.

Is there some additional config to go along with this?

rjmarques avatar Jul 17 '16 17:07 rjmarques