gpt4 book ai didi

javascript - webpack - scss 无法解析背景图像 url

转载 作者:行者123 更新时间:2023-12-04 17:07:54 25 4
gpt4 key购买 nike

在我的 scss 文件中,我使用 background-image: url("../../../assets/images/home/banner-mobile.png");应用程序运行成功,但没有显示背景图像:
背景图像 URL 未解析。
enter image description here
webpack/webpack.base.js

const webpack = require("webpack");
const path = require("path");

const utils = require("./utils");

const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
entry: "./src/index.jsx",
resolve: {
alias: {
"@": utils.resolve("src")
},
extensions: ["*", ".js", ".jsx"],
fallback: {...},
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: "style-loader",
},
{
loader: "css-loader",
},
{
loader: "sass-loader"
},
{
loader: "sass-resources-loader",
options: {
resources: ["./src/assets/scss/main.scss"],
},
},
],
},
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: "url-loader",
options: {
limit: 8192
},
},
],
}
],
},
plugins: [
new HtmlWebpackPlugin({
template: "public/index.html",
filename: "index.html",
inject: true,
})
],
};
webpack/webpack.dev.js
const { merge } = require("webpack-merge");
const base = require("./webpack.base");

const Dotenv = require("dotenv-webpack");

module.exports = merge(base, {
mode: "development",
devtool: "inline-source-map",
output: {
publicPath: '/',
},
devServer: {
port: 3000,
static: true,
static: 'dist'
},
plugins: [new Dotenv({ path: "./.env.development" })],
});
更新 1
当我在 Web Inspector > Sources 中查看 png 时:
enter image description here
当我在浏览器中打开带有 URL 的图像时:
enter image description here
更新 2:
当我通过 VSCode 构建和查看图像时,它显示如下:
enter image description here
不确定以下文件是否相关
webpack/Util.js
const path = require('path')

module.exports = {
resolve: function(dir) {
return path.join(__dirname, '..', dir)
}
}

最佳答案

由于您使用的是 Webpack 5,我建议使用 Asset Modules而不是弃用的加载器

module: {
rules: [
// ...
{
test: /\.(png|jpg|gif)$/i,
type: "asset",
parser: {
dataUrlCondition: {
maxSize: 8192
}
}
}
]
}
我怀疑您遇到了文档中所述的资源处理重复问题...

When using the old assets loaders (i.e. file-loader / url-loader / raw-loader) along with Asset Module in webpack 5, you might want to stop Asset Module from processing your assets again as that would result in asset duplication. This can be done by setting asset's module type to 'javascript/auto'.

关于javascript - webpack - scss 无法解析背景图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70149171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com