Webpack排除特定文件

时间:2017-06-05 19:48:19

标签: javascript reactjs webpack webpack-2

我有这个代码在我的webpack.config.prod.js中我想知道如何排除除src/configs/configs

等特定路径中的所有json之外的所有json
exclude: [
  /\.html$/,
  /\.(js|jsx)$/,
  /\.css$/,
  /\.json$/,
  /\.bmp$/,
  /\.gif$/,
  /\.jpe?g$/,
  /\.png$/,
],
loader: require.resolve('file-loader'),
options: {
  name: 'static/media/[name].[hash:8].[ext]',
}

...

3 个答案:

答案 0 :(得分:12)

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

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

答案 1 :(得分:5)

要进行排除工作,我必须在我想要排除的特定文件中转义点。以下是从一般规则中排除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]',
    },
  },

答案 2 :(得分:0)

对于 Webpack 5:

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