Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exclude nested node_module folders from a 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 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
like image 524
Flion Avatar asked Jul 28 '15 11:07

Flion


2 Answers

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.

like image 104
Juho Vepsäläinen Avatar answered Sep 22 '22 09:09

Juho Vepsäläinen


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.

like image 21
gauche-droite Avatar answered Sep 19 '22 09:09

gauche-droite