首页 > 解决方案 > 如何在 style.scss 中使用 @import 配置 Webpack?

问题描述

将@import 添加到 style.scss webpack 返回 ModuleBuildError 时,谁能帮我解决 Webpack 设置中的问题。

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';

模块构建错误

标签: webpack

解决方案


问题是因为图像文件夹 ./src/images 是空的,而我的 webpack.config.js 没有像这样的文件加载器:

{
    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')
            }
        }
    ]
},
    

推荐阅读