Anakin Anakin - 2 months ago 25
Javascript Question

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 (which in my opinion is a hell to decipher and understand).

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 website 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:


  • a (requires: jquery, jquery.plugin1)

  • b (requires: jquery, a)

  • c (requires: jquery, jquery.ui, a, b)

  • d (requires: jquery, jquery.plugin2, a)



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


  • vendor bundle: with jquery, jquery.plugin1 and jquery.ui;

  • website bundle: with modules a, b and c;



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!

Answer

in my webpack.config.js file, I have

function isExternal(module) {
  var userRequest = module.userRequest;

  if (typeof userRequest !== 'string') {
    return false;
  }

  return userRequest.indexOf('/bower_components/') >= 0 ||
         userRequest.indexOf('/node_modules/') >= 0 ||
         userRequest.indexOf('/libraries/') >= 0;
}

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 cond
    }
  }),
  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.

Comments