Using SCSS in a webpack project
🎯 Introduction
By the end of this article, you will be able to integrate SCSS into a webpack project for more structured and flexible style management. You will learn to install and configure the required loaders, organize your project, and write modular, maintainable styles. These skills improve development efficiency and code quality in everyday projects.
🎨 Installing sass-loader and configuring style-loader
The central piece of SCSS configuration is sass-loader, which lets webpack process and compile .scss files. It works alongside css-loader and style-loader:
sass-loader: Converts SCSS to CSS.css-loader: Processes CSS files.style-loader: Inserts the processed CSS into the page using<style>elements.
Install dependencies
Open a terminal in your project root and run:
npm install --save-dev sass-loader sass css-loader style-loader
sass-loader relies on sass, which performs the actual compilation.
Configure webpack
Add a basic configuration like this to webpack.config.js:
module.exports = {
// Other configuration omitted
module: {
rules: [
{
test: /\.scss$/, // Match all .scss files
use: [
'style-loader', // Inject CSS into the DOM
'css-loader', // Process CSS
'sass-loader', // Compile SCSS to CSS
],
},
],
},
};
use run from right to left: SCSS is compiled into CSS, CSS is processed into a JavaScript module, and the styles are injected into the DOM.📂 Importing .scss files
Once the loaders are configured, you can import SCSS files just like JavaScript modules.
Suppose a styles folder contains main.scss.
Import it in your JavaScript entry file, such as index.js:
import './styles/main.scss';
webpack processes the import automatically, and the styles are applied to the page.
Example
Suppose main.scss contains the following:
$primary-color: #42b983;
body {
background-color: $primary-color;
font-family: Arial, sans-serif;
}
After importing it, the page background turns green.
.scss extension so webpack applies the appropriate loaders.❓ Questions and answers
Q1: Why do I need sass-loader, css-loader, and style-loader together?
sass-loader compiles SCSS into CSS. css-loader processes the CSS, including dependencies and paths. style-loader injects it into the page using <style> elements. Together, they handle the complete process from SCSS source to styles on the page.
🧪 Exercises
Exercise 1: Update webpack.config.js to support .css files using the appropriate loaders from the same setup.
module.exports = {
module: {
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
};
Exercise 2: Create main.scss, import it into main.js, and use its styles.
$primary-color: #42b983;
body {
background-color: $primary-color;
font-family: Arial, sans-serif;
}
import './styles/main.scss';
🎉 You now know how to install and configure SCSS in webpack, import SCSS files, and organize your styles. These foundations are useful in everyday frontend projects and prepare you to explore Sass and style optimization further. Happy coding!
