How to exclude subfolders node_module from loader in webpack

Will the following exclude: '/node_modules/'

only exclude the node_modules folder in the same directory, or will it also exclude the subfolders of node_module? If not: how to configure it to exclude all node_module folders?

module: {
    loaders: [
        { test: /\.ts$/, loader: 'ts-loader',exclude: '/node_modules/' } 
    ]
},

      

Filestructure:

/general
/node_modules
/mod1
     /src
     /node_modules
/mod2
     /src
     /node_modules

      

+3


source to share


2 answers


The documentation says that

The condition can be RegExp, a string containing an absolute path, a function (absPath): bool, or an array of one of them combined with "and".

So, based on this, you can try to create a RegExp according to your case.

Personally, I prefer to use include

over exclude

as whitelisting is easier to maintain than blacklisting. At least you have stronger control this way.



Following this thought, you can skip the problem altogether exclude

and create a rule like this:

include: [
    path.resolve(__dirname, 'general'),
    path.resolve(__dirname, 'mod1/src'),
    path.resolve(__dirname, 'mod2/src')
]

      

Of course, if you have a more complex structure, you may end up with a combination of both. One rule: exclude

node_modules and one rule for the include

directories you want to browse.

+11


source


If your problem is only with TypeScript files, you can take advantage of the fact that 'ts-loader'

your file will be used tsconfig.json

when compiling. Instead, you can define the paths you want to exclude.



+1


source







All Articles