17

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

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: {
    ...
  }
}

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

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

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

Has anyone got RegEx working in Include/Exclude field?

1 Answer 1

21

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).*')
1
  • 1
    Another option would be to use an OR for os specific path separator: ... exclude: [ /node_modules(?!(\/|\\)foo)/ ] ...
    – david00f
    Apr 20, 2021 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.