Although code splitting gives control over when code is loaded, it's not the only way webpack lets you shape the output.
Bundle splitting
is a complementary technique that lets you define splitting behavior on the level of configuration. A common use case is extracting so called vendor bundle that contains third-party dependencies.
The split allows the client to download only the application bundle if there are changes only in the application code. The same goes for vendor-only changes.
To give you a quick example, instead of having
main.js
(100 kB), you could end up with
main.js
(10 kB) and
vendor.js
(90 kB). Now changes made to the application are cheap for the clients that have already used the application earlier.
It's possible to get good caching behavior with these plugins if a webpack records are used. The idea is discussed in detail in the Adding Hashes to Filenames chapter.
webpack.optimize contains LimitChunkCountPlugin and MinChunkSizePlugin which give further control over chunk size.
In the example above, you used different types of webpack chunks. Webpack treats chunks in three types:
Entry chunks contain webpack runtime and modules it then loads.
Normal chunksdon't contain webpack runtime. Instead, these can be loaded dynamically while the application is running. A suitable wrapper (JSONP for example) is generated for these. You generate a normal chunk in the next chapter as you set up code splitting.
Initial chunks are normal chunks that count towards initial loading time of the application. As a user, you don't have to care about these. It's the split between entry chunks and normal chunks that is important.
The situation is better now compared to the earlier. Note how small main bundle compared to the vendor bundle. To benefit from this split, you set up caching in the next part of this book in the Adding Hashes to Filenames chapter.
To recap:
Webpack allows you to split bundles from configuration entries through the optimization.splitChunks.cacheGroups field. It performs bundle splitting by default in production mode as well.
A vendor bundle contains the third-party code of your project. The vendor dependencies can be detected by inspecting where the modules are imported.
Webpack offers more control over chunking through specific plugins, such as AggressiveSplittingPlugin and AggressiveMergingPlugin. Mainly the splitting plugin can be handy in HTTP/2 oriented setups.
Internally webpack relies on three chunk types: entry, normal, and initial chunks.