【问题标题】:Webpack Exclude a specific fileWebpack 排除特定文件
【发布时间】:2017-11-06 16:16:52
【问题描述】:

我的webpack.config.prod.js 中有这段代码,我想知道如何排除所有 json,除了特定路径中的一个,如src/configs/configs

exclude: [
  /\.html$/,
  /\.(js|jsx)$/,
  /\.css$/,
  /\.json$/,
  /\.bmp$/,
  /\.gif$/,
  /\.jpe?g$/,
  /\.png$/,
],
loader: require.resolve('file-loader'),
options: {
  name: 'static/media/[name].[hash:8].[ext]',
}

...

【问题讨论】:

标签: javascript reactjs webpack webpack-2


【解决方案1】:

根据Webpack documentation,你可以这样做。

exclude: {
  test: [
    /\.html$/,
    /\.(js|jsx)$/,
    /\.css$/,
    /\.json$/,
    /\.bmp$/,
    /\.gif$/,
    /\.jpe?g$/,
    /\.png$/,
  ],
  exclude: [
    'src/configs/configs/your.json'
  ]
}

【讨论】:

    【解决方案2】:

    为了使排除工作,我必须转义我要排除的特定文件中的点。下面是从一般规则中排除 favicon.ico 并为其添加特殊规则的示例:

      {
        test: /\.(ico|jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
        exclude: /favicon\.ico$/,
        loader: 'file-loader',
        options: {
          name: 'static/media/[name].[hash:8].[ext]',
        },
      },
      // A special rule for favicon.ico to place it into build root directory.
      {
        test: /favicon\.ico$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash:8]',
        },
      },
    

    【讨论】:

      【解决方案3】:

      对于 Webpack 5:

        {
          test: /\.(png|svg|jpg|jpeg|gif)$/,
          type: 'asset/resource',
        },
        {
          test: /favicon\.ico$/,
          type: 'asset/resource',
          generator: {
            filename: '[name][ext]',
          },
        },
      

      【讨论】:

      • 无排除选项
      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 2018-07-12
      • 2020-05-05
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多