How to Fix Vite’s “Some chunks are larger than 500 kB” Warning
The message "Some chunks are larger than 500 kB after minification" at the end of vite build means one of the JS files (chunks) that Vite outputs was larger than 500 kB after minification, before gzip compression. If you import only the parts of a large library you use, or split the files with import() and codeSplitting, the warning goes away.
This is the situation where the warning started appearing every time I built after adding a library, or where the first page load became slow. In a test environment, I built a React app that uses highlight.js for syntax highlighting, triggered the warning, and then checked for each fix whether the warning went away and how the files loaded in the browser changed.
The test environment was Ubuntu 24.04, Node.js 22.23.5, Vite 8.3.8 (Rolldown 1.2.19), @vitejs/plugin-react 6.3.0, React 19.3.1, TypeScript 7.0.2, and highlight.js 11.12.0. I created the project from the react-ts template of npm create vite@latest. For the browser checks, I opened the page served by vite preview in headless Chromium from Playwright 1.63.0. I did not test Windows or macOS, Vite 7 or earlier (which use Rollup), or loading speed when served from a real server.
Reproducing the warning
The app is only a button that shows code wrapped in span elements with highlight.js classes for coloring. I did not load the theme CSS that adds colors, so the text on screen is all black. What I checked was whether the span elements were added. I split the display into CodeViewer.tsx and imported all of highlight.js.
// src/CodeViewer.tsx
import hljs from 'highlight.js'
type Props = { code: string }
export default function CodeViewer({ code }: Props) {
const html = hljs.highlightAuto(code).value
return (
<pre>
<code dangerouslySetInnerHTML={{ __html: html }} />
</pre>
)
}
// src/App.tsx
import { useState } from 'react'
import CodeViewer from './CodeViewer'
const sample = `function add(a, b) {
return a + b
}`
function App() {
const [show, setShow] = useState(false)
return (
<main>
<h1>コードの表示</h1>
<button onClick={() => setShow(true)}>コードを表示</button>
{show && <CodeViewer code={sample} />}
</main>
)
}
export default App
Running npm run build produced the following output.
vite v8.3.8 building client environment for production...
transforming...
✓ 213 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html 0.38 kB │ gzip: 0.26 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/index-T6EbTOO8.js 1,141.41 kB │ gzip: 376.00 kB
[plugin builtin:vite-reporter]
(!) Some chunks are larger than 500 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 228ms
There was only one JS file, index-T6EbTOO8.js, at 1,141.41 kB. The gzip size was 376.00 kB, but as the official documentation for build.chunkSizeWarningLimit says, the warning compares the size before gzip compression.
This is a warning, not an error. The exit code was 0, and dist/ was generated. When I opened it with vite preview and pressed the button, function and return were displayed inside span elements with the hljs-keyword class.
The warning lists three possible fixes: split the file with import(), control the splitting with codeSplitting, or change the limit with chunkSizeWarningLimit. Before trying them, I first looked at which library made the chunk large. The only library this app added to the template was highlight.js, so I reviewed how it was imported first. When you have many libraries and don't know which one is large, you can inspect the contents of the output files (covered in the Japanese article "How to Check the Size of Files Built with React and Vite" on this blog).
Loading only the languages you use
The ES modules section of the highlight.js README says that import hljs from 'highlight.js' registers all languages. It also says that importing only the core and registering the languages you need is more efficient. The large count of ✓ 213 modules transformed. comes from including all the language definition files.
This app only colors JavaScript, so following the README, I changed it to import highlight.js/lib/core and only the JavaScript definition. I did not change App.tsx.
// src/CodeViewer.tsx
import hljs from 'highlight.js/lib/core'
import javascript from 'highlight.js/lib/languages/javascript'
hljs.registerLanguage('javascript', javascript)
type Props = { code: string }
export default function CodeViewer({ code }: Props) {
const html = hljs.highlight(code, { language: 'javascript' }).value
return (
<pre>
<code dangerouslySetInnerHTML={{ __html: html }} />
</pre>
)
}
Since the language to color is fixed to JavaScript, I stopped using highlightAuto, which detects the language automatically, and switched to highlight with the language specified.
After building, the warning no longer appeared.
✓ 20 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html 0.38 kB │ gzip: 0.26 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/index-cj4FxKHy.js 247.30 kB │ gzip: 79.13 kB
✓ built in 152ms
The JS file went from 1,141.41 kB to 247.30 kB. When I opened it in the browser and pressed the button, function and return were again wrapped in span elements with the hljs-keyword class. Because I stopped using highlightAuto and specified the language, the class on add changed from hljs-title to hljs-title function_, and the arguments a, b also got hljs-params.
I tried this before splitting files because it reduces the amount of code the browser actually loads. The import() and codeSplitting approaches described later change when and in what units files load, so they hardly change the total size.
Splitting with import() may not be enough
Sometimes you cannot decide on the languages in advance and want to use all of highlight.js. In that case, I followed the first suggestion in the warning and used import() to move the display component into a separate file. In React, lazy and Suspense let you load a component the first time it is rendered (React reference for lazy). I returned CodeViewer.tsx to the first version that imports everything.
// src/App.tsx
import { lazy, Suspense, useState } from 'react'
const CodeViewer = lazy(() => import('./CodeViewer'))
const sample = `function add(a, b) {
return a + b
}`
function App() {
const [show, setShow] = useState(false)
return (
<main>
<h1>コードの表示</h1>
<button onClick={() => setShow(true)}>コードを表示</button>
{show && (
<Suspense fallback={<p>読み込み中…</p>}>
<CodeViewer code={sample} />
</Suspense>
)}
</main>
)
}
export default App
After building, the JS was split into two files, but the warning remained.
dist/index.html 0.38 kB │ gzip: 0.26 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/index-CzCPxKl7.js 222.06 kB │ gzip: 69.71 kB
dist/assets/CodeViewer-DzOskbnI.js 920.85 kB │ gzip: 307.48 kB
[plugin builtin:vite-reporter]
(!) Some chunks are larger than 500 kB after minification. Consider:
(The other three lines of the warning were the same as before.)
In the browser, when the page opened, only index-CzCPxKl7.js was loaded as JS. When I pressed the button, CodeViewer-DzOskbnI.js was loaded. The amount loaded on the first screen dropped to 222.06 kB. However, the split-off CodeViewer-DzOskbnI.js is still 920.85 kB, which is over 500 kB, so the warning remains.
Splitting a large library further with codeSplitting
The second suggestion, codeSplitting, is an option of Rolldown, the bundler used by Vite 8. You write it under build.rolldownOptions.output. In groups, you specify which modules are bundled into the same file. Here I grouped highlight.js's files into a group named highlight and used maxSize to set a rough target size for each file. I left App.tsx with import() as it was.
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
build: {
rolldownOptions: {
output: {
codeSplitting: {
groups: [
{
name: 'highlight',
test: /node_modules[\\/]highlight\.js/,
maxSize: 400 * 1024,
},
],
},
},
},
},
})
The regular expression in test uses [\\/] so that it also matches Windows path separators. This is the form recommended in the test note in the Rolldown codeSplitting reference.
After building, the warning no longer appeared.
dist/index.html 0.38 kB │ gzip: 0.25 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/highlight-DuAr9H9l.js 0.11 kB │ gzip: 0.11 kB
dist/assets/CodeViewer-CQJQIpno.js 0.25 kB │ gzip: 0.21 kB
dist/assets/highlight-DtO18omU.js 28.56 kB │ gzip: 10.25 kB
dist/assets/highlight-kMuR5eq3.js 211.09 kB │ gzip: 77.54 kB
dist/assets/highlight-BYLTUj7l.js 217.61 kB │ gzip: 69.54 kB
dist/assets/highlight-BU41oHqH.js 221.62 kB │ gzip: 67.25 kB
dist/assets/index-BnNt1PMl.js 222.38 kB │ gzip: 69.87 kB
dist/assets/highlight-DyfBiQ_O.js 244.85 kB │ gzip: 86.71 kB
✓ built in 210ms
The 920.85 kB CodeViewer file was split into six files whose names begin with highlight-, and the largest is 244.85 kB. However, adding the CodeViewer and highlight files together gives 924.09 kB, which is almost the same as before the split.
In the browser, when the page opened, only index-BnNt1PMl.js was loaded as JS. When I pressed the button, CodeViewer-CQJQIpno.js and the six highlight- files were loaded, and the code was displayed with classes such as hljs-keyword.
According to Rolldown's manual code splitting guide, maxSize is a target, not a strict limit. If a single module is larger than maxSize, its file will exceed maxSize, because Rolldown cannot currently split one module across multiple files. In this case, highlight.js is already divided into files per language, so it could be split.
Splitting with codeSplitting alone, without import()
When I returned App.tsx to the first version without lazy and built with the same vite.config.ts, the warning did not appear.
dist/index.html 0.95 kB │ gzip: 0.36 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/highlight-DfB4CiOC.js 0.12 kB │ gzip: 0.12 kB
dist/assets/rolldown-runtime-WkwZ5CU-.js 0.59 kB │ gzip: 0.36 kB
dist/assets/highlight-BdkMVSna.js 28.57 kB │ gzip: 10.26 kB
dist/assets/highlight-BP8-y3d1.js 211.10 kB │ gzip: 77.55 kB
dist/assets/highlight-CzUa8Vcm.js 217.62 kB │ gzip: 69.55 kB
dist/assets/index-95NiCcLo.js 219.91 kB │ gzip: 68.84 kB
dist/assets/highlight-CQm8NJjf.js 221.64 kB │ gzip: 67.26 kB
dist/assets/highlight-CfIR6WBT.js 244.86 kB │ gzip: 86.72 kB
However, in the browser, all eight files, which are the six highlight- files, rolldown-runtime-WkwZ5CU-.js, and index-95NiCcLo.js, were loaded when the page opened. No new JS was loaded when I pressed the button. The eight files total 1,144.41 kB. The number of files increased, but the amount loaded on the first screen is almost the same as with the full import (1,141.41 kB). If you want a lighter first page load, you need to combine this with loading parts later through import().

Raising chunkSizeWarningLimit only hides the warning
The third suggestion is to change the limit. I kept the original code that imports all of highlight.js and added chunkSizeWarningLimit: 1200 to vite.config.ts. The unit is kB.
// vite.config.ts
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
build: {
chunkSizeWarningLimit: 1200,
},
})
dist/index.html 0.38 kB │ gzip: 0.26 kB
dist/assets/index-mjUEDsdN.css 0.04 kB │ gzip: 0.06 kB
dist/assets/index-T6EbTOO8.js 1,141.41 kB │ gzip: 376.00 kB
✓ built in 246ms
The warning no longer appeared, but the JS file is the same index-T6EbTOO8.js, still 1,141.41 kB. This setting is only for when you accept that the file is large and just want to stop the warning.
When you see the warning, first find which library made the chunk large, and check whether you can load only the parts you use. If that is not enough, load parts later with import(), and if the split-off files still exceed 500 kB, split them with codeSplitting. codeSplitting alone or chunkSizeWarningLimit alone will remove the warning, but neither reduces the amount loaded at first.





