日本語

React and Vite: Checking the Bundle Size of Build Output

​​Vite​lodash,React,rollup-plugin-visualizer,TypeScript

In a React and Vite app, npm run build (which runs tsc -b && vite build) prints the size of each output file (bundle size) and its gzip-compressed size at the end. This list shows the total of the files. To see which libraries take up the most space within them, you can use the rollup-plugin-visualizer plugin to draw a chart.

This article explains how to find out, with numbers, what is large and by how much, when the first page load is slow or JavaScript grows after you add one library.

In my test environment, I added lodash to the react-ts template from Vite, measured the size, and checked how a change in the import style made it smaller.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Vite 8.3.8, @vitejs/plugin-react 6.3.0, React 19.3.1, TypeScript 7.0.2, and rollup-plugin-visualizer 7.1.3. I created the project with the react-ts template from npm create vite@latest. I recorded the build output in a non-terminal environment (writing it to a log). When I tried different settings, I put them in a separate config file and passed it with vite build --config. I took the screenshots of the charts by opening them in Chromium from Playwright 1.63.0. I did not test Windows or macOS, and I did not test transfer sizes when serving the files from a real server.

Reading the summary that vite build prints at the end

When I ran npm run build on the unmodified template, the following summary was printed at the end.

text
vite v8.3.8 building client environment for production...
transforming...
✓ 20 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                   0.45 kB │ gzip:  0.29 kB
dist/assets/react-CHdo91hT.svg    4.12 kB │ gzip:  2.06 kB
dist/assets/vite-BF8QNONU.svg     8.70 kB │ gzip:  1.60 kB
dist/assets/hero-CLDdwZDr.png    13.05 kB
dist/assets/index-D64VDMd1.css    4.10 kB │ gzip:  1.47 kB
dist/assets/index-GpEN3EET.js   222.44 kB │ gzip: 69.28 kB

✓ built in 170ms

From left to right, the columns show the output file, its size as is, and its size after gzip compression. There is only one JS file, index-GpEN3EET.js.

When I checked this file with ls -l, it was 222,447 bytes. The kB values in the summary count 1,000 bytes as 1 kB.

The PNG line has no gzip column. The favicon.svg and icons.svg files in the template's public/ directory were copied to dist/, but they did not appear in the summary.

The gzip column is the value that Vite calculates. When I compressed the same JS file with the gzip command in the test environment, using default settings, the result was 68,541 bytes. That differs slightly from the 69.28 kB in the summary.

Checking how much a library adds

Next, I built a screen that counts the unique words in the text you enter, and I used lodash's uniq for it. I installed lodash and imported the whole library.

text
npm i lodash@4
npm i -D @types/lodash rollup-plugin-visualizer

I use rollup-plugin-visualizer on the second line later, to check what is inside the bundle.

tsx
// src/App.tsx
import { useState } from 'react'
import _ from 'lodash'

function App() {
  const [text, setText] = useState('')
  const words = _.uniq(text.split(/\s+/).filter(Boolean))

  return (
    <main>
      <h1>単語の数え上げ</h1>
      <textarea value={text} onChange={(e) => setText(e.target.value)} />
      <p>重なりを除いた単語:{words.length}個</p>
    </main>
  )
}

export default App

After the build, the JS file was 291.95 kB. The app no longer loads the template's image and App.css, so the image line disappeared and the CSS got smaller.

text
dist/index.html                   0.45 kB │ gzip:  0.29 kB
dist/assets/index-DGNrK5qb.css    1.78 kB │ gzip:  0.81 kB
dist/assets/index-BF_JFffr.js   291.95 kB │ gzip: 94.95 kB

The app uses only uniq, but the JS grew by about 70 kB from the template's 222.44 kB. However, this summary shows totals per file, so it does not tell you whether the increase comes from lodash.

Visualizing the contents with rollup-plugin-visualizer

You can check what takes up space inside a file with rollup-plugin-visualizer. If you add it as a Vite plugin, it writes a chart of the contents as HTML on every build. I used the package that I installed together with lodash.

ts
// vite.config.ts
import react from '@vitejs/plugin-react'
import { visualizer } from 'rollup-plugin-visualizer'
import { defineConfig } from 'vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    visualizer({
      filename: 'stats.html',
      gzipSize: true,
    }),
  ],
})

The README's Rollup example says to place this plugin last, so I put it at the end of plugins. With gzipSize: true, the chart also shows the gzipped size. After I ran npm run build with this setting, a stats.html file was created in the project root.

When I opened it in a browser, a treemap chart appeared by default. Larger files are drawn as wider rectangles.

A treemap where react-dom-client.production.js takes up most of the right side and lodash/lodash.js takes up a tall rectangle on the lower left
The treemap in stats.html. The tall rectangle on the lower left is lodash

The large rectangle on the right is react-dom, and the tall rectangle on the lower left is lodash. If you hover the mouse over a rectangle, you can see the file's size and which file imports it.

With the mouse hovering over lodash/lodash.js, the tooltip shows Rendered: 177.23KB, Gzip: 38.29KB, and Imported By: /src/App.tsx
What appears when hovering over the lodash rectangle

lodash is imported from /src/App.tsx, and Rendered shows 177.23KB.

The numbers in the chart do not match the build summary

Be careful when comparing the numbers in the chart with the numbers in the build summary. The chart shows 177.23KB for lodash alone, but the whole JS file is only 291.95 kB.

As the hover tooltip explains, Rendered is the size after transformation and tree shaking. It is measured before minification (the process that shortens whitespace and long names), so it is larger than the size inside the output file. Compared with the post-minify size shown later, react-dom-client.production.js is 517.13 KiB in the chart and 198.30 KiB after minification, a difference of more than 2x. Also, the KB in the chart is in units of 1,024 bytes. (The Markdown report shown later writes it as KiB.)

If you want to see sizes after minification, enable Vite's source maps and set the plugin's sourcemap option to true. The README says to place the plugin last when you use this option too.

ts
// vite.config.ts
export default defineConfig({
  build: {
    sourcemap: true,
  },
  plugins: [
    react(),
    visualizer({
      filename: 'stats-sourcemap.md',
      template: 'markdown',
      sourcemap: true,
    }),
  ],
})

Here I also set template: 'markdown' to write a report as a Markdown table instead of a chart. The total and the first three of the top 10 entries are shown below.

text
| Metric | Total |
| --- | ---: |
| Rendered | 283.51 KiB |

## Top 10

- `/node_modules/react-dom/cjs/react-dom-client.production.js`: 69.9% (198.30 KiB)
- `/node_modules/lodash/lodash.js`: 24.5% (69.57 KiB)
- `/node_modules/react/cjs/react.production.js`: 2.7% (7.63 KiB)

lodash is 69.57 KiB (about 71 kB), which is about a quarter of the whole JS file. The total of 283.51 KiB is also close to the 291.95 kB JS file measured in 1,024-byte units (about 285 KiB).

The Notes section of the generated Markdown report says that when sourcemap is enabled in the plugin, gzip and Brotli sizes are not calculated. In a later app that I switched to lodash-es, I also set gzipSize: true. In the report's settings table, gzip requested was yes and gzip enabled was no, and no gzip sizes were shown.

Making lodash smaller by changing how it is imported

There is another package called lodash-es, which provides lodash as ES modules (the npm description is "Lodash exported as ES modules."). I changed the code to import only uniq from it.

text
npm i lodash-es
npm i -D @types/lodash-es
tsx
// src/App.tsx (top part)
import { useState } from 'react'
import { uniq } from 'lodash-es'

function App() {
  const [text, setText] = useState('')
  const words = uniq(text.split(/\s+/).filter(Boolean))

In the build summary, the JS file dropped from 291.95 kB to 224.94 kB, a reduction of about 67 kB.

text
dist/index.html                   0.45 kB │ gzip:  0.29 kB
dist/assets/index-DGNrK5qb.css    1.78 kB │ gzip:  0.81 kB
dist/assets/index-Dnoaf6Yq.js   224.94 kB │ gzip: 70.63 kB

In the report generated with source maps, the lodash/lodash.js line was gone. Instead, several small files such as lodash-es/_baseUniq.js (360 B) appeared. Below are the first six of the top 10 entries.

text
- `/node_modules/react-dom/cjs/react-dom-client.production.js`: 90.6% (198.07 KiB)
- `/node_modules/react/cjs/react.production.js`: 3.5% (7.64 KiB)
- `/node_modules/react-dom/cjs/react-dom.production.js`: 1.6% (3.47 KiB)
- `/node_modules/scheduler/cjs/scheduler.production.js`: 1.6% (3.41 KiB)
- `/node_modules/lodash-es/_baseUniq.js`: 0.2% (360 B)
- `/node_modules/lodash-es/_baseIsNative.js`: 0.1% (328 B)

When you add a library or change how it is imported, you can narrow down the cause by first checking the change in the total in the build summary, and then using the visualizer to look inside.

Turning off gzip size computation

The gzip column in the build summary disappears when you set build.reportCompressedSize to false. The official documentation says that compressing large files takes time, so turning this off may speed up builds of large projects.

ts
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    reportCompressedSize: false,
  },
  plugins: [react()],
})

When I tried this in the app that uses lodash-es, the summary looked like this.

text
rendering chunks...
dist/index.html                   0.45 kB
dist/assets/index-DGNrK5qb.css    1.78 kB
dist/assets/index-Dnoaf6Yq.js   224.94 kB

The computing gzip size... line and the gzip column are gone.

The warning shown when a chunk exceeds 500 kB

If a single JS file (chunk) becomes too large, a warning appears at the end of the build. The limit is set by build.chunkSizeWarningLimit, and the default is 500 (kB). According to the official documentation, the comparison uses the size before compression.

The test app does not reach 500 kB, so I lowered the limit to 200 and triggered the warning with the 224.94 kB file.

ts
// vite.config.ts
export default defineConfig({
  build: {
    chunkSizeWarningLimit: 200,
  },
  plugins: [react()],
})

The last part of the build output was as follows.

text
dist/assets/index-Dnoaf6Yq.js   224.94 kB │ gzip: 70.63 kB

[plugin builtin:vite-reporter] 
(!) Some chunks are larger than 200 kB after minification. Consider:
- Using dynamic import() to code-split the application
- Use build.rolldownOptions.output.codeSplitting to improve chunking: https://rolldown.rs/reference/OutputOptions.codeSplitting
- Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.
✓ built in 238ms

Although the gzipped size of 70.63 kB is under the limit, the warning appears because the uncompressed 224.94 kB is over 200. The build did not stop and finished normally (exit code 0).

The warning suggests two approaches: splitting code with import(), and changing the chunking settings (build.rolldownOptions.output.codeSplitting). When you see this warning, first use the visualizer to check what is large. That makes it easier to decide where to split the code.

​Posted by 86