-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (49 loc) · 1.55 KB
/
index.js
File metadata and controls
49 lines (49 loc) · 1.55 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
const fs = require('fs').promises;
const path = require('path');
const CleanCSS = require('clean-css');
const terser = require('terser');
async function minifyCSS(inputPath, outputPath) {
try {
const css = await fs.readFile(inputPath, 'utf8');
const minified = new CleanCSS().minify(css);
await fs.writeFile(outputPath, minified.styles);
console.log(`CSS minified: ${outputPath}`);
} catch (err) {
console.error(`Error minifying CSS: ${err}`);
}
}
async function minifyJS(inputPath, outputPath) {
try {
const js = await fs.readFile(inputPath, 'utf8');
const minified = await terser.minify(js);
await fs.writeFile(outputPath, minified.code);
console.log(`JS minified: ${outputPath}`);
} catch (err) {
console.error(`Error minifying JS: ${err}`);
}
}
async function minifyJSON(inputPath, outputPath) {
try {
const json = await fs.readFile(inputPath, 'utf8');
const minified = JSON.stringify(JSON.parse(json));
await fs.writeFile(outputPath, minified);
console.log(`JSON minified: ${outputPath}`);
} catch (err) {
console.error(`Error minifying JSON: ${err}`);
}
}
async function main() {
await minifyCSS(
path.join(__dirname, 'public', 'css', 'style.css'),
path.join(__dirname, 'public', 'css', 'style.min.css')
);
await minifyJS(
path.join(__dirname, 'public', 'js', 'script.js'),
path.join(__dirname, 'public', 'js', 'script.min.js')
);
await minifyJSON(
path.join(__dirname, 'public', 'json', 'list.json'),
path.join(__dirname, 'public', 'json', 'list.min.json')
);
}
main();