-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostbuild-windows.js
71 lines (57 loc) · 1.8 KB
/
postbuild-windows.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
console.log('🚀🚀 Running postbuild.js...🚀🚀');
console.time('🔥 HTML minified in');
import fs from 'fs';
import path from 'path';
import { minify } from 'html-minifier-terser';
// HTML minification
const minifyOptions = {
minifyCSS: true,
minifyJS: true,
removeComments: false,
useShortDoctype: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
};
async function traverseDir(dirPath) {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
await traverseDir(filePath);
} else if (path.extname(filePath) === '.html') {
await minifyFile(filePath);
}
}
}
async function minifyFile(filePath) {
const input = fs.readFileSync(filePath, 'utf8');
const output = await minify(input, minifyOptions);
fs.writeFileSync(filePath, output);
}
(async () => {
await traverseDir('./build');
})().catch((err) => {
console.error(`🚨 Error while processing HTML files in build directory:`, err);
});
console.timeEnd('🔥 HTML minified in');
// specify the folders to delete
const buildFolder = 'build';
const foldersToDelete = ['db', 'image-data'];
// helper function to delete a folder
const deleteFolder = (folderPath) => {
if (fs.existsSync(folderPath)) {
fs.rm(folderPath, { recursive: true, force: true }, (err) => {
if (!err) {
console.log(`💥 ${folderPath} is deleted!`);
}
});
}
};
// delete the specified folders
foldersToDelete.forEach((folder) => {
const folderPath = path.join(buildFolder, folder);
deleteFolder(folderPath);
});