-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostbuild-linux.js
More file actions
68 lines (54 loc) · 1.83 KB
/
postbuild-linux.js
File metadata and controls
68 lines (54 loc) · 1.83 KB
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
console.log('🚀🚀 Running postbuild.js...🚀🚀');
console.time('🔥 HTML minified in');
import fs from 'fs';
import path from 'path';
import { minify } from 'html-minifier-terser';
import { fileURLToPath } from 'url';
import * as glob from 'glob';
// HTML minification
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const minifyOptions = {
minifyCSS: true,
minifyJS: true,
removeComments: false,
useShortDoctype: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
collapseBooleanAttributes: true,
removeRedundantAttributes: true,
};
const buildPath = path.join(__dirname, 'build');
const processHtmlFiles = async () => {
const files = glob.sync(path.join(buildPath, '**/*.html'));
for (const file of files) {
const stats = await fs.promises.stat(file);
if (stats.isFile()) {
const fileContent = await fs.promises.readFile(file, 'utf8');
const result = await minify(fileContent, minifyOptions);
await fs.promises.writeFile(file, result);
}
}
};
processHtmlFiles().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);
});