【问题标题】:How to keep an absolute path unchanged for a script tag in index.html with Vite如何使用 Vite 保持 index.html 中脚本标签的绝对路径不变
【发布时间】:2023-01-19 23:40:44
【问题描述】:

使用 Vite,在 index.html 中使用绝对路径会导致它们根据 base 配置进行转换,这很好。

例如我的index.html 包含这个:

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

base 设置为“/demo”。导出的index.html 将包含:

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

这对我的大部分文件来说都很好,但我需要从服务器的根目录加载一个脚本来处理非常具体的事情。是否可以指示 Vite 不更改特定脚本标签的路径?像预处理器命令?像这样:

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

那样就好了!

【问题讨论】:

    标签: javascript html vite rollupjs


    【解决方案1】:

    我使用了一个丑陋的修复程序:

        <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>
    

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2015-08-14
      • 2021-09-15
      • 2022-08-09
      • 1970-01-01
      相关资源
      最近更新 更多