Javascript Webpack 排除特定文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44376589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:29:20  来源:igfitidea点击:

Webpack Exclude a specific file

javascriptreactjswebpackwebpack-2

提问by Lee Han Kyeol

I have this code In my webpack.config.prod.jsand I was wondering how do I exclude all json except one in a specific path like src/configs/configs

我有这段代码webpack.config.prod.js,我想知道如何排除除特定路径中的所有 json 之外的所有 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]',
}

...

回答by Lee Han Kyeol

According to the Webpack documentation, you can do something like this.

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

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

回答by Hamund

To make exclude work I had to escape the dot in the specific file I wanted to exclude. Here's an example of excluding favicon.ico from a general rule and adding a special rule for it:

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