Search code examples
javascriptnode.jsregexwebpackwebpack-dev-server

Webpack watchOptions.ignored – exclude certain directories from being ignored


For webpack's watch feature, watchOptions.ignored allows me to specify anymatch patterns to ignore, like this:

module.exports = {
  //...
  watchOptions: {
    ignored: ['files/**/*.js', 'node_modules']
  }
};

Is it possible to use an anymatch or regex pattern to also specify subfolders of node_modules to not ignore?

In this situation, I need to ignore everything in node_modules except for all subfolders of node_modules that start with "abc-".


Solution

  • I was trying to exclude all folders that do not have /src/. I tried negative on glob pattern, which did not work.

    watchOptions.ignored

    RegExp string [string]

    The document says it supports string and [string] where string can be glob patterns.

    When using glob patterns, we convert them to regular expressions with glob-to-regexp

    Since they use glob-to-regexp internally, I think the negative match does not work as expected https://github.com/fitzgen/glob-to-regexp/issues/15


    Since the document says we can use RegExp too. I tried following two, and both worked!

    watchOptions: {
        ignored: new RegExp('.*^((?!(/src/)).)*$'),
    },
    

    and

    watchOptions: {
        ignored: '.*^((?!(/src/)).)*$',
    },
    

    I don't think it supports arrays having RegExp (for multiple cases we can use regex or).

    If you use regex, you will be able to achieve the case mentioned in this question.

    Reference: