how to exclude nested node_module folders from a loader in webpack
Asked Answered
S

2

7

Will the following exclude: '/node_modules/' only exclude the node_modules folder in the same directory, or will it also exclude the nested node_module folders? If not: how can I adjust it to exclude all the 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
Sarraceniaceous answered 28/7, 2015 at 11:30 Comment(0)
L
14

The documentation says that

A condition may be a RegExp, a string containing the absolute path, a function(absPath): bool, or an array of one of these combined with “and”.

So based on that you could try to build a RegExp to fit your case.

Personally I prefer to use include over exclude as a whitelist is easier to maintain than a blacklist. At least you have stronger control this way.

Following this line of thought you could skip exclude problem altogether and build a rule like this:

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

Of course if you have more complicated structure maybe you'll end up doing a combination of both. One rule to exclude node_modules and one rule to include the directories you want to look into.

Leaves answered 28/7, 2015 at 19:45 Comment(1)
This is the example the documentation was missing. Awkwardly, neither DevDocs nor the official documentation made it straightforward to search for include usage. Thanks for this example using an Array.Engaged
Y
1

If your issue is only with TypeScript files, you can take advantage of the fact that 'ts-loader' will use your tsconfig.json file upon compiling. You can define paths you want to exclude right there instead.

Yakutsk answered 28/11, 2015 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.