-
Notifications
You must be signed in to change notification settings - Fork 7
/
postbuild.js
94 lines (80 loc) · 2.78 KB
/
postbuild.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require("fs");
const archiver = require("archiver");
const { execFileSync } = require("child_process");
const ect = require("ect-bin");
const path = require("path");
const inliner = require("web-resource-inliner").html;
const minify = require("html-minifier").minify;
const BUNDLE_FILE = "./dist/bundle.html";
// Create final html
inliner(
{
fileContent: fs.readFileSync("./dist/index.html", "utf8"),
relativeTo: "dist",
},
function (err, result) {
if (err) {
throw new Error("Failed to inline");
}
const minifiedResult = minify(result, {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: false,
minifyCSS: {
compatibility: {
properties: {
colors: false,
shorterLengthUnits: true,
},
},
},
});
fs.writeFileSync(BUNDLE_FILE, minifiedResult);
const ZIP_FILE = path.resolve("./dist/build.zip");
let output = fs.createWriteStream(ZIP_FILE);
let archive = archiver("zip");
const MAX = 13 * 1024; // 13kb
output.on("close", function () {
console.log(`Applying ECT to html file ${ZIP_FILE}`);
// ECT -#
// Select compression level [1-9] (default: 3).
// Advanced usage:
// A different syntax may be used to achieve even more compression for deflate compression if time (and efficiency) is not a concern.
// If the value is above 10000, the blocksplitting-compression cycle is repeated # / 10000 times. If # % 10000 is above 9, level 9 is
// used and the number of iterations of deflate compression per block is set to # % 10000. If # % 10000 is 9 or below, this number
// specifies the level.
// For instance, "-100500" saved 3 bytes
// Running advzip saved 2 bytes: npx advzip-bin -z -4 -i 5000 build.zip
const result = execFileSync(ect, ["-100500", "-strip", "-zip", ZIP_FILE]);
console.log(result.toString("utf8"));
const stats = fs.statSync(ZIP_FILE);
const bytes = stats["size"];
const percent = ((bytes / MAX) * 100).toFixed(2);
if (bytes > MAX) {
console.error(`Size overflow: ${bytes} bytes (${percent}%)`);
} else {
console.log(`Size: ${bytes} bytes (${percent}%)`);
}
});
archive.on("warning", function (err) {
if (err.code === "ENOENT") {
console.warn(err);
} else {
throw err;
}
});
archive.on("error", function (err) {
throw err;
});
archive.pipe(output);
archive.append(fs.createReadStream(BUNDLE_FILE), {
name: "index.html",
});
archive.finalize();
}
);