I faced the same issue. My CSS file is named extension.css
and I have it in public folder, so Vite moves it to root folder. And I want it last in the generated HTML, to give it priority.
The plugin turned out to be quite simple to implement:
{
name: 'move-extension-css-to-end',
transformIndexHtml(html) {
return html
.replace(
' <link rel="stylesheet" href="./extension.css" />\n',
''
)
.replace(
' </head>',
' <link rel="stylesheet" href="./extension.css" />\n </head>'
);
},
},
First replace removes my stylesheet link it from where it is placed by Vite. The second puts it directly before body.
You can make it fancier / more flexible with regex, etc. For me string replacement works just fine. You can find what to replace in the generated HTML file in dist
folder.