首页 > 解决方案 > 正则表达式从 node_modules 中排除文件夹

问题描述

我目前正在使用 webpack 并运行一个通过 node_modules 并排除一个特定模块的测试。

但是,当我在正则表达式检查器上尝试它时,我正在努力使用正则表达式,它工作正常。但是,在 webpack 上它失败了。

示例https ://regex101.com/r/rO4jD0/34

^node_modules\/(?!example).*

在 webpack 本身上,我正在写以下内容:

vendor: {
    test: /^[\\/]node_modules[\\/](?!example).*[\\/]/,
    // test: /^[\\/]node_modules[\\/](?!example)[\\/].*/,
    // test: /^node_modules\/(?!example).*/,
    name: 'vendor',
    chunks: 'all',
}

这些测试是我迄今为止运行的测试的示例,但不幸的是它们都失败了。

作为一个仅供参考- 这工作正常,但它排除一个示例文件夹。

test: /[\\/]node_modules[\\/]/,

标签: regexwebpack

解决方案


这是要将大型供应商模块拆分为自己的捆绑包吗?我是这样做的:

vendor: {
  name: 'vendor',
  test: /[\\/]node_modules[\\/]((?!(example.js|slick-carousel)).*)[\\/]/,
  name: 'vendors',
  chunks: 'all'
},
v_example: {
  test: /[\\/]node_modules[\\/]((example.js).*)[\\/]/,
  name: 'v_example',
  chunks: 'all'
},
v_slick: {
  test: /[\\/]node_modules[\\/]((slick-carousel).*)[\\/]/,
  name: 'v_slick',
  chunks: 'all'
}

推荐阅读