79359524

Date: 2025-01-15 19:54:46
Score: 0.5
Natty:
Report link

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.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: smyk