I
I
Ivan2021-06-24 01:04:03
webpack
Ivan, 2021-06-24 01:04:03

Why does the url of the image change after Webpack build?

background: url(../img/intro__bg.jpg) in main.scss I write this path to the image, the output is: background:url(assets/img/intro__bg.jpg.

tried to process scss using resolve-url-loader, but didn't help

...
module: {
        rules: [
            {
                test: /\.(scss|css)$/,
                use: [{
                    loader: MiniCSSExtractPlugin.loader
                }, {
                    loader: 'css-loader',
                    options: {
                      url: true,
                      sourceMap: true,
                    }
                  },'postcss-loader',
                  {
                    loader: 'resolve-url-loader',
                    options: {
                      root: paths.src
                    }
                  }, 
                  {
                    loader: 'sass-loader',
                    options: {
                      sourceMap: true,
                    }
                  }]
            },

...


Webpack code:

common
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const MiniCSSExtractPlugin = require('mini-css-extract-plugin')

const fs = require('fs')
const paths = require('./paths')
const path = require('path')

const pages_dir = `${paths.src}/pages`
const pages_dirs = fs.readdirSync(pages_dir).filter(filename => filename.endsWith(''))

const component_dir = `${paths.src}/components`
const component_dirs = fs.readdirSync(component_dir).filter(filename => filename.endsWith(''))

module.exports = {
    entry: {
        index: paths.src + '\\index.js'
    },
    output: {
        filename: `${paths.assets}js/[name].js`,
        path: paths.dist,
        assetModuleFilename: 'assets/img/[name][ext]'
    },
    optimization: {
        splitChunks: {
            chunks: "all",
            minSize: 1,
            minChunks: 2
        }
    },
    module: {
        rules: [
            {
                test: /\.(scss|css)$/,
                use: [{
                    loader: MiniCSSExtractPlugin.loader
                }, {
                    loader: 'css-loader',
                    options: {
                      url: true,
                      sourceMap: true,
                    }
                  },'postcss-loader',
                  {
                    loader: 'resolve-url-loader',
                    options: {
                      root: paths.src
                    }
                  }, 
                  {
                    loader: 'sass-loader',
                    options: {
                      sourceMap: true,
                    }
                  }]
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader'],
            },
            {
                test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
                type: 'asset',
            },
            {
                test: /\.(woff(2)?|eot|ttf|otf|svg|)$/,
                type: 'asset/resource',
            },
            {
                test: /\.pug$/,
                loader: 'pug-loader'
            }
        ],
    },
    plugins: [
        new CleanWebpackPlugin(),
        ...pages_dirs.map(dir => new HtmlWebpackPlugin({
            template: `${pages_dir}/${dir}/${dir}.html`,
            filename: `${dir}.html`,
            chunks: [`${dir}`],
            inject: 'body'
        })),
        new MiniCSSExtractPlugin({
            filename: `${paths.assets}css/[name].css`
        }),
    ],
}


dev
const webpack = require('webpack')
const { merge } = require('webpack-merge')

const common = require('./webpack.common')
const paths = require('./paths')

module.exports = merge(common, {
    mode: 'development',
    target: 'web',
    devtool: 'source-map',
    devServer: {
        contentBase: paths.dist,
        open: true,
        hot: true,
        port: 8080,
        writeToDisk: true,
        serveIndex: true,
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ]
})


product
const { merge } = require('webpack-merge')

const paths = require('./paths')
const common = require('./webpack.common.js')

module.exports = merge(common, {
    mode: 'production',
})


project structure:
60d3afc8be810560220161.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan, 2021-06-24
@Vamba45

This helped:

output: {
 publicPath: '../../'
}

D
Denis Masson, 2021-06-24
@pumbasl

You answered your own question, "After building with Webpack".
I think this screenshot on the main webpack page will tell you what's going on>
60d40607dfea3316897285.png

E
Egor Danchenko, 2021-07-24
@YahorDanchanka

The assetModuleFilename field specifies where the files will go after the build. Delete the property or redefine the value. All images after assembly will be on the path outputPath/assetModuleFilename

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question