Skip to content

Expose code in entryFileNames, chunkFileNames, and assetFileNames #5362

@relaxedleaf

Description

@relaxedleaf

Feature Use Case

Currently I am using Vite to build bundle for my React app. One of the features I need is to customize names of the output files. To be specific, I want to make all of my output names to be in assets/[name]-[hash].[ext] format, but in lowercase.

I don't think there is any way for me to get the hash generated by rollup directly, so I will have to create a custom hashing function to generate hash in lowercase

The following is my implementation

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import { createHash } from "node:crypto";

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        chunkFileNames: (chunkInfo) => {
	  const uniqueToken = Date.now().toString();
          const hash = createHash("sha256")
            .update(JSON.stringify(chunkInfo) + uniqueToken)
            .digest("hex")
            .substring(0, 8);
          return `assets/${chunkInfo.name.toLowerCase()}-${hash}.js`;
        },
      },
    },
  },
});

My implementation is not ideal for cache busting as it appends uniqueToken to chunkInfo, which will generate new file names for every single file on each build.

An ideal situation is that I could generate custom hash based on the content of the file. This way the hash will remain the same if the source file is never changed

Feature Proposal

  • Expose the file content in entryFileNames, chunkFileNames, and assetFileNames so that we have a better way to generate hash based on the actual content of the file
  • In my case, it would even be better if the default output hash is part of the callbacks

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions