Mario Basten
Mario Basten

Reputation: 1

Can not use background-image with webpack5

I have a problem with webpack5 and the use of background images. The problem is older, but I am a relatively new webpack user. As soon as I want to integrate a background image I get the info:

 background-image: url(${___CSS_LOADER_URL_REPLACEMENT_0___});
// Exports
2278 | /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (___CSS_LOADER_EXPORT___);

in the console.

here is my webpack.config file

module.exports = {
    entry: entryPoints,
    output: {
        path: path.resolve(__dirname, outputPath),
        filename: '[name].js',
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css',
        }),

        // Uncomment BrowserSyncPlugin if not wanted
        new BrowserSyncPlugin({
            proxy: localDomain,
            files: [ outputPath + '/*.css' ],
            injectCss: true,
        }, 
        { 
            reload: true, 
        }),
        
        new CleanWebpackPlugin({
            cleanOnceBeforeBuildPatterns: [
                outputPath + '/*',
            ]
          }),
    ],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.s?[c]ss$/i,
                use: [
                    
                    MiniCssExtractPlugin.loader,                   
                    'style-loader',                    
                    'css-loader',                    
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: true,
                        },
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: [
                                    autoprefixer
                                ]
                            }
                        }
                    }
                ],
            },
            {
                test: /\.sass$/i,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader',
                    {
                        loader: 'sass-loader',
                        options: {
                            sassOptions: { indentedSyntax: true },
                        },
                    },
                ],
            },
            {
                test: /\.(jpg|jpeg|png|gif|woff|woff2|eot|ttf|svg)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                        },
                    },
                ],
                type: 'javascript/auto'
            },
        ]
    },
    optimization: {
        minimizer: [
            `...`,
            new CssMinimizerPlugin(),
        ]
    },
};

Can somebody help me. I was looking for hours to fix this, but nothing worked....

I found several solutions on the internet, but none of them worked for my problem

Upvotes: 0

Views: 78

Answers (1)

biodiscus
biodiscus

Reputation: 880

MiniCssExtractPlugin.loader, <= remove                   
'style-loader',                    
'css-loader',          

The MiniCssExtractPlugin.loader can't be used together with style-loader.

Use the style-loader:

'style-loader',                    
'css-loader',          

Or the MiniCssExtractPlugin.loader:

MiniCssExtractPlugin.loader,                                  
'css-loader',          

Upvotes: 0

Related Questions