-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
48 lines (42 loc) · 1.55 KB
/
Copy pathbuild.js
File metadata and controls
48 lines (42 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
const fs = require("fs");
const path = require("path");
const { minify } = require("html-minifier");
const postcss = require("postcss");
const cssnano = require("cssnano");
const terser = require("terser");
// Paths
const inputDir = path.resolve(__dirname);
const outputDir = path.join(__dirname, "dist");
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
// Minify HTML
const html = fs.readFileSync(path.join(inputDir, "index.html"), "utf-8");
const minifiedHtml = minify(html, {
removeComments: true,
collapseWhitespace: true,
});
fs.writeFileSync(path.join(outputDir, "index.html"), minifiedHtml);
// Minify CSS using PostCSS and CSSNano
(async () => {
try {
const css = fs.readFileSync(path.join(inputDir, "style.css"), "utf-8");
const result = await postcss([cssnano()]).process(css, { from: undefined });
fs.writeFileSync(path.join(outputDir, "style.css"), result.css);
console.log("CSS successfully minified!");
} catch (error) {
console.error("Error while minifying CSS:", error);
}
})();
// Minify JavaScript using Terser
(async () => {
try {
const js = fs.readFileSync(path.join(inputDir, "script.js"), "utf-8");
const result = await terser.minify(js);
if (result.error) throw result.error;
fs.writeFileSync(path.join(outputDir, "script.js"), result.code);
console.log("JavaScript successfully minified!");
} catch (error) {
console.error("Error while minifying JavaScript:", error);
}
})();
console.log("Build process completed! Files are in the dist folder.");