无法从 Css 文件中加载背景图片 Webpack5

huangapple go评论66阅读模式
英文:

Unable to load background image from Css file Webpack5

问题

.container {
  width: 300px;
  height: 300px;
  background-image: url("../images/gaia.png");
  background-size: auto;
}
英文:

I'm trying to put an image as background in a div but it doesn't recognize it because webpack changes the path to http://localhost:8080/gaia.png instead of http://localhost:8080/assets/images/gaia.png
The image is displayed correctly inside an <img> tag but it doesn't load if I want to use it from the Css file.
I'm sure the problem is the file path but I don't know how to fix it.
无法从 Css 文件中加载背景图片 Webpack5

无法从 Css 文件中加载背景图片 Webpack5

index.js

import &#39;./assets/style/style.css&#39;
import &#39;./assets/images/gaia.png&#39;
import &#39;./assets/fonts/CascadiaCode.ttf&#39;

const fun = () =&gt; {
  console.log(&#39;hey&#39;)
}
fun();

webpack.config.js

const HtmlWebpackPlugin = require(&quot;html-webpack-plugin&quot;);
const path = require(&#39;path&#39;);

module.exports = {
  mode: &#39;development&#39;,
  devtool: &#39;inline-source-map&#39;,
  entry: &#39;./src/index.js&#39;,
  output: {
    filename: &#39;main.js&#39;,
    path: path.resolve(__dirname, &#39;dist&#39;),
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, &#39;src&#39;, &#39;index.html&#39;)
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: [&#39;style-loader&#39;, &#39;css-loader&#39;],
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [&quot;babel-loader&quot;]
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: &#39;asset/resource&#39;,
        generator: {
          filename: &#39;[name][ext][query]&#39;,
          outputPath: &#39;assets/images/&#39;,
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: &#39;asset/resource&#39;,
        generator: {
          filename: &#39;[name][ext][query]&#39;,
          outputPath: &#39;assets/fonts/&#39;,
        },
      },
    ],
  },
};

style.css

.container {
  width: 300px;
  height: 300px;
  background-image: url(&quot;../images/gaia.png&quot;);
  background-size: auto;
}

答案1

得分: 1

删除输出路径并设置为:

generator: {
    filename: 'assets/images/[hash][ext][query]'
}
英文:

delete outputpath and set it up as :

generator: {
         filename: &#39;assets/images/[hash][ext][query]&#39;
       }
     }

答案2

得分: 0

我通过安装特定的Html图像加载器<img>来解决了这个问题:

npm install --save-dev html-loader image-webpack-loader

我按照这个教程操作:使用Webpack优化静态HTML和图片

这是我的webpackconfig.js文件:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    static: './dist',
  },
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src', 'index.html'),
    }),
  ],
  module: {
    rules: [
      {
        test: /\.css/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.html$/i,
        use: 'html-loader',
      },
      {
        test: /\.(png|jpg)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'images/[name]-[hash][ext]',
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: 'asset/resource',
        generator: {
          filename: 'fonts/[name]-[hash][ext]',
        },
      },
    ],
  },
};

我还创建了一个Webpack模板,你可以在我的GitHub上使用默认配置:
Webpack模板仓库

英文:

I fixed the problem by installing a specific loader for Html images <img>

npm install --save-dev html-loader image-webpack-loader

I followed this tutorial: Optimizing Static HTML And Images With Webpack

Here is my webpackconfig.js file:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const HtmlWebpackPlugin = require(&#39;html-webpack-plugin&#39;);
const path = require(&#39;path&#39;);

module.exports = {
  mode: &#39;development&#39;,
  devtool: &#39;inline-source-map&#39;,
  devServer: {
    static: &#39;./dist&#39;,
  },
  entry: &#39;./src/index.js&#39;,
  output: {
    filename: &#39;main.js&#39;,
    path: path.resolve(__dirname, &#39;dist&#39;),
    clean: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, &#39;src&#39;, &#39;index.html&#39;),
    }),
  ],
  module: {
    rules: [
      {
        test: /\.css/,
        use: [&#39;style-loader&#39;, &#39;css-loader&#39;],
      },
      {
        test: /\.html$/i,
        use: &#39;html-loader&#39;,
      },
      {
        test: /\.(png|jpg)$/i,
        type: &#39;asset/resource&#39;,
        generator: {
          filename: &#39;images/[name]-[hash][ext]&#39;,
        },
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: &#39;asset/resource&#39;,
        generator: {
          filename: &#39;fonts/[name]-[hash][ext]&#39;,
        },
      },
    ],
  },
};

<!-- end snippet -->

I also created a Webpack Template that you can use with the default configuration on my GitHub:
Webpack Template Repo

huangapple
  • 本文由 发表于 2023年2月10日 05:05:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定