gpt4 book ai didi

javascript - 如何使用 webpack file-loader 加载图像文件

转载 作者:IT王子 更新时间:2023-10-29 02:39:13 28 4
gpt4 key购买 nike

我正在使用 webpack 来管理一个 reactjs 项目。我想通过 webpack file-loader 在 javascript 中加载图像。下面是 webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const NpmInstallPlugin = require('npm-install-webpack-plugin');

const PATHS = {
react: path.join(__dirname, 'node_modules/react/dist/react.min.js'),
app: path.join(__dirname, 'src'),
build: path.join(__dirname, './dist')
};

module.exports = {
entry: {
jsx: './app/index.jsx',
},
output: {
path: PATHS.build,
filename: 'app.bundle.js',
},
watch: true,
devtool: 'eval-source-map',
relativeUrls: true,
resolve: {
extensions: ['', '.js', '.jsx', '.css', '.less'],
modulesDirectories: ['node_modules'],
alias: {
normalize_css: __dirname + '/node_modules/normalize.css/normalize.css',
}
},
module: {
preLoaders: [

{
test: /\.js$/,
loader: "source-map-loader"
},
],
loaders: [

{
test: /\.html$/,
loader: 'file?name=[name].[ext]',
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader?presets=es2015',
},
{test: /\.css$/, loader: 'style-loader!css-loader'},
{test: /\.(jpe?g|png|gif|svg)$/i, loader: "file-loader?name=/public/icons/[name].[ext]"},
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ['babel-loader?presets=es2015']
}
]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
output: {
comments: false,
},
}),
new NpmInstallPlugin({
save: true // --save
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production")
}
}),
],
devServer: {
colors: true,
contentBase: __dirname,
historyApiFallback: true,
hot: true,
inline: true,
port: 9091,
progress: true,
stats: {
cached: false
}
}
}

我用这一行加载图像文件并将它们复制到 dist/public/icons 目录并保持相同的文件名。

{test: /\.(jpe?g|png|gif|svg)$/i, loader: "file-loader?name=/public/icons/[name].[ext]"}

但是我在使用的时候遇到了两个问题。当我运行 webpack 命令时,图像文件按预期被复制到 dist/public/icons/ 目录。然而,它也被复制到 dist 目录,文件名为“df55075baa16f3827a57549950901e90.png”。

下面是我的项目结构: enter image description here

另一个问题是我使用下面的代码导入了这个图像文件,但它没有显示在浏览器上。如果我在 img 标签上使用 url 'public/icons/imageview_item_normal.png',它工作正常。如何使用从图片文件中导入的对象?

import React, {Component} from 'react';
import {render} from 'react-dom';
import img from 'file!../../public/icons/imageview_item_normal.png'

export default class MainComponent extends Component {

render() {
return (
<div style={styles.container}>
download
<img src={img}/>
</div>
)
}

}

const styles = {
container: {
width: '100%',
height: '100%',
}
}

最佳答案

关于问题#1

一旦你在 webpack.config 中配置了文件加载器,每当你使用 import/require 时,它​​都会针对所有加载器测试路径,如果有匹配项,它就会通过该加载器传递内容。在你的情况下,它匹配

{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: "file-loader?name=/public/icons/[name].[ext]"
}

// For newer versions of Webpack it should be
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader',
options: {
name: '/public/icons/[name].[ext]'
}
}

因此您会看到发射到的图像

dist/public/icons/imageview_item_normal.png

这是想要的行为。

您还获得哈希文件名的原因是因为您要添加一个额外的内联文件加载器。您正在将图像导入为:

'file!../../public/icons/imageview_item_normal.png'.

file!为前缀,再次将文件传入file-loader,这次没有name配置。

所以你的导入应该只是:

import img from '../../public/icons/imageview_item_normal.png'

更新

正如@cgatian 所指出的,如果您真的想使用内联文件加载器,忽略 webpack 全局配置,您可以在导入前加上两个感叹号 (!!):

import '!!file!../../public/icons/imageview_item_normal.png'.

关于问题#2

导入 png 后,img 变量只保存文件加载器“知道的”路径,即 public/icons/[name].[ext](又名 “file-loader?name=/public/icons/[name].[ext]”)。您的输出目录“dist”未知。您可以通过两种方式解决此问题:

  1. 在“dist”文件夹下运行所有​​代码
  2. publicPath 属性添加到您的输出配置,它指向您的输出目录(在您的例子中为 ./dist)。

例子:

output: {
path: PATHS.build,
filename: 'app.bundle.js',
publicPath: PATHS.build
},

关于javascript - 如何使用 webpack file-loader 加载图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671342/

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