Webpack 4. Compile scss to separate css file

Other answers gave me just headache as they were not working. I did a lot of googling and I realized you can compile scss into separate css file without using any additional plugins

webpack.config.js

    const path = require('path');

    module.exports = {
        entry: [
            __dirname + '/src/js/app.js',
            __dirname + '/src/scss/app.scss'
        ],
        output: {
            path: path.resolve(__dirname, 'dist'), 
            filename: 'js/app.min.js',
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: [],
                }, {
                    test: /\.scss$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: 'file-loader',
                            options: { outputPath: 'css/', name: '[name].min.css'}
                        },
                        'sass-loader'
                    ]
                }
            ]
        }
    };

package.json

    {
      "name": "...",
      "version": "1.0.0",
      "description": "...",
      "private": true,
      "dependencies": {},
      "devDependencies": {
        "file-loader": "^5.0.2",
        "node-sass": "^4.13.1",
        "sass-loader": "^8.0.2",
        "webpack": "^4.41.5",
        "webpack-cli": "^3.3.10"
      },
      "scripts": {
        "build": "webpack --config webpack.config.js --mode="production""
      },
      "keywords": [],
      "author": "...",
      "license": "ISC"
    }

Dependencies:

npm install --save-dev webpack webpack-cli file-loader node-sass sass-loader

How to run JS and SCSS compilation

npm run build

Leave a Comment

techhipbettruvabetnorabahisbahis forumu