[英]How to make webpack bundle my assets/image/ although i call them from a url, into the component
Webpack is copying static png,jpg,svg
files that are imported into each component file. Webpack 正在复制导入到每个组件文件中的静态
png,jpg,svg
文件。
The file that hosts the image, it is fixed and the path is static.承载图像的文件,它是固定的,路径是静态的。
The component file is like so: mycomponent.ts组件文件是这样的: mycomponent.ts
import img from 'assets/img/aa.png
.... <img src={img} alt="" />
Below is a more complicated case.下面是一个更复杂的案例。
import imgurl from ${url}
import imgurl from ${url}
<img src={imgurl} />
<img src={imgurl} />
static
folder!!static
文件夹中!! (this is my goal) What i need webpack to do: .我需要 webpack 做什么:
assets/img
and move them into static/img
, as if the images had been imported from assets/img
, instead of url.assets/img
的图像并将它们移动到static/img
,就像图像是从assets/img
而不是 url 导入的一样。js & css
output with publicPath
: config.output.publicPath = http://www...
;publicPath
伪造js & css
输出时: config.output.publicPath = http://www...
; , snippet of bundling image, so far:到目前为止,捆绑图像的片段:
// 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 );
}
So i wonder if there is a way to bundle/copy images from assets/img
--> static/media
, althougt into react file i import them from a url ?所以我想知道是否有办法从
assets/img
--> static/media
捆绑/复制图像,但是我从 url 导入它们到 react 文件中?
You could use the copy-webpack-plugin
to copy images from assets/img
to static/media
like so:您可以使用
copy-webpack-plugin
将图像从assets/img
复制到static/media
如下所示:
const CopyPlugin = require("copy-webpack-plugin");
module.exports = {
plugins: [
new CopyPlugin({
patterns: [
{ from: "assets/img", to: "static/media" }
],
options: {
concurrency: 100,
},
}),
],
};
Documentation of copy-webpack-plugin
: https://webpack.js.org/plugins/copy-webpack-plugin/ copy-webpack-plugin
文档: https : copy-webpack-plugin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.