英文:
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.
index.js
import './assets/style/style.css'
import './assets/images/gaia.png'
import './assets/fonts/CascadiaCode.ttf'
const fun = () => {
console.log('hey')
}
fun();
webpack.config.js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require('path');
module.exports = {
mode: 'development',
devtool: 'inline-source-map',
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'src', 'index.html')
})
],
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.js$/,
exclude: /node_modules/,
use: ["babel-loader"]
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
generator: {
filename: '[name][ext][query]',
outputPath: 'assets/images/',
},
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: {
filename: '[name][ext][query]',
outputPath: 'assets/fonts/',
},
},
],
},
};
style.css
.container {
width: 300px;
height: 300px;
background-image: url("../images/gaia.png");
background-size: auto;
}
答案1
得分: 1
删除输出路径并设置为:
generator: {
filename: 'assets/images/[hash][ext][query]'
}
英文:
delete outputpath and set it up as :
generator: {
filename: 'assets/images/[hash][ext][query]'
}
}
答案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('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]',
},
},
],
},
};
<!-- end snippet -->
I also created a Webpack Template that you can use with the default configuration on my GitHub:
Webpack Template Repo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论