问题描述,
- 开发环境
vite
报 util
的引入的错。因为mammoth
使用 require('util')
- 打包,报
undefined
的错。因为mammoth
使用require('stream')
配置
- package.sjon
"mammoth": "^1.5.1",
"vite": "^3.0.0",
- vite.config.ts
import rollupNodePolyFill from 'rollup-plugin-node-polyfills'
export default defineConfig(({ mode, command }) => {
let aliasObj: AliasOptions = {}
// 生成环境需要的node模块。直接写入开发环境的 mammoth.convertToHtml 有问题
if (mode === 'production') {
aliasObj = {
events: 'rollup-plugin-node-polyfills/polyfills/events',
process: 'rollup-plugin-node-polyfills/polyfills/process-es6.js',
stream: 'rollup-plugin-node-polyfills/polyfills/stream',
}
}
return {
// ...
resolve: {
alias: {
// 兼容包使用 node的模块
util: 'rollup-plugin-node-polyfills/polyfills/util',
...aliasObj,
},
},
rollupOptions: {
plugins: [
// Enable rollup polyfills plugin, used during production bundling
rollupNodePolyFill(),
],
},
},
}
})