vite打包,pdfjs-dist 报错import引入pdfjs-dist报错Top-level await is not available、No "GlobalWorkerOptions.workerSrc"等

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)

node_modules/pdfjs-dist/build/pdf.mjs:17349:53:
  17349 │ /******/ __webpack_exports__ = globalThis.pdfjsLib = await (globalThis.pdfjsLibPromise = __webpack_exports__);

原因:vite 不支持顶级的 async/await 语法,需要安装插件做兼容
解决方案:
vite.config.ts 安装并引入 topLevelAwait
npm install vite-plugin-top-level-await -D
// vite.config.ts 文件
// 解决 vite build打包报错Top-level await is not available

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

export default ({ mode }: any) => {
    return defineConfig({
        plugins: [
            topLevelAwait({
                promiseExportName: '__tla',
                promiseImportName: (i) => `__tla_${i}`
            })
        ],
    })
}
posted @ 2024-04-25 17:52  shine_lovely  阅读(740)  评论(0编辑  收藏  举报