vite 使用 html-docx.js

59 阅读1分钟

vite使用html-docx.js会报错

ERROR] With statements cannot be used with the "esm" output format due to strict mode

    node_modules/.pnpm/html-docx-js@0.3.1/node_modules/html-docx-js/dist/html-docx.js:13103:0:
      13103 │ with(obj||{}){
            ╵ ~~~~
            ╵ ~~~~

原因是 Vite 无法处理、也不支持仅可在非严格模式(sloppy mode)下运行的代码。这是因为 Vite 使用了 ESM 并且始终在 ESM 中使用 严格模式。 要解决这问题 ,需要修改源码,将with用法去除 下面是改好之后的html-docx.js文件,可以直接使用 html-docx.js