gpt4 book ai didi

Webpack 不加载 png 图像

转载 作者:行者123 更新时间:2023-12-02 10:38:52 24 4
gpt4 key购买 nike

我在 src 文件夹中有几张图片:

src/img/favicon.png
src/img/profpic.png

在 index.html 文件中,我将指向
    <link rel="shortcut icon" href="img/favicon.png" />

在一些 html 文件中,我会指出
    <img src="img/profpic.png" />

我正在尝试通过 webpack 加载图像、字体。下面是我的 webpack.config
module.exports = {
context: path.resolve('src'),
entry: {
app: ["./index.ts"]
},
output: {
path: path.resolve('build'),
filename: "appBundle.js"
},
devServer: {
contentBase: 'src'
},
watch: true,
module: {
preLoaders: [
{
test: /\.ts$/,
loader: "tslint"
}
],
loaders: [
{test: /\.ts(x?)$/, exclude: /node_modules/,loaders: ['ng-annotate-loader','ts-loader']},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.scss$/,
loader: 'style!css!sass'
}, {
test: /\.html$/,
exclude: /node_modules/,
loader: 'raw'
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=1000000&mimetype=application/font-woff'
}, {
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}, {
test: /\.json$/,
loader: "json-loader"
}, {
test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'
}
];
}
plugins: [
new HtmlWebpackPlugin({
template: './index.html',
inject: 'body',
hash: true
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
'window.jquery': 'jquery'
})
],
resolve: {
extensions: ['', '.js', '.es6', '.ts']
}
}

尝试将图像/字体加载到 webpack 输出文件夹。它没有抛出任何错误。它成功构建但在构建中,这是我的 webpack 输出文件夹字体加载正常但图像未加载

Build Folder after running webpack

最佳答案

将原始加载程序更改为 html 加载程序以完成 html 的操作。下面是我的改动

{test: /\.ts(x?)$/, exclude: /node_modules/,loaders: ['ng-annotate-loader','ts-loader']},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.scss$/,
loader: 'style!css!sass'
}, {
**test: /\.html$/,
exclude: /node_modules/,
loader: 'html-loader'**
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=1000000&mimetype=application/font-woff'
}, {
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}, {
test: /\.json$/,
loader: "json-loader"
}, {
**test: /\.png$/,
exclude: /node_modules/,
loader: 'file-loader?name=images/[name].[ext]'**
}

关于Webpack 不加载 png 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343300/

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