Splitting vendor chunk with Vite and loading them async
Tags: performance, web, vite, frontend
Published: 2023-08-07 21:15:44

How have your website chunks been? Does it need a diet?

Let's take a look at how to split the vendor chunk and reduce its size. Allowing the browsers to loading smaller chunks and loading them async. I'll use a [personal project](https://frontend-pattern.vercel.app/) to show it. The stack is as follows: 

- [React](https://react.dev/) (SPA);
- [Vite](https://vitejs.dev/);
- [Rollup Plugin Visualizer](https://www.npmjs.com/package/rollup-plugin-visualizer);

The same applies to any other js frameworks with Vite. It might applicable with Webpack too, consult the doc to know how.

[See my other articles](https://frontend-pattern.vercel.app/articles) 

## What are chunks at development word?

Usually, front-end developers have contact with .ts, .vue, and .tsx files, but browsers don't understand them. To do so, it needs to convert those files into .js files - and they can even be optimized, typically by using a build command. 

Even though all development files can be converted into a single .js file, it's not recommended because of the performance impact. Ideally, those files a converted into multiple .js files (chunks) to take the ability to loading the files at the moment they are required - thinking on user navigation.

So, chunks are .js files that, together, define a whole front-end system, in a nutshell. 


## Current project bundle

To start, let's see how the project looks like:

![A huge index.js file with all third party](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nuyd8rngap0n5dsyke8e.gif)

As you can see, there are some js files in the project. let's focus on the index._hash_.js.

Usually, tools such as Vite and Wepack create two main chunk files: index._hash_.js and vendor._hash_.js: 

- index: there are crucial application codes, such as App.tsx code;
- vendor: All production dependencies (package.json > dependencies);

Wait. Why isn't the vendor chunk at the gif? [Vite has removed this behavior since the 2.9 version](https://vitejs.dev/guide/build.html#chunking-strategy). Now, by default, the index also has the production dependencies code.

By using  Rollup Plugin Visualizer, which gives us an interactive graph of the project bundle, it's easier to see:

![Bundle screenshot with no vendor js file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia4zu1z7xg1n8ad2layo.png)

As you can see, there is a huge index chunk.

The first thing we can do is to split core content from third-party content, which allows the browser to download both files async by adding the [modulepreload attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/modulepreload). 

But before move on, see the current index size (255.35 kb):


![current index size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agfd65acxl83lzoqguxk.png)

## Split index chunk into two chunks and load them async

Even though Vite doesn't split them by default, it provides a plugin to do it for you:

```TS
import { splitVendorChunkPlugin } from 'vite';

export default defineConfig({
  plugins: [
    react(),
    splitVendorChunkPlugin()
]})
```

Vite does the rest for you. 

![With index and vendor chunks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7i22866aey7xpkcbva9.png)

Vendor size: 239.95kb
Index size: 15.90kb

Well, it's possible to see how third-party packages impact the bundle of this project.

---

_Enjoying it? If so, don't forget to give a_ ❤️_ and follow me to keep updated. Then, I'll continue creating more content like this_

---

## Split vendor chunk into more chunks

As we figured out who is the villain, let's try to split the vendor chunk into smaller chunks. 

To do so, we gonna use the [manualChunk function](https://rollupjs.org/configuration-options/#output-manualchunks), provided by Rollup. But before it, we must decide who we'll remove from the main vendor chunk(?) and that's the trick question... looking into our bundle again, we find some huge (comparing with other) packages, like react-dom, but to this article, I will create two other chunks from vendor chunk: 

- @open-ish
- @react-router

Why? Well, when we have a vendor chunk, it might have dependencies that must be loaded in the right order, as some of them might use others, otherwise, your application might not work. This is the hardest part of creating chunks manually: **knowing who must be in the same chunk and it depends on the application you have been working on**. For my project, I know that those dependencies must be in the same chunk: 'react-router-dom', '@remix-run', and 'react-router'. 

So let's see the code to split them: 

```TS
import { splitVendorChunkPlugin } from 'vite';

export default defineConfig({
  plugins: [
    react(),
    splitVendorChunkPlugin()
],
 build: {
    rollupOptions: {
      output: {
        manualChunks(id: string) {
          // creating a chunk to @open-ish deps. Reducing the vendor chunk size
          if (id.includes('@open-ish') || id.includes('tslib')) {
            return '@open-ish';
          }
          // creating a chunk to react routes deps. Reducing the vendor chunk size
          if (
            id.includes('react-router-dom') ||
            id.includes('@remix-run') ||
            id.includes('react-router')
          ) {
            return '@react-router';
          }
        },
      },
    },
  },
})
```

With this code, Rollup will create two new chunks: 

![Bundle with two new chunks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usj6djpa83lyhep0gvyd.png)

And now, comparing the vendor chunk size: 

![build after use manual chunk funcion](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/on8juu6o2o8uxra4qe3o.png)

Vendor size: 211.94kb

As we can see, we reduce the biggest chunk from 255.35kb to 211.94kb (**~18%**). That might not sound too much, but it is just the beginning of splitting out the chunks and creating smaller ones and even given the possibility of loading them async to the browser.

See now how our application loads (focus on the waterfall):

![final bundle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42mrj8zgmexvtdqubzw8.gif)
 
They start to load at the same time, did you see?

> [Buy me a coffee](https://ko-fi.com/tjordao) ☕. Hope I have helped you somehow. 🤗

## Conclusion

In this article, you saw a bit more about chunks, and how to split them and load them async using Vite/Rollup.

Even though the code itself is simple (thanks to Vite/Rollup), the hardest is to identify which dependencies must be in the same chunk. 

I might create a second part of this content splitting the vendor chunk more, later.

- [Source Code here](https://github.com/tassioFront/frontend-pattern)
- [The commit with those changes](https://github.com/tassioFront/frontend-pattern/commit/aa2bf43a4ac42e2fb02319fcaa2c5b0ea4caada2)

[See my other articles](https://frontend-pattern.vercel.app/articles)