🔧 ci: update vite script, auto remove trivial files under dist/

This commit is contained in:
frostime 2024-10-24 14:51:01 +08:00
parent 591cf2e95e
commit 38b19fdb88
2 changed files with 75 additions and 1 deletions

View file

@ -41,6 +41,7 @@ export default defineConfig({
{ src: "./icon.png", dest: "./" }
],
}),
],
define: {
@ -77,6 +78,11 @@ export default defineConfig({
}
}
] : [
// Clean up unnecessary files under dist dir
cleanupDistFiles({
patterns: ['i18n/*.yaml', 'i18n/*.md'],
distDir: outputDir
}),
zipPack({
inDir: './dist',
outDir: './',
@ -98,4 +104,60 @@ export default defineConfig({
},
},
}
})
});
/**
* Clean up some dist files after compiled
* @author frostime
* @param options:
* @returns
*/
function cleanupDistFiles(options: { patterns: string[], distDir: string }) {
const {
patterns,
distDir
} = options;
return {
name: 'rollup-plugin-cleanup',
enforce: 'post',
writeBundle: {
sequential: true,
order: 'post' as 'post',
async handler() {
const fg = await import('fast-glob');
const fs = await import('fs');
// const path = await import('path');
// 使用 glob 语法,确保能匹配到文件
const distPatterns = patterns.map(pat => `${distDir}/${pat}`);
console.debug('Cleanup searching patterns:', distPatterns);
const files = await fg.default(distPatterns, {
dot: true,
absolute: true,
onlyFiles: false
});
// console.info('Files to be cleaned up:', files);
for (const file of files) {
try {
if (fs.default.existsSync(file)) {
const stat = fs.default.statSync(file);
if (stat.isDirectory()) {
fs.default.rmSync(file, { recursive: true });
} else {
fs.default.unlinkSync(file);
}
console.log(`Cleaned up: ${file}`);
}
} catch (error) {
console.error(`Failed to clean up ${file}:`, error);
}
}
}
}
};
}