美文网首页
解决 vite build打包报错Top-level await

解决 vite build打包报错Top-level await

作者: 武汉前端阿杰1001 | 来源:发表于2023-02-02 11:19 被阅读0次

一、错误原因
最高层中不让使用 await
二、解决方案
1.引入vite-plugin-top-level-await

npm install vite-plugin-top-level-await -D

2.在vite.config.js配置此插件

import topLevelAwait from 'vite-plugin-top-level-await'

export default defineConfig({
  plugins: [
    topLevelAwait({
      // The export name of top-level await promise for each chunk module
      promiseExportName: '__tla',
      // The function to generate import names of top-level await promise in each chunk module
      promiseImportName: i => `__tla_${i}`
    })
  ]
});

3.重新打包

相关文章

网友评论

      本文标题:解决 vite build打包报错Top-level await

      本文链接:https://www.haomeiwen.com/subject/beiwhdtx.html