【问题标题】:Webpack favicons into different folderWebpack favicons 到不同的文件夹
【发布时间】:2018-02-22 15:52:16
【问题描述】:

我有两个文件夹 src/images/src/images/icons。 所有网站图标均采用png 格式。

src/images/icons 中,我将不同设备的所有网站图标放入了wwwroot/images/icons,所有其他图像放入wwwroot/images

如何区分图片和网站图标?

现在我有图片:

 {
            test: /\.(png|ico|svg|jpg|gif)$/,
            use: [
                'file-loader?name=/images/[name].[ext]'
            ]
        },

但这会将所有图像复制到dist\images,包括图标,它应该在文件夹dist\images\icons中更深一层

【问题讨论】:

    标签: webpack-3


    【解决方案1】:

    有几种方法可以做到这一点(即使用test 键对文件名、单独的规则等)。但是,这里有一种似乎效果很好且相当明确的方法:

    const path = require('path')
    
    module.exports = {
      // ...
      module: {
        rules: [
    
          {
            test: /\.(png|ico|svg|jpg|gif)$/,
            exclude: /node_modules/,
            use: {
              loader: 'file-loader',
              options: {
                name: function(fullPath) {
                  return path.relative(__dirname + '/src', fullPath)
                }
              }
            }
          }
    
        ]
      }
      // ...
    }
    

    【讨论】:

    • 所有图标都是png 格式...这是问题所在,我还有png 格式的其他图像
    • @Legends 你如何分辨什么是图标,什么不是?
    • 我有一个src/images/icons 文件夹,其中所有图标和图像都放在src/images 中。在我的标记中,我会使用相应的网址:<link href="/dist/images/icons/xxx.png">
    • 我猜test: 检查资源路径,也许可以修改正则表达式以包含icons 文件夹?这会是一种有效的方法吗?
    • 我已经更新了答案:执行此操作的标准方法是在规则中添加 exclude: /node_modules/ 键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多