-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-minified.js
More file actions
84 lines (71 loc) · 2.48 KB
/
Copy pathbuild-minified.js
File metadata and controls
84 lines (71 loc) · 2.48 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
const fs = require('fs');
const path = require('path');
const { minify } = require('terser');
async function minifyFile(inputPath, outputPath) {
try {
const code = fs.readFileSync(inputPath, 'utf8');
const result = await minify(code, {
compress: {
drop_console: false, // Keep console logs for debugging
drop_debugger: true,
passes: 2
},
mangle: {
toplevel: false,
keep_classnames: true, // Keep class names for better debugging
keep_fnames: true // Keep function names
},
format: {
comments: false // Remove all comments
},
sourceMap: {
filename: path.basename(outputPath),
url: path.basename(outputPath) + '.map'
}
});
if (result.error) {
throw result.error;
}
// Write minified file
fs.writeFileSync(outputPath, result.code);
console.log(`✓ Created ${outputPath}`);
// Write source map if generated
if (result.map) {
fs.writeFileSync(outputPath + '.map', result.map);
console.log(`✓ Created ${outputPath}.map`);
}
// Calculate size reduction
const originalSize = fs.statSync(inputPath).size;
const minifiedSize = fs.statSync(outputPath).size;
const reduction = ((originalSize - minifiedSize) / originalSize * 100).toFixed(1);
console.log(` Size reduced by ${reduction}% (${originalSize} → ${minifiedSize} bytes)`);
} catch (error) {
console.error(`✗ Error minifying ${inputPath}:`, error.message);
process.exit(1);
}
}
async function buildMinified() {
console.log('Building minified versions...\n');
// Ensure dist directory exists
if (!fs.existsSync('dist')) {
console.error('Error: dist directory not found. Run "npm run build" first.');
process.exit(1);
}
// Minify ES6 module
await minifyFile(
'dist/HTMLTableKit.js',
'dist/HTMLTableKit.min.js'
);
console.log(''); // Empty line for spacing
// Minify standalone version
await minifyFile(
'dist/HTMLTableKit.standalone.js',
'dist/HTMLTableKit.standalone.min.js'
);
console.log('\n✅ Minification complete!');
}
// Run the build
buildMinified().catch(error => {
console.error('Build failed:', error);
process.exit(1);
});