Vite escapes characters from index.html src tag

86 views Asked by At

I am trying to generate index.html with Vite that has unescaped variable in src tag, but Vite is always escaping it

What I want is this:

  <!DOCTYPE html>
  <html lang="en">
    <head>
       <script type="module" src="/{{ variable }}/assets/index.js"></script>
    </head>
    <body>
    </body>
  </html>

But after building with Vite it is transformed to

  <!DOCTYPE html>
  <html lang="en">
    <head>
       <script type="module" src="/%7B%7B%20variable%20%7D%7D/assets/index.js"></script>
    </head>
    <body>
    </body>
  </html>

I am trying to accomplish that by setting base property in Vite configuration as follows

{
   base: "/{{ variable }}"
}

But obviously not working

1

There are 1 answers

1
yory100 On

I ended writing my own plugin, something like that:

plugins: [{
   name: "transform-index-html",
   transformIndexHtml(html) {
        return html.replaceAll("/value_to_replace", "{{ new value }}");
   },
}]