javascriptwebpackexternal-dependencies

How to bundle vendor scripts separately and require them as needed with Webpack?


I'm trying to do something that I believe should be possible, but I really can't understand how to do it just from the webpack documentation.

I am writing a JavaScript library with several modules that may or not depend on each other. On top of that, jQuery is used by all modules and some of them may need jQuery plugins. This library will then be used on several different websites which may require some or all modules.

Defining the dependencies between my modules was very easy, but defining their third-party dependencies seems to be harder then I expected.

What I would like to achieve: for each app I want to have two bundle files one with the necessary third-party dependencies and other with the necessary modules from my library.

Example: Let's imagine that my library has the following modules:

And I have an app (see it as a unique entry file) that requires modules a, b and c. Webpack for this case should generate the following files:

In the end, I would prefer to have jQuery as a global so I don't need to require it on every single file (I could require it only on the main file, for example). And jQuery plugins would just extend the $ global in case they are required (it is not a problem if they are available to other modules that don't need them).

Assuming this is possible, what would be an example of a webpack configuration file for this case? I tried several combinations of loaders, externals, and plugins on my configuration file, but I don't really get what they are doing and which ones should I use. Thank you!


Solution

  • in my webpack.config.js (Version 1,2,3) file, I have

    function isExternal(module) {
      var context = module.context;
    
      if (typeof context !== 'string') {
        return false;
      }
    
      return context.indexOf('node_modules') !== -1;
    }
    

    in my plugins array

    plugins: [
      new CommonsChunkPlugin({
        name: 'vendors',
        minChunks: function(module) {
          return isExternal(module);
        }
      }),
      // Other plugins
    ]
    

    Now I have a file that only adds 3rd party libs to one file as required.

    If you want get more granular where you separate your vendors and entry point files:

    plugins: [
      new CommonsChunkPlugin({
        name: 'common',
        minChunks: function(module, count) {
          return !isExternal(module) && count >= 2; // adjustable
        }
      }),
      new CommonsChunkPlugin({
        name: 'vendors',
        chunks: ['common'],
        // or if you have an key value object for your entries
        // chunks: Object.keys(entry).concat('common')
        minChunks: function(module) {
          return isExternal(module);
        }
      })
    ]
    

    Note that the order of the plugins matters a lot.

    Also, this is going to change in version 4. When that's official, I update this answer.

    Update: indexOf search change for windows users