how to use sass files
Kindly suggest how to use sass files for creating application
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
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.
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.
What about global styles, ones not tied to components?
Using loaders: ["style", "css", "sass"] works but throws errors preventing the app to run.
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.
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 {}
Opened #199 to consolidate these issues.
@liampmccabe thanks for the heads-up. I was confused :wink:
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
@gdi2290 - Is it possible to override bootstrap variables along with the implementation from your wiki?
@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 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:``
})
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 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.
Hey guys, my sass is working perfectly with my components, but I can't make it work with karma. what should I do?
@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.
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 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";
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.
@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?
