marketingSaaS-front_ui/vite.plugin.js

51 lines
1.4 KiB
JavaScript

const fs = require('fs-extra')
const path = require('path')
function viteCopyToNewFolderPlugin(options = {}) {
const { outputDir } = options // 允许通过选项传递输出目录
return {
name: 'vite-plugin-copy-to-new-folder',
closeBundle() {
// 使用fs-extra的copy方法来复制目录
const distDir = path.resolve(process.cwd(), 'dist/build/h5')
const outDir = path.resolve(process.cwd(), 'dist/build', outputDir)
try {
fs.copySync(distDir, outDir, { overwrite: true })
} catch (e) {
console.log('复制打包文件出错-->', e)
}
},
}
}
function vitePluginRemoveDir(options = {}) {
// 默认要删除的目录名
const { dirName } = options
if (!dirName) {
return {}
}
return {
name: 'vite-plugin-remove-dir', // 插件名称
closeBundle() {
setTimeout(() => {
const distDir = path.resolve(process.cwd(), 'dist/build')
const fullPath = path.join(distDir, dirName) // 构造要删除的目录的完整路径
console.log('fullPath', fullPath)
// 构建结束后执行的逻辑
// 尝试删除目录
try {
fs.removeSync(fullPath)
console.log(`目录${fullPath}成功删除.`)
} catch (err) {
console.log(`目录${fullPath}删除失败-->${err}`)
}
}, 2000)
},
}
}
module.exports = {
viteCopyToNewFolderPlugin,
vitePluginRemoveDir,
}