繁体   English   中英

Webpack 2:如何排除所有 node_modules 除了

[英]Webpack 2: How to exclude all node_modules except for

我需要在/node_modules/identicons/上运行 babel 但是我仍然想排除所有其他包。

原因是identicons 包正在使用模板字符串并在我运行时中断

"webpack -p"

有问题的字符串(node_modules/identicons/index.js):

str += `<rect x="${x}" y="${y}" width="${xside}" height="${xside}" style="fill:${color}" />`

webpack.config.babel

module: {
  rules: [
    {
      test: /\.jsx?$/,
      exclude: /node_modules/,
      //include: /node_modules/identicons/,
      use: ["babel-loader"]
    },

这种模式会怎么写?

我认为您可以使用正则表达式,例如

exclude: [
  /node_modules\/(?!identicons).*/
]

您可以从node_modules中排除所有不是identicons

exclude: /node_modules\/(?!identicons$)/

另一种方式:

exclude: [
  {
    test: [
      path.resolve(__dirname, './node_modules'),
    ],
    exclude: [
      path.resolve(__dirname, './node_modules/MODULE_TO_INCLUDE'),
      path.resolve(__dirname, './node_modules/ANOTHER_MODULE_TO_INCLUDE'),
    ]
  }
]

它对我有用。

排除整个node_modules文件夹,所需模块除外:

{
  test: /\.js$/,
  exclude: /node_modules\/(?!identicons\/).*/,
}

https://github.com/webpack/webpack/issues/2031#issuecomment-219040479

暂无
暂无

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

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