In this essay we’ll be using the DllPlugin and DllReferencePlugin in a sample project and find out how it works to “drastically improve build time performance”.
DllPlugin
This plugin is designed to optimize the build performance by pre-compiling certain libraries and frameworks into a separate bundle. It is particularly beneficial for large projects that rely heavily on stable third-party dependecies which do not change often.
The DllPlugin creates two things:
A manifest.json file - It contains mappings from import requests to module ids, it is used by the DllReferencePlugin.
A bundle of modules that are not frequently changed
Note: This plugin is used in a sperate webpack configuration file to create a dll-only-bundle.
DllReferencePlugin
This plugin is used to reference the dll-only-bundle(s) to require pre-built dependencies, and it used in the primary webpack configuration file.
Example
We are going to create a basic webpage that uses lodash and React.
WARNING in configuration The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack 5.93.0 compiled with 1 warning in 2290 ms
At the point, the size of main.js is 205Kb.
Step 1. Create webpack.vendor.config.js
This configuration file is used to create the DLL bundle.
WARNING in configuration The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack 5.93.0 compiled with 1 warning in 1682 ms
In our case, the file vendor.dll.js is created with the size of 205Kb.
Step 2: Reference the DLL in main webpack configuration
Modify the main Webpack configuration to use the DllReferencePlugin.
WARNING in configuration The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment. You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack 5.93.0 compiled with 1 warning in 623 ms
We can see the size of main.js has been dramatically decreased from 205Kb to 249 bytes, also the compilation time was much shorter.