简体   繁体   English

如何使 webpack 捆绑我的资产/图像/ 尽管我从 url 调用它们到组件中

[英]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.下面是一个更复杂的案例。

  1. import an image from a url : import imgurl from ${url}从 url 导入图像: import imgurl from ${url}
  2. use it into <img src={imgurl} />将其用于<img src={imgurl} />
  3. and webpack, still, move the images into static folder!!和 webpack,仍然,将图像移动到static文件夹中!! (this is my goal) (这是我的目标)

What i need webpack to do: .我需要 webpack 做什么:


  1. bundle the images from 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 导入的一样。
  2. so far webpack ignores the images that are imported from a url , possibly because it cant find them , during the build process到目前为止,webpack 忽略了从url导入的图像,可能是因为它在构建过程中找不到它们
  3. but as i fake the js & css output with publicPath : config.output.publicPath = http://www... ;但是当我用publicPath伪造js & css输出时: config.output.publicPath = http://www... ; , ,
    similarly can it be done for the images ?同样可以对图像进行处理吗?

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.

相关问题 Webpack 5 - 如何从 npm 模块捆绑资产 - Webpack 5 - how to bundle assets from an npm module 如何完全捆绑资产文件夹-Webpack - How to bundle assets folder fully - webpack 如何使用 webpack 和 file-loader 在两个捆绑 (scss) 文件中使用一个图像? 最后一个返回 undefined - How can I use one image in two bundle (scss) files with webpack and file-loader? The last one of them returns undefined 如何从webpack中的资源url中删除后缀斜杠“/” 盖茨比 - How to remove suffix slash “/” from the assets url in webpack | Gatsby React + Webpack提取文本插件。 我究竟做错了什么? 我的样式从bundle.js工作,但是我试图将它们移到bundle.css - React + Webpack Extract Text Plugin. What am I doing wrong? My styles work from bundle.js, but I am trying to move them out to bundle.css 在React中,如何直接从webpack捆绑包导入组件? - In React, How to Import Component from webpack bundle directly? 如果事件以HTML文件编写,如何从webpack捆绑包中调用事件? - How can I call event from webpack bundle if event written in HTML file? 如何从 webpack 创建的包中引用或访问我的类? - How do I reference or access my classes from a bundle created by webpack? 如何使用webpack从多个脚本中制作一个捆绑包? - How to make one bundle from several scripts using webpack? 如何让 Next.Js 下载我在网站中使用的所有外部资源并在本地提供它们? - How can I make Next.Js download all the external assets I used in my website and serve them locally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM