forked from MathMan05/JankClient
-
Notifications
You must be signed in to change notification settings - Fork 1
/
minify.js
149 lines (134 loc) · 5.23 KB
/
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict"
const fsPromises = require("node:fs").promises
const UglifyJS = require("uglify-js")
const CleanCSS = require("clean-css")
const nameCache = {}
const defaultOptions = {
compress: {
passes: 5,
unsafe: true,
unsafe_Function: true,
unsafe_math: true,
unsafe_proto: true,
unsafe_regexp: true
}
}
const results = []
const minifyFile = async (inputPath, options = {}) => {
const filename = inputPath.split("/").pop()
const content = await fsPromises.readFile(inputPath, "utf8")
let result = {}
if (filename.endsWith(".js")) {
result = UglifyJS.minify({
[inputPath]: content
}, {
sourceMap: {
root: "https://spacebar.vanillaminigames.net/",
filename,
url: filename + ".map"
},
warnings: "verbose",
parse: {
shebang: false
},
toplevel: true,
nameCache,
mangle: true,
...defaultOptions,
...options
})
if (result.error) throw result.error
if (result.warnings && result.warnings.length > defaultOptions.compress.passes)
console.log(inputPath, result.warnings.filter(w => !w.includes(" last_count: ") && !w.includes("Retaining variable ")))
} else if (filename.endsWith(".css")) {
const clean = new CleanCSS({
compatibility: {
colors: {
hexAlpha: true
},
properties: {
shorterLengthUnits: true,
urlQuotes: false
}
},
level: {
2: {
mergeSemantically: true,
removeUnusedAtRules: true
}
},
inline: false,
sourceMap: true,
...options
})
const output = clean.minify(content)
result = {
code: output.styles + "\n/*# sourceMappingURL=" + filename + ".map */",
map: output.sourceMap.toString().replace("$stdin", filename)
}
if (output.warnings.length > 0 || output.errors.length > 0) console.log(inputPath, output.warnings, output.errors)
} else if (filename.endsWith(".json")) {
result = {
code: JSON.stringify(JSON.parse(content))
}
} else return console.error("Unknown minify file type: " + inputPath)
if (result.code.length >= content.length) return console.log("No reduction for " + inputPath + " (" + content.length + " -> " + result.code.length + ")")
if (process.env.MINIFY_ENABLED) {
await fsPromises.writeFile(inputPath, result.code)
if (result.map) await fsPromises.writeFile(inputPath + ".map", result.map)
}
results.push({
path: inputPath.slice(2),
size: content.length,
compressed: result.code.length,
"% reduction": Number.parseFloat((100 - (result.code.length / content.length * 100)).toFixed(1))
})
}
const generateJSOptions = (retain = []) => ({
compress: {
...defaultOptions.compress,
top_retain: retain
},
mangle: {
reserved: retain
}
})
const minify = async () => {
await minifyFile("./webpage/attachment.js", generateJSOptions(["Attachment"]))
await minifyFile("./webpage/audio.js", generateJSOptions(["Audio"]))
await minifyFile("./webpage/channel.js", generateJSOptions(["Channel"]))
await minifyFile("./webpage/component.js", generateJSOptions(["Component"]))
await minifyFile("./webpage/connections.js", generateJSOptions())
await minifyFile("./webpage/contextmenu.js", generateJSOptions(["Contextmenu"]))
await minifyFile("./webpage/dialog.js", generateJSOptions(["Dialog"]))
await minifyFile("./webpage/direct.js", generateJSOptions(["Group", "Direct"]))
await minifyFile("./webpage/embed.js", generateJSOptions(["Embed"]))
await minifyFile("./webpage/emoji.js", generateJSOptions(["Emoji"]))
await minifyFile("./webpage/emojis.js", generateJSOptions(["emojis"]))
await minifyFile("./webpage/guild.js", generateJSOptions(["Guild"]))
await minifyFile("./webpage/index.js", generateJSOptions(["requestTestNotif"]))
await minifyFile("./webpage/invite.js", generateJSOptions())
await minifyFile("./webpage/infiniteScroller.js", generateJSOptions(["InfiniteScroller"]))
await minifyFile("./webpage/localuser.js", generateJSOptions(["LocalUser"]))
await minifyFile("./webpage/login.js", generateJSOptions(["getBulkInfo", "getBulkUsers", "handleEndpoint", "adduser", "getAPIURLs", "checkInstance", "setTheme"]))
await minifyFile("./webpage/markdown.js", generateJSOptions(["MarkDown"]))
await minifyFile("./webpage/member.js", generateJSOptions(["Member"]))
await minifyFile("./webpage/message.js", generateJSOptions(["Message"]))
await minifyFile("./webpage/permissions.js", generateJSOptions(["Permissions"]))
await minifyFile("./webpage/register.js", generateJSOptions())
await minifyFile("./webpage/role.js", generateJSOptions(["Role", "RoleList", "PermissionToggle"]))
await minifyFile("./webpage/service.js", generateJSOptions())
await minifyFile("./webpage/settings.js", generateJSOptions(["Settings", "Form", "Options", "Buttons"]))
await minifyFile("./webpage/snowflake.js", generateJSOptions(["SnowFlake"]))
await minifyFile("./webpage/user.js", generateJSOptions(["User"]))
await minifyFile("./webpage/style.css")
await minifyFile("./webpage/manifest.json")
results.push({
path: "= Total",
size: results.reduce((acc, cur) => acc + cur.size, 0),
compressed: results.reduce((acc, cur) => acc + cur.compressed, 0),
"% reduction": Number.parseFloat((100 - (results.reduce((acc, cur) => acc + cur.compressed, 0) / results.reduce((acc, cur) => acc + cur.size, 0) * 100)).toFixed(1))
})
console.table(results.sort((a, b) => a["% reduction"] - b["% reduction"]))
}
minify()