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?
1 Answer
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'),
],
})
3 Comments
Jaromanda X
Nice! You saved my receding hairline 👴
waldemar_enns
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.
vite.config.js->build: { rollupOptions: { ...but it's just a guess.