Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack how to exclude node_modules except a folder

Tags:

webpack

I'm using webpack 4 and I have installed a module with a lot of subfolders, the structure would be:

/node_modules/my_main_package/what_i_need_to_include

I need to exclude the whole node_modules except what_i_need_to_include. Here is what I tried in my webpack.base.babel so far:

The first approach:

module: {
    rules: [
      {
        test: /\.js$/, 
        exclude: '/node_modules/',
        include: '/node_modules/my_main_package/what_i_need_to_include',
use: {
          loader: 'babel-loader',
          options: options.babelQuery,
        },
      },
...

The second approach:

{
  test: /\.js$/, 
  exclude: '/node_modules\/(?!my_main_package/what_i_need_to_include).*/'
   ...
}

both times unsuccessfully.

How can I get this working? What am I doing wrong?

like image 251
assembler Avatar asked Dec 07 '18 16:12

assembler


People also ask

Does Webpack include node_modules?

Webpack builds a dependency graph used internally Now all modules that are used in your app are included in the dependency graph. Your project have many installed dependencies in the node_modules folder that should not be included in your client-side JavaScript production bundle.

What is exclude in Webpack?

Actually, those 'include' and 'exclude' properties are telling the loaders whether to include/exclude the files described (such as the contents of node_modules ), not webpack itself.

What is node Webpack?

Webpack is a static module bundler for JavaScript applications. It takes modules, whether that's a custom file that we created or something that was installed through NPM, and converts these modules to static assets.


1 Answers

I have to thank Mr. The Bear, he gave me a link with a working solution, and I will answer my question myself for sharing a solution, since very few people read the comments and this may probably would be a question for others. here is the solution:

module: {
    rules: [
      {
        test: /\.js$/, 
        exclude: /node_modules\/(?!(my_main_package\/what_i_need_to_include)\/).*/,
use: {
          loader: 'babel-loader',
          options: options.babelQuery,
        },
      },
...
}

Hope it might be helpful.

like image 123
assembler Avatar answered Sep 27 '22 20:09

assembler