<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Himanshu Gupta</title>
    <description>The latest articles on DEV Community by Himanshu Gupta (@himanshugupta714).</description>
    <link>https://dev.to/himanshugupta714</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F999505%2F5e7a7e26-8947-4cfc-800b-8953e300cd3d.jpg</url>
      <title>DEV Community: Himanshu Gupta</title>
      <link>https://dev.to/himanshugupta714</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/himanshugupta714"/>
    <language>en</language>
    <item>
      <title>Optimizing Performance with the SplitChunksPlugin in Webpack</title>
      <dc:creator>Himanshu Gupta</dc:creator>
      <pubDate>Sat, 31 Dec 2022 11:07:41 +0000</pubDate>
      <link>https://dev.to/himanshugupta714/optimizing-performance-with-the-splitchunksplugin-in-webpack-3e72</link>
      <guid>https://dev.to/himanshugupta714/optimizing-performance-with-the-splitchunksplugin-in-webpack-3e72</guid>
      <description>&lt;p&gt;Webpack is a powerful tool for building and bundling frontend applications, but as your app grows, the size of your bundle can also increase. This can lead to slower load times and a less optimal user experience.&lt;/p&gt;

&lt;p&gt;One way to improve the performance of your app is to split your bundle into smaller chunks, which can be loaded on demand as the user navigates through your app. This is especially useful if you have a large app with many dependencies, as it can reduce the time it takes for the initial bundle to load.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll look at how to use the SplitChunksPlugin in Webpack to split your bundle into smaller chunks based on size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Webpack
&lt;/h2&gt;

&lt;p&gt;Before we get started, make sure you have the following tools installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Node.js and npm (the Node Package Manager)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To set up Webpack, create a new directory for your project and run the following command to initialize a package.json file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, install Webpack and its dependencies by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev webpack webpack-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring the SplitChunksPlugin
&lt;/h2&gt;

&lt;p&gt;To split your bundle into smaller chunks based on size, you'll need to use the SplitChunksPlugin. First, install the plugin by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev webpack-split-chunks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, update your Webpack configuration to include the SplitChunksPlugin, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
const webpack = require('webpack');

module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    splitChunks: {
      maxSize: 250000,
      chunks: 'all'
    }
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration will split your bundle into multiple smaller bundles if any of them are larger than 250KB in size.&lt;/p&gt;

&lt;p&gt;You can also specify specific dependencies to be split into their own bundles by using the &lt;code&gt;cacheGroups&lt;/code&gt; option in the SplitChunksPlugin configuration. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;optimization: {
  splitChunks: {
    cacheGroups: {
      vendor: {
        test: /[\\/]node_modules[\\/]/,
        name: 'vendor',
        chunks: 'all',
        maxSize: 250000
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will extract all dependencies in the node_modules directory into a separate bundle called &lt;code&gt;vendor.bundle.js&lt;/code&gt; if the bundle is larger than 250KB in size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Webpack
&lt;/h2&gt;

&lt;p&gt;To run Webpack with the SplitChunksPlugin configuration, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx webpack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the Split Chunks
&lt;/h2&gt;

&lt;p&gt;To use the split chunks in your app, you'll need to include them in your HTML file. You can do this by using the &lt;code&gt;script&lt;/code&gt;&lt;br&gt;
 tag and specifying the path to the chunk file.&lt;/p&gt;

&lt;p&gt;For example, if you have a chunk called &lt;code&gt;vendor.bundle.js&lt;/code&gt;&lt;br&gt;
that contains all your vendor dependencies, you can include it in your HTML file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="dist/vendor.bundle.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to include the vendor chunk before the main bundle, as it will be needed by the main bundle.&lt;/p&gt;

&lt;p&gt;You can also use the script tag to asynchronously load the split chunks when they are needed. To do this, you can use the async attribute in the script tag, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="dist/vendor.bundle.js" async&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will allow the browser to load the vendor chunk asynchronously, without blocking the rendering of the page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Splitting your bundle into smaller chunks can improve the performance of your app and provide a better user experience. By using the SplitChunksPlugin in Webpack, you can easily split your bundle based on size or specific dependencies.&lt;/p&gt;

&lt;p&gt;I hope this tutorial has helped you understand how to use the SplitChunksPlugin in Webpack. If you have any questions or comments, please feel free to leave them in the comments below.&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
