skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Manage Vite Assets Like A Pro

Control where Vite puts build assets. Set entryFileNames, assetFileNames, and chunkFileNames in rollupOptions to sort images, CSS, and JS into folders.

7 min read Updated

To control where Vite writes build assets, set build.rollupOptions.output.entryFileNames, chunkFileNames, and assetFileNames. Each one takes a path pattern like assets/js/[name]-[hash].js, and assetFileNames also accepts a function, so you can route images, CSS, and XML to different folders. Updated: November 2026.

I do a lot of work with TypeScript and Vite, and my vite.config.ts gets messy fast. Vite’s configurability is its strength and its biggest barrier. In this post, I’ll show how to place assets predictably while keeping Vite’s hashing and chunking, plus a plugin I think belongs in Vite out of the box. If you’re wiring Vite into a .NET app, my post on running Vite with ASP.NET Core web applications covers that side.

How do I control where Vite outputs assets?

Override three options under build.rollupOptions.output:

  • entryFileNames: your entry point JavaScript files.
  • chunkFileNames: shared chunks Vite splits out, such as dynamic imports.
  • assetFileNames: everything else, like images, CSS, fonts, and XML.
build: {
rollupOptions: {
output: {
entryFileNames: "assets/js/[name]-[hash].js",
chunkFileNames: "assets/js/[name]-[hash]-chunk.js",
assetFileNames: "assets/[name]-[hash][extname]"
}
}
}

Use build.outDir to move the whole output folder and build.assetsDir to rename the assets subfolder. The rest of this post builds a per-file-type version of assetFileNames.

A Vite Project and Goals

When starting a Vite-powered project, you’ll likely pick your frontend framework, possibly set up TypeScript, install external dependencies, and add assets like images, videos, and other static files. However, Vite operates in what I call a split-mind approach. As a result, development mode differs from what you expect during the build process. Your development folder can use one organizational strategy, while your distribution folder takes a completely different approach.

Understanding and configuring where your final build artifacts end up is optional in Vite, but taking the time to think through the process can set you up for success when deploying your app.

For example, keeping images in a dedicated folder makes it easier to manage static assets in a hosting environment. Additionally, some files, like RSS feeds, might not benefit from hashing and could be counterproductive. Finally, folders enable CDN and syncing strategies at a higher level than a single folder or file.

So, how do you manage your Vite assets? Let me show you.

Vite Assets Configuration

Vite options offer many methods and values you can override to achieve the behavior you want. In short, you need to override the following Vite option values:

  • build.rollupOptions.output.entryFileNames
  • build.rollupOptions.output.assetFileNames
  • build.rollupOptions.output.chunkFileNames

While you could certainly set these values inline, you’ll find your vite.config.ts file quickly becomes unruly. So what I did was create a file at config/assets.ts. Let’s take a look at it.

import type {PreRenderedAsset} from "rollup";
type AssetOutputEntry = {
output: string,
regex: RegExp
}
export const assetDir = "assets";
export const entryFileNames = `${assetDir}/js/[name]-[hash].js`;
export const chunkFileNames = `${assetDir}/js/[name]-[hash]-chunk.js`
const assets: AssetOutputEntry[] = [
{
output: `${assetDir}/img/[name]-[hash][extname]`,
regex: /\.(png|jpe?g|gif|svg|webp|avif)$/
},
{
regex: /\.css$/,
output: `${assetDir}/css/[name]-[hash][extname]`
},
{
output: `${assetDir}/js/[name]-[hash][extname]`,
regex: /\.js$/
},
{
output: `[name][extname]`,
regex: /\.xml$/
}
];
export function processAssetFileNames(info: PreRenderedAsset): string {
const name = info?.names?.[0];
if (name) {
const result = assets.find(a => a.regex.test(name));
if (result) {
return result.output;
}
}
// default since we don't have an entry
return `${assetDir}/[name]-[hash][extname]`
}

2026 note: entryFileNames, chunkFileNames, and assetFileNames are unchanged in Rollup 4, and PreRenderedAsset is still exported from rollup. Rollup 4 deprecated the single-value fields, though. info.name is deprecated in favor of info.names (a string[]), and info.originalFileName is deprecated in favor of info.originalFileNames. I use info.names[0] in the code above. info.name still works, but migrate when you touch this file.

Vite 8 is powered by Rolldown instead of Rollup. In Vite 8, build.rollupOptions is deprecated in favor of build.rolldownOptions, and rollupOptions remains as an alias for now. Rolldown exports its own PreRenderedAsset type, so don’t assume the rollup import is interchangeable if you move your config to rolldownOptions. The rolldown-vite package was the opt-in path on Vite 7.

The file allows anyone to manage assets by setting a new AssetOutputEntry item with a corresponding output and regex value. As Vite finds new assets to process, it passes the information to processAssetFileNames, at which point you can route the file to its final destination. Let’s see how you use this new file in your vite.config.ts file.

import {defineConfig} from "vite";
import {processAssetFileNames, entryFileNames, chunkFileNames, assetDir} from "./config/assets";
import {resolve} from "path";
import viteHtmlResolveAlias from 'vite-plugin-html-resolve-alias'
export default defineConfig({
resolve: {
alias: {
'@img': resolve(__dirname, 'src/img/'),
'@rss': resolve(__dirname, 'src/rss.xml')
}
},
plugins: [
viteHtmlResolveAlias()
],
build: {
minify: true,
assetsDir: assetDir,
// don't inline anything for demo
assetsInlineLimit: 0,
emptyOutDir: true,
rollupOptions: {
output: {
entryFileNames: entryFileNames,
assetFileNames: processAssetFileNames,
chunkFileNames: chunkFileNames
}
}
}
});

Now you can manage all your assets from your assets.ts file using regular expressions. Neat!

Now let’s look at how we can use the assets in our HTML files.

HTML Resolve Alias Vite Assets

You will want to use some assets in HTML and JavaScript files. For example, I’m using an image of vite.svg in the main.ts file and index.html in this sample.

Note: The following package says it’s incompatible with the latest Vite version, but it’s not. So you may need to run npm install --force to install the package.

Using the package vite-plugin-html-resolve-alias, I can create resolution aliases that the plugin will process in my HTML files. But first, let’s see what setting up aliases looks like. You may have noticed the resolve section in my previous configuration file.

resolve: {
alias: {
'@img': resolve(__dirname, 'src/img/'),
'@rss': resolve(__dirname, 'src/rss.xml')
}
},

These are aliases to files found in my src directory that I want to run through the Vite asset pipeline and use in TypeScript and HTML. To use these files in HTML, we can use our new aliases.

<link rel="icon" type="image/svg+xml" href="@img/vite.svg"/>
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="@rss"/>

When we build, these assets are processed similarly to any other asset imported in our TypeScript. Neat! Here’s a snippet from the final build output.

<link rel="icon" type="image/svg+xml" href="/assets/img/vite-4a748afd.svg">
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/rss.xml">

Depending on the use case, I’m no longer hampered by duplicate assets in public or src. I can use the same asset in both static HTML and dynamic JavaScript. That’s a win-win!

FAQ

How do I change Vite’s output directory?

Set build.outDir in vite.config.ts. It defaults to dist. Set build.emptyOutDir: true if the folder sits outside your project root, and you want it cleaned on each build.

How do I stop Vite from hashing certain files?

Two options. Put the file in the public folder, and Vite copies it as-is with no hash. Or return a pattern without [hash] from assetFileNames, like the [name][extname] rule for .xml above.

How do I organize Vite assets into folders (images, css, js)?

Make assetFileNames a function, match on the file extension, and return a different folder for each type. That’s what processAssetFileNames does in this post. Set entryFileNames and chunkFileNames to a js folder.

What is assetFileNames vs chunkFileNames vs entryFileNames?

entryFileNames names your entry point scripts, chunkFileNames names shared or lazily loaded JavaScript chunks, and assetFileNames names non-JavaScript files like images and CSS.

Conclusion

Vite’s asset pipeline is robust, especially for folks building web applications. With some care and thought, you can decide how assets are processed and where they end up in the final distribution folder.

If you’d like to see a working sample of this blog post, I’ve uploaded it to my GitHub repository, where you can see the build in action.

I hope you found this post helpful, and let me know what you think. As always, thanks for reading and sharing my posts. Cheers.

Comments

Related Posts