dan1234827's avatar

Changing Vite's asset naming scheme

When I run a Vite production build the js assets are named "myfile-abcxzy.js", presumably for cache busting. Is there a way to have it output to "myfile.js"?

The js is to be embedded on a third party site, so I really don't want the name to change between builds!

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To change Vite's asset naming scheme and ensure that the output file names remain consistent between builds, you can configure the build.rollupOptions.output option in your vite.config.js file. Specifically, you can set the entryFileNames option to a fixed name.

Here's how you can do it:

// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        entryFileNames: 'myfile.js', // Set a fixed name for the entry file
        // You can also set fixed names for other types of assets if needed
        // chunkFileNames: 'chunk-[name].js',
        // assetFileNames: 'asset-[name].[ext]'
      }
    }
  }
});

This configuration will ensure that the main entry file is always named myfile.js in the production build, preventing the name from changing between builds. Note that this approach is suitable when you have a single entry point or when you want to control the naming of specific entry points. If you have multiple entry points, you might need to adjust the naming pattern accordingly.

1 like
Sinnbeck's avatar

Just remember to ensure cache busting some other way then!

Please or to participate in this conversation.