【发布时间】:2021-12-08 14:05:50
【问题描述】:
Webpack 正在复制静态 png,jpg,svg 文件,这些文件被导入到每个组件文件中。
承载图像的文件,它是固定的,路径是静态的。
组件文件是这样的:mycomponent.ts
import img from 'assets/img/aa.png
.... <img src={img} alt="" />
下面是一个更复杂的案例。
- 从 url 导入图像:
import imgurl from ${url} - 将其用于
<img src={imgurl} /> - 和 webpack,仍然将图像移动到
static文件夹中!! (这是我的目标)
我需要 webpack 做什么:。
- 将
assets/img中的图片捆绑到static/img中,就好像图片一样 已从assets/img导入,而不是 url。 - 到目前为止,webpack 会忽略从 url 导入的图像,可能是因为在构建过程中找不到它们李>
- 但是当我用
publicPath伪造js & css输出时:config.output.publicPath =http://www...;,
图像也可以这样做吗?
捆绑图片的sn-p,到目前为止:
// so, this changes copy the imported, into react components, images, into
// static/media
// but it copies ONLY the images that are imported from `assets/img`
// it ignores if i import an image from a url e.g. import img from ${url}
function addMedia(config, outputPath){
const media = {
test: /\.(woff(2)?|ttf|eot|svg|png)(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash:8].[ext]',
outputPath: 'static/media',
}
}
]
};
config.module.rules.push( media );
}
所以我想知道是否有办法从assets/img 捆绑/复制图像--> static/media, althougt 到反应文件 我从一个 url 导入它们?
【问题讨论】:
标签: javascript node.js reactjs webpack webpack-4