-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-minify.js
56 lines (50 loc) · 1.81 KB
/
build-minify.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
const fs = require('fs');
const terser = require('terser');
const filesToConcatAndMinify = [
'./assets/js/navigation.js',
'./assets/js/video-player.js',
'./assets/js/animations.js',
'./assets/js/card-flip.js',
'./assets/js/card.js',
'./assets/js/image-lazyloader.js',
'./assets/js/zoomable.js',
];
const individualFilesToMinify = [
'./assets/js/fxhash.js',
'./assets/js/interplay-page.js',
'./assets/js/notifications.js',
'./assets/js/homepage.js',
'./assets/js/toc-toggle.js'
];
// Function to minify a single file
async function minifyFile(inputFile, outputFile) {
try {
const fileContent = fs.readFileSync(inputFile, 'utf-8');
const result = await terser.minify(fileContent);
fs.writeFileSync(outputFile, result.code, 'utf-8');
console.log(`Minified ${inputFile} to ${outputFile}`);
} catch (err) {
console.error(`Failed to minify ${inputFile}:`, err);
}
}
// Function to concatenate and minify multiple files
async function concatAndMinifyFiles(inputFiles, outputFile) {
try {
let concatenatedContent = '';
for (const file of inputFiles) {
concatenatedContent += fs.readFileSync(file, 'utf-8');
}
const result = await terser.minify(concatenatedContent);
fs.writeFileSync(outputFile, result.code, 'utf-8');
console.log(`Concatenated and minified ${inputFiles.length} files to ${outputFile}`);
} catch (err) {
console.error(`Failed to concatenate and minify files:`, err);
}
}
// Minify individual files
individualFilesToMinify.forEach((file) => {
const outputFile = file.replace('.js', '.min.js');
minifyFile(file, outputFile);
});
// Concatenate and minify specified files
concatAndMinifyFiles(filesToConcatAndMinify, './assets/js/concatenated.min.js');