You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vite中自动移除script标签的type="module" crossorigin属性?

如何修改Vite配置移除index.html中script标签的type="module"和crossorigin属性

移除crossorigin属性

在vite.config.js的build选项里设置crossorigin: false即可:

export default {
  build: {
    crossorigin: false
  }
}

移除type="module"属性

Vite默认输出ES模块脚本,要去掉type="module",需要将构建产物改为非模块格式(比如IIFE或UMD),具体分两种场景:

场景1:构建类库

直接配置build.lib指定输出格式:

export default {
  build: {
    lib: {
      entry: 'src/main.js', // 你的项目入口文件路径
      name: 'YourLibName',
      formats: ['iife'] // 选择iife或umd格式,这类格式无需type="module"
    },
    crossorigin: false // 同时移除crossorigin属性
  }
}

场景2:构建普通应用

通过rollupOptions.output指定输出格式:

export default {
  build: {
    rollupOptions: {
      output: {
        format: 'iife' // 或umd,根据项目需求选择
      }
    },
    crossorigin: false
  }
}

注意:切换到IIFE/UMD格式后,需确保项目代码兼容这类非模块语法,避免出现兼容性问题。

内容的提问来源于stack exchange,提问作者Rowling.Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:57:13