5

I'm creating a Vue 3 application that includes Vite's building process, and I'm looking to change the final built index.html filename to index.html.php. I can't find any reference in their documentation. Is it even possible?

2
  • I guess you can modify the index file inside your vite.config.js -> build: { rollupOptions: { ... but it's just a guess. Commented May 5, 2022 at 14:44
  • I looked through the rollupOptions documentation but I couldn't find an option to change the compiled filename. Commented May 5, 2022 at 15:24

1 Answer 1

16

One solution is to add a Vite plugin that renames the index.html of the bundle. For example, this Vite plugin takes a string and assigns that to index.html's final name:

/**
 * @param newFilename {string}
 * @returns {import('vite').Plugin}
 */
const renameIndexPlugin = (newFilename) => {
  if (!newFilename) return

  return {
    name: 'renameIndex',
    enforce: 'post',
    generateBundle(options, bundle) {
      const indexHtml = bundle['index.html']
      indexHtml.fileName = newFilename
    },
  }
}

Then use it in your Vite config:

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

export default defineConfig({
  plugins: [
    renameIndexPlugin('index.html.php'),
  ],
})

demo

Sign up to request clarification or add additional context in comments.

3 Comments

Nice! You saved my receding hairline 👴
When running npm run build and afterwards npm run serve (running the built application in preview mode) that leads into a 404 resolve, since the preview mode can't resolve the indx.html.php file. To make it work in production, you need to also point to that index.html.php file. Have not tried it yet, but will post it here when I am done.
This is really cool, but it doesn't work with other plugins such as CRXJS. The file may have been renamed, but manifest.json still retains the old path. Anyone know how to resolve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.