-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
69 lines (56 loc) · 2.21 KB
/
build.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
const fs = require("fs-extra");
const path = require("path");
const globby = require("globby");
const CleanCSS = require('clean-css');
const UglifyJS = require("uglify-es");
const HtmlMinifier = require('html-minifier').minify;
const child_process = require("child_process");
// Get directory paths
let xpPaintDir = path.join(__dirname, "xp-paint");
let tempBuildDir = path.join(__dirname, "xp-paint-temp-build");
// Removing existing build path
if (fs.existsSync(tempBuildDir)) {
fs.removeSync(tempBuildDir);
}
// Copy the source path to the build path
fs.copySync(xpPaintDir, tempBuildDir);
// Recursively search all files in build directory
globby.sync(path.join(tempBuildDir, "**")).forEach(filePath => {
// Minify and clean each file
switch (path.extname(filePath)) {
case ".js":
let result = UglifyJS.minify(fs.readFileSync(filePath).toString());
if (!result.error) {
fs.writeFileSync(filePath, result.code);
}
break;
case ".css":
let minifiedCSSCode = new CleanCSS({}).minify(fs.readFileSync(filePath).toString()).styles;
// CSS minifier is failing somewhere
//fs.writeFileSync(filePath, minifiedCSSCode);
break;
}
});
// Running monolith to create a single html file
let indexHtmlFilePath = path.join(tempBuildDir, "index.html");
let xpPaintHtmlOutputPath = path.join(__dirname, "xp-paint.html");
let proc = child_process.execSync(`monolith "${indexHtmlFilePath}" > "${xpPaintHtmlOutputPath}"`);
// Running the html minifier
let minfiedHtml = HtmlMinifier(fs.readFileSync(xpPaintHtmlOutputPath).toString(), {
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
collapseWhitespace: true,
minifyURLs: true,
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
//removeScriptTypeAttributes: true,
//removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
});
fs.writeFileSync(xpPaintHtmlOutputPath, minfiedHtml)
// Cleaning up the temporary build directory
fs.removeSync(tempBuildDir);
console.log("Build completed successfully.");