How to config Webpack with @import in style.scss?

Viewed 119

Who can help me with a problem in the Webpack settings when adding @import to style.scss webpack returns ModuleBuildError.

webpack.config.js

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
    entry: {
        main: path.join(__dirname, 'src', 'webpack_entry_point'),
    },
    output: {
        filename: 'bundle.[hash].js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.[contenthash].css'
        })
    ],
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                include: path.resolve()
            },
            {
                test:/\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "sass-loader"
                ]
            }

        ]
    }
};

webpack_entry_point.js

import './webpack_style.scss';

webpack_style.scss

@import 'css/style';

ModuleBuildError

1 Answers

The problem was because the images folder ./src/images was empty, and my webpack.config.js had no file-loader like:

{
    test: /\.(jpe?g|png|gif|bmp|webp)$/,
    use: [
        {
            loader: 'file-loader',
            options: {
                name: '[name].[ext]',
                outputPath: path.join('assets', 'images'),
                publicPath: path.join('assets', 'images')
            }
        }
    ]
},
    
Related