Search code examples
javascripthtmlviterollupjs

How to keep an absolute path unchanged for a script tag in index.html with Vite


With Vite, using absolute paths in index.html results in them being transformed based on the base config, which is nice.

For instance my index.html contains this:

<script type="text/javascript" src="/config.js"></script>

And base is set to "/demo". The exported index.html will contain:

<script type="text/javascript" src="/demo/config.js"></script>

This is fine for most of my file but I would need to have a script loaded from the root of the server for a very specific thing. Is it possible to indicate to Vite to not change the path of a specific script tag? Like a preprocessor command? Like this:

<!-- @vite-ignore -->
<script type="text/javascript" src="/config.js"></script>

That would be nice!


Solution

  • I used an ugly fix:

        <script type="text/javascript">
          // We have to do it using javascript otherwise Vite modifies the path
          var config = document.createElement('script')
          config.type = 'text/javascript'
          config.src = '/config.js'
          document.head.appendChild(config)
        </script>