Webpack 排除特定文件

IT技术 javascript reactjs webpack webpack-2
2021-04-02 09:39:22

我有这段代码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]',
}

...
3个回答

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

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

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

对于 Webpack 5:

  {
    test: /\.(png|svg|jpg|jpeg|gif)$/,
    type: 'asset/resource',
  },
  {
    test: /favicon\.ico$/,
    type: 'asset/resource',
    generator: {
      filename: '[name][ext]',
    },
  },
没有排除选项
2021-05-29 09:39:22