简体   繁体   English

Webpack 排除特定文件

[英]Webpack Exclude a specific file

I have this code In my webpack.config.prod.js and I was wondering how do I exclude all json except one in a specific path like src/configs/configs我在我的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]',
}

...

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'
  ]
}

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]',
    },
  },

For Webpack 5:对于 Webpack 5:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM