Webpack 加载程序中的 RegEx 排除/包含配置不起作用 [英] RegEx in Webpack loader exclude/include config doesn't work

查看:35
本文介绍了Webpack 加载程序中的 RegEx 排除/包含配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 babel-loader 应用于特定模式的模块,比如 foo.*,在 node_modules 下,除了我的应用程序.

I am trying to apply babel-loader to a specific pattern of modules, say foo.*, under node_modules in addition to my app.

node_modules 下的其他模块应该像往常一样跳过.首先,我尝试使用负面预测不起作用:

Other modules under node_modules should be skipped as usual. First I tried to use negative look-ahead didn't work:

{
  test: /\.js$/,
  exclude: [
    /node_modules\/(?!foo).*/
  ],
  loader: 'babel-loader',
  query: {
    ...
  }
}, 

然后我分成了两个加载器,也没有工作:

Then I splited to two loaders and didn't work either:

{
  test: /\.js$/,
  exclude: [
    path.resolve(_path, "node_modules")
  ],
  loader: 'babel-loader',
  query: {
    ...
  }
}, 
{
  test: /\.js$/,
  include: [
    /node_modules\/foo.*/
  ],
  loader: 'babel-loader',
  query: {
    ...
  }
}

但是,如果我使用 string 而不是 RegEx,它适用于一个文件夹:

However, if I use string instead of RegEx, it works for one folder:

include: [
  path.resolve(_path, "node_modules/foo")
],

这向我表明 Regex 没有按预期工作.

This indicates to me the Regex is not working as expected.

有人在 Include/Exclude 字段中使用 RegEx 吗?

Has anyone got RegEx working in Include/Exclude field?

推荐答案

原来这是我正在使用的 Windows 中的路径分隔符问题.以下否定前瞻语法有效并且与操作系统无关:

Turns out it's a matter of path separator in Windows, which I am using. The following negative lookahead syntax works and is OS agnostic:

new RegExp('node_modules\\'+path.sep+'(?!foo).*')

这篇关于Webpack 加载程序中的 RegEx 排除/包含配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆