This tutorial is part 3 of 4 in 'Webpack with Style'-series.
- Part 1: How to set up Webpack 5 with Babel
- Part 2: How to use Webpack with CSS
- Part 4: How to use Webpack with PostCSS
If you happen to have a custom Webpack setup, you may be wondering how to set up SASS with Webpack. This short tutorial walks you through the process. First of all, you need to install a SASS loader and a SASS to your dev dependencies:
npm install --save-dev sass-loader node-sass
And second, you can use the SASS loader for all CSS and SCSS files in your Webpack configuration:
...module.exports = {...module: {rules: [...{test: /\.(scss|css)$/,use: ['style-loader', 'css-loader', 'sass-loader'],},],},...};
Then in a new src/style.scss file, add some CSS with SASS specific features (e.g. nested selectors) to it:
h1 {color: red;&:hover {color: blue;}}
And in your src/index.js file, or any other JS file, import this new CSS file:
import './style.scss';
That's it. From here you can use SASS in your JavaScript project which is powered by Webpack.
This tutorial is part 3 of 4 in 'Webpack with Style'-series.
- Part 1: How to set up Webpack 5 with Babel
- Part 2: How to use Webpack with CSS
- Part 4: How to use Webpack with PostCSS