vite-plugin-clean
Version:
A vite plugin to remove/clean your build file(s) or folder(s).
46 lines (40 loc) • 1.27 kB
JavaScript
;
const fs = require('fs');
const path = require('path');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
const { resolve, join } = path__default;
const { existsSync, readdirSync, statSync, unlinkSync, rmdirSync } = fs__default;
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);
});
}
});
module.exports = cleanPlugin;