This post shows how to use SASS with Webpack and Angular 2 in Visual Studio. I had various problems trying to get this to work from Visual Studio using a Webpack build. The following is a solution which works, but not the only one.
Code: https://github.com/damienbod/Angular2WebpackVisualStudio
Install node-sass globally, so that the package is available everywhere. The latest installed node-sass will then be available on the path.
npm install node-sass -g
Add the SASS packages as required in the project npm packages.json file.
"devDependencies": { "node-sass": "3.10.1", "sass-loader": "^3.1.2", "style-loader": "^0.13.0", ... }
The SASS configuration can then be added to the Webpack config file(s). The SASS scss files are built as part of the Webpack build.
{ test: /\.scss$/, loaders: ["style", "css", "sass"] },
Now a SASS file can be created and appended to any Angular 2 component.
body { padding-top: 50px; } .starter-template { padding: 40px 15px; text-align: center; } .navigationLinkButton:hover { cursor: pointer; } a { color: #03A9F4; }
The scss file or files can be used in the Angular 2 component typescript file using the @Component. The styles property defines an array of strings so each scss require method, needs to be converted to a string, otherwise it will not work. Thanks to Jackie Gleason for this solution.
import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'my-app', template: require('./app.component.html'), styles: [String(require('./app.component.scss')), String(require('../style/app.scss'))] }) export class AppComponent { constructor(private router: Router) { } }
If you have the following error message somewhere in your Webpack build: binding.node. Try reinstalling `node-sass`, try the following fixes:
1: Reinstall node-sass
npm install node-sass -g
2: Edit the Visual Studio project and settings
You can use the node from your path, and not the installed Visual Studio node by changing the Tools > Options > Projects and Solutions > External Web Tools. Move the path option to the top. This solution is from Mads Kristensen and explained here: solution
m1les gave this solution in Stack overflow.
You can then view the css styles created from SASS Visual Studio Webpack build.
Links
http://stackoverflow.com/questions/31301582/task-runner-explorer-cant-load-tasks/31444245
https://github.com/webpack/style-loader/issues/123
https://www.bensmithett.com/smarter-css-builds-with-webpack/
https://github.com/jtangelder/sass-loader
http://eng.localytics.com/faster-sass-builds-with-webpack/
