vite编译html-docx.js提示With statements cannot be used with the “esm“ output format due to strict mode

使用pnpm run dev 编译时,提示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:
      13103with(obj||{}){~~~~~~~~

经Vite官方网站查询,发现是以下原因导致的

Vite 无法处理、也不支持仅可在非严格模式(sloppy mode)下运行的代码。这是因为 Vite 使用了 ESM 并且始终在 ESM
中使用 严格模式。
详见:https://cn.vitejs.dev/guide/troubleshooting

解决方案很简单,直接编辑修改html-docx.js中有with的语句,将它删掉后,把大括号{}中的变量使用obj.这样去处理。
例如原语句:

with(obj||{}){
__p+='MIME-Version: 1.0\nContent-Type: multipart/related;\n    type="text/html";\n    boundary="----=mhtDocumentPart"\n\n\n------=mhtDocumentPart\nContent-Type: text/html;\n    charset="utf-8"\nContent-Transfer-Encoding: quoted-printable\nContent-Location: file:///C:/fake/document.html\n\n'+
((__t=( htmlSource ))==null?'':__t)+
'\n\n'+
((__t=( contentParts ))==null?'':__t)+
'\n\n------=mhtDocumentPart--\n';
}

改为:

__p+='MIME-Version: 1.0\nContent-Type: multipart/related;\n    type="text/html";\n    boundary="----=mhtDocumentPart"\n\n\n------=mhtDocumentPart\nContent-Type: text/html;\n    charset="utf-8"\nContent-Transfer-Encoding: quoted-printable\nContent-Location: file:///C:/fake/document.html\n\n'+
((__t=( obj.htmlSource ))==null?'':__t)+
'\n\n'+
((__t=( obj.contentParts ))==null?'':__t)+
'\n\n------=mhtDocumentPart--\n';

参考:
https://blog.csdn.net/weixin_43110440/article/details/108518385

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值