uniapp-h5/vite.plugin.js

37 lines
1.0 KiB
JavaScript

const fs = require("fs-extra");
const path = require("path");
// Vite插件函数
function vitePluginRemoveDir(options = {}) {
// 默认要删除的目录名
const { dirName } = options;
if (!dirName.length) {
return {};
}
return {
name: "vite-plugin-remove-dir", // 插件名称
closeBundle() {
const distDir = path.resolve(process.cwd(), "dist");
// 构建结束后执行的逻辑
for (const item of dirName) {
const fullPath = path.join(
distDir,
["production"].includes(process.env.NODE_ENV) ? "build" : "dev",
process.env.UNI_PLATFORM,
`static/${item}`
); // 构造要删除的目录的完整路径
console.log("fullPath", fullPath);
// 尝试删除目录
try {
fs.removeSync(fullPath);
console.log(`目录${fullPath}成功删除.`);
} catch (err) {
console.log(`目录${fullPath}删除失败-->${err}`);
}
}
},
};
}
module.exports = vitePluginRemoveDir;