Understanding the Concept of Treeshaking in JavaScript

SHUBHAM GAUTAM
4 min readFeb 7, 2023

--

Photo by Tracy Adams on Unsplash

Treeshaking is a technique used in modern JavaScript builds that allows developers to remove unused code from their applications. The main goal of treeshaking is to reduce the size of the final JavaScript bundle, making it faster to download and run, especially on mobile devices.

In this article, we will explore the concept of treeshaking, including its benefits and how it works. We’ll also go over a basic configuration for setting up treeshaking in a JavaScript project.

Benefits of Treeshaking:

  • Reduced file size and faster download speeds
  • Improved performance and load times
  • Optimized code that is easier to maintain

How Treeshaking Works:

  • Treeshaking relies on the static structure of JavaScript code to determine which code is used and which is not
  • During the build process, the bundler analyzes the code and removes any unused exports
  • The final bundle contains only the code that is actually used in the application, without any unused code or dependencies

Setting up Treeshaking in a JavaScript Project: -Use a modern JavaScript bundler that supports treeshaking, such as Webpack or Rollup -Mark unused code as exports, using the export keyword in JavaScript -Configure the bundler to use treeshaking by specifying the optimization option in the configuration file

Example Webpack Configuration for Treeshaking:

module.exports = {
mode: "production",
optimization: {
usedExports: true
}
};

The example Webpack configuration for treeshaking is a simple module.exports object that defines the settings for the Webpack build process. It has two properties:

  1. mode: Specifies the mode for the build process. The production mode enables optimizations for production environments.
  2. optimization: An object that contains configuration options for Webpack's optimization process. The only option specified in this example is usedExports.

The usedExports option is set to true to enable treeshaking. This tells Webpack to analyze the code during the build process and remove any exports that are not actually used in the application.

It’s important to note that Webpack will only remove exports that are unused. If an export is used, even if it’s not imported directly, it will not be removed by treeshaking. To take advantage of treeshaking, it’s important to structure your code in a way that makes it clear which exports are used and which are not.

This is a basic example of a Webpack configuration for treeshaking, but there are other options and settings that can be configured to customize the build process further. It’s worth taking the time to understand all of the options available in the Webpack configuration so that you can optimize your build process for your specific needs.

Here’s a more advanced example of a Webpack configuration for treeshaking:

const path = require("path");
const webpack = require("webpack");

module.exports = {
mode: "production",
entry: {
main: "./src/index.js"
},
output: {
path: path.resolve(__dirname, "dist"),
filename: "[name].bundle.js"
},
optimization: {
usedExports: true,
splitChunks: {
chunks: "all",
minSize: 30000,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 5,
maxInitialRequests: 3,
automaticNameDelimiter: '~',
name: true,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
}
},
plugins: [
new webpack.HashedModuleIdsPlugin({
hashFunction: "sha256",
hashDigest: "hex",
hashDigestLength: 20
})
]
};

In this example, the configuration has a few additional options and settings:

  1. entry: An object that defines the entry points for the application. In this example, there is only one entry point: ./src/index.js.
  2. output: An object that defines the output configuration for the build process. The path property specifies the directory where the output file will be placed, and the filename property defines the name of the output file.
  3. splitChunks: An object that specifies how code should be split into multiple chunks during the build process. This can improve the performance of the application by allowing different parts of the code to be loaded on demand, instead of loading everything at once.
  4. plugins: An array of plugins to use in the build process. In this example, the HashedModuleIdsPlugin is included to help prevent issues with caching.

In this configuration, we have set the usedExports option to true, which enables treeshaking. The splitChunks option is also set to split code into smaller chunks, making the application faster and more efficient. With these settings, Webpack will analyze the code during the build process, remove any unused exports, and split the code into smaller chunks, creating a more optimized and efficient application.

In conclusion, treeshaking is an important technique for optimizing modern JavaScript applications. By removing unused code, developers can reduce the size of their applications and improve performance, making their applications faster and more efficient. If you’re not already using treeshaking in your projects, consider adding it to your workflow today.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Happy Coding !!!

--

--

No responses yet