Same modules in several apps

In notebook:
Egghead Webpack
Created at:
2016-06-19
Updated:
2016-06-19
Tags:
Webpack libraries JavaScript
Two independent apps use the same helpers module. Let’s separate and bundle this file so that the browser only has to load and cache this file once. 

CommonChunksPlugin

CommonChunksPlugin is the answer. Same workflow as the previous example:
  1. require webpack in webpack.config.js
  2. add ​plugins:[]
  3. add ​new webpak.optimize.CommonsChunkPlugin
  4. configure the plugin: ​name: ‘common’, filename: ‘bundle.common.js’
  5. The above is enough for webpack to create the separate bundle
  6. again, ​webpackJsonp is not defined​ error
  7. add the created bundle file (bundle.common.js) to both pages that reference it
  8. Modify setup for the Karma test
  9. Don’t use this plugin for the test environment
  10. Conditionally load the plugin if ​env.test​ is false
  11. Use ​undefined​ if in test environment
  12. Filter out ​undefined​ plugin from loading ​.filter(p => !!p)​