vite-plugin-clean
Version:
A vite plugin to remove/clean your build file(s) or folder(s).
39 lines (36 loc) • 998 B
JavaScript
import fs from 'fs';
import path from 'path';
const { resolve, join } = path;
const { existsSync, readdirSync, statSync, unlinkSync, rmdirSync } = fs;
function cleanFiles(dirPath) {
if (existsSync(dirPath)) {
if (statSync(dirPath).isDirectory()) {
let files = readdirSync(dirPath);
files.forEach((file) => {
let curPath = join(dirPath, file);
if (statSync(curPath).isDirectory()) {
cleanFiles(curPath);
rmdirSync(curPath);
} else {
unlinkSync(curPath);
}
});
} else {
unlinkSync(dirPath);
}
}
}
const cleanPlugin = (options) => ({
name: "vite-plugin-clean",
enforce: "pre",
apply: "build",
buildStart: () => {
const target = options?.targetFiles || ["dist"];
const targets = Array.isArray(target) ? target : [target];
targets.forEach((dir) => {
const dirPath = resolve(process.cwd(), dir);
cleanFiles(dirPath);
});
}
});
export { cleanPlugin as default };