forked from session-foundation/session-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminify.js
More file actions
184 lines (162 loc) · 5.32 KB
/
minify.js
File metadata and controls
184 lines (162 loc) · 5.32 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
/* eslint-disable no-console */
/**
* Minify all JS files in ts/ directory using Terser
* Usage: node build/minify.js [--no-mangle] [--keep-console]
*/
const fs = require('fs');
const os = require('os');
const { globSync } = require('glob');
const { minify } = require('terser');
// ANSI colors
const c = code => (process.env.NO_COLOR ? '' : `\x1b[${code}m`);
const green = c(32);
const yellow = c(33);
const dim = c(2);
const reset = c(0);
// Parse CLI args
const args = process.argv.slice(2);
const noMangle = args.includes('--no-mangle');
const keepConsole = args.includes('--keep-console');
const skipMinify = process.env.SKIP_MINIFY === '1';
if (skipMinify) {
console.log(`${yellow}Skipping minification (SKIP_MINIFY=1)${reset}`);
process.exit(0);
}
function formatBytes(bytes) {
if (bytes < 1024) {
return `${bytes} B`;
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)} KB`;
}
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
async function main() {
const startTime = Date.now();
const files = globSync('app/ts/**/*.js', { ignore: ['app/ts/**/*.min.js'] });
if (files.length === 0) {
console.log(`${yellow}No files found to minify${reset}`);
return;
}
console.log(`${dim}Minifying ${files.length} files...${reset}`);
let totalInputSize = 0;
let totalOutputSize = 0;
let errors = 0;
/** @type {import('terser').MinifyOptions} */
const terserOptions = {
// Electron 34 = Chromium 132, supports ES2024+
ecma: 2024,
module: true,
compress: {
ecma: 2024,
passes: 2, // more than 2 passes doesn't seem to improve anything
dead_code: true,
drop_debugger: true,
drop_console: !keepConsole ? ['log', 'debug', 'info'] : false,
// Aggressive optimizations safe for modern V8
arrows: true, // Convert functions to arrows where safe
arguments: true, // Replace arguments[i] with named params
booleans_as_integers: false, // Keep booleans as booleans (clearer)
booleans: true,
collapse_vars: true,
comparisons: true,
computed_props: true,
conditionals: true,
dead_code: true,
directives: true,
evaluate: true,
expression: false,
hoist_funs: true,
hoist_props: true, // Hoist properties from objects
hoist_vars: false, // Don't hoist vars (can break things)
if_return: true,
inline: 3, // Aggressive function inlining
join_vars: true,
keep_fnames: noMangle,
keep_classnames: noMangle,
loops: true,
negate_iife: true,
properties: true, // Rewrite property access
reduce_funcs: true,
reduce_vars: true,
sequences: true,
side_effects: true, // Drop side-effect-free code
switches: true,
toplevel: false, // Don't touch top-level (module exports)
typeofs: true,
unsafe_arrows: true, // Safe for Electron
unsafe_methods: true, // Safe for Electron
unsafe_proto: true, // Safe for Electron
unused: true,
},
mangle: noMangle
? false
: {
toplevel: false, // Preserve module exports
eval: false, // Don't mangle when eval is present
properties: false, // Don't mangle properties (can break things)
},
format: {
ecma: 2024,
comments: false,
webkit: false, // No Safari workarounds needed
wrap_func_args: false,
},
};
// Process files in parallel with concurrency based on CPU cores
const CONCURRENCY = os.cpus().length;
const chunks = [];
for (let i = 0; i < files.length; i += CONCURRENCY) {
chunks.push(files.slice(i, i + CONCURRENCY));
}
let processed = 0;
for (const chunk of chunks) {
await Promise.all(
chunk.map(async file => {
try {
const inputCode = fs.readFileSync(file, 'utf-8');
const inputSize = Buffer.byteLength(inputCode, 'utf-8');
totalInputSize += inputSize;
const result = await minify(inputCode, terserOptions);
if (result.code) {
fs.writeFileSync(file, result.code, 'utf-8');
const outputSize = Buffer.byteLength(result.code, 'utf-8');
totalOutputSize += outputSize;
} else {
// No output means no change needed
totalOutputSize += inputSize;
}
} catch (err) {
console.error(`\n${yellow}Failed to minify ${file}: ${err.message}${reset}`);
errors++;
// Keep original file, count its size
try {
const size = fs.statSync(file).size;
totalOutputSize += size;
} catch {
// Ignore
}
} finally {
processed++;
process.stdout.write(`\r${dim}Minifying... ${processed}/${files.length}${reset}`);
}
})
);
}
// Clear progress line
process.stdout.write('\r\x1b[K');
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
const saved = totalInputSize - totalOutputSize;
const percent = ((saved / totalInputSize) * 100).toFixed(1);
console.log(
`${green}Minified ${formatBytes(totalInputSize)} → ${formatBytes(totalOutputSize)} (-${percent}%) [${files.length} files in ${elapsed}s]${reset}`
);
if (errors > 0) {
console.log(`${yellow}${errors} file(s) failed to minify${reset}`);
}
}
main().catch(err => {
console.error(err);
process.exit(1);
});