bertui 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -1
- package/src/build/image-optimizer.js +105 -105
- package/src/build.js +34 -60
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bertui",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Lightning-fast React dev server powered by Bun and Elysia",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -44,6 +44,9 @@
|
|
|
44
44
|
"url": "https://github.com/BunElysiaReact/BERTUI.git"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
+
"@jsquash/jpeg": "^1.6.0",
|
|
48
|
+
"@jsquash/png": "^3.1.1",
|
|
49
|
+
"@jsquash/webp": "^1.5.0",
|
|
47
50
|
"elysia": "^1.0.0",
|
|
48
51
|
"ernest-logger": "latest",
|
|
49
52
|
"lightningcss": "^1.30.2"
|
|
@@ -1,18 +1,52 @@
|
|
|
1
|
-
// bertui/src/build/image-optimizer.js
|
|
2
|
-
import { join, extname
|
|
3
|
-
import { existsSync, mkdirSync, readdirSync,
|
|
1
|
+
// bertui/src/build/image-optimizer.js - WASM-POWERED VERSION 🚀
|
|
2
|
+
import { join, extname } from 'path';
|
|
3
|
+
import { existsSync, mkdirSync, readdirSync, cpSync } from 'fs';
|
|
4
4
|
import logger from '../logger/logger.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* 🎯 WASM-powered image optimization using @jsquash
|
|
8
|
+
* Zero OS dependencies, pure JavaScript, blazing fast!
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Lazy-load WASM modules (only when needed)
|
|
12
|
+
let pngEncode, pngDecode;
|
|
13
|
+
let jpegEncode, jpegDecode;
|
|
14
|
+
let webpEncode, webpDecode;
|
|
15
|
+
|
|
16
|
+
async function initializePNG() {
|
|
17
|
+
if (!pngEncode) {
|
|
18
|
+
const { encode, decode } = await import('@jsquash/png');
|
|
19
|
+
pngEncode = encode;
|
|
20
|
+
pngDecode = decode;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function initializeJPEG() {
|
|
25
|
+
if (!jpegEncode) {
|
|
26
|
+
const { encode, decode } = await import('@jsquash/jpeg');
|
|
27
|
+
jpegEncode = encode;
|
|
28
|
+
jpegDecode = decode;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function initializeWebP() {
|
|
33
|
+
if (!webpEncode) {
|
|
34
|
+
const { encode, decode } = await import('@jsquash/webp');
|
|
35
|
+
webpEncode = encode;
|
|
36
|
+
webpDecode = decode;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Optimize images using WASM-powered codecs
|
|
42
|
+
* This is FAST and has ZERO OS dependencies! 🚀
|
|
9
43
|
*/
|
|
10
44
|
export async function optimizeImages(srcDir, outDir) {
|
|
11
|
-
const imageExtensions = ['.png', '.jpg', '.jpeg', '.webp', '.gif'];
|
|
45
|
+
const imageExtensions = ['.png', '.jpg', '.jpeg', '.webp', '.gif', '.svg'];
|
|
12
46
|
let optimized = 0;
|
|
13
47
|
let totalSaved = 0;
|
|
14
48
|
|
|
15
|
-
logger.info('🖼️ Optimizing images...');
|
|
49
|
+
logger.info('🖼️ Optimizing images with WASM codecs...');
|
|
16
50
|
|
|
17
51
|
async function processDirectory(dir, targetDir) {
|
|
18
52
|
const entries = readdirSync(dir, { withFileTypes: true });
|
|
@@ -35,10 +69,13 @@ export async function optimizeImages(srcDir, outDir) {
|
|
|
35
69
|
if (result) {
|
|
36
70
|
optimized++;
|
|
37
71
|
totalSaved += result.saved;
|
|
38
|
-
|
|
72
|
+
const savedPercent = ((result.saved / result.originalSize) * 100).toFixed(1);
|
|
73
|
+
logger.debug(
|
|
74
|
+
`✨ ${entry.name}: ${(result.originalSize / 1024).toFixed(1)}KB → ${(result.newSize / 1024).toFixed(1)}KB (-${savedPercent}%)`
|
|
75
|
+
);
|
|
39
76
|
}
|
|
40
77
|
} catch (error) {
|
|
41
|
-
logger.warn(
|
|
78
|
+
logger.warn(`⚠️ Failed to optimize ${entry.name}: ${error.message}`);
|
|
42
79
|
// Fallback: just copy the file
|
|
43
80
|
cpSync(srcPath, destPath);
|
|
44
81
|
}
|
|
@@ -50,15 +87,16 @@ export async function optimizeImages(srcDir, outDir) {
|
|
|
50
87
|
await processDirectory(srcDir, outDir);
|
|
51
88
|
|
|
52
89
|
if (optimized > 0) {
|
|
53
|
-
logger.success(
|
|
90
|
+
logger.success(
|
|
91
|
+
`✅ Optimized ${optimized} images (saved ${(totalSaved / 1024).toFixed(2)}KB total)`
|
|
92
|
+
);
|
|
54
93
|
}
|
|
55
94
|
|
|
56
95
|
return { optimized, saved: totalSaved };
|
|
57
96
|
}
|
|
58
97
|
|
|
59
98
|
/**
|
|
60
|
-
* Optimize a single image using
|
|
61
|
-
* Falls back to direct copy if optimization fails
|
|
99
|
+
* Optimize a single image using WASM codecs
|
|
62
100
|
*/
|
|
63
101
|
async function optimizeImage(srcPath, destPath) {
|
|
64
102
|
const ext = extname(srcPath).toLowerCase();
|
|
@@ -66,118 +104,80 @@ async function optimizeImage(srcPath, destPath) {
|
|
|
66
104
|
const originalSize = originalFile.size;
|
|
67
105
|
|
|
68
106
|
try {
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (ext === '.png' || ext === '.jpg' || ext === '.jpeg') {
|
|
74
|
-
// Use Bun's native image optimization
|
|
75
|
-
// This is fast because it uses native C libraries
|
|
76
|
-
const optimized = await optimizeWithBun(imageBuffer, ext);
|
|
77
|
-
|
|
78
|
-
if (optimized && optimized.byteLength < originalSize) {
|
|
79
|
-
await Bun.write(destPath, optimized);
|
|
80
|
-
const saved = originalSize - optimized.byteLength;
|
|
81
|
-
return { saved, originalSize, newSize: optimized.byteLength };
|
|
82
|
-
}
|
|
107
|
+
// For SVG and GIF, just copy (no optimization needed/supported)
|
|
108
|
+
if (ext === '.svg' || ext === '.gif') {
|
|
109
|
+
cpSync(srcPath, destPath);
|
|
110
|
+
return null;
|
|
83
111
|
}
|
|
84
112
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
return null;
|
|
88
|
-
} catch (error) {
|
|
89
|
-
// If anything fails, just copy the original
|
|
90
|
-
cpSync(srcPath, destPath);
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
113
|
+
// Read the original image
|
|
114
|
+
const originalBuffer = await originalFile.arrayBuffer();
|
|
94
115
|
|
|
95
|
-
|
|
96
|
-
* Optimize using Bun's native capabilities
|
|
97
|
-
* This is a placeholder - Bun doesn't have built-in image optimization yet
|
|
98
|
-
* We'll use a fast external library via Bun's FFI or shell commands
|
|
99
|
-
*/
|
|
100
|
-
async function optimizeWithBun(buffer, ext) {
|
|
101
|
-
try {
|
|
102
|
-
// For now, we'll use oxipng and mozjpeg via shell commands
|
|
103
|
-
// These are the FASTEST available options (Rust-based)
|
|
104
|
-
const tempInput = `/tmp/bertui_input_${Date.now()}${ext}`;
|
|
105
|
-
const tempOutput = `/tmp/bertui_output_${Date.now()}${ext}`;
|
|
106
|
-
|
|
107
|
-
await Bun.write(tempInput, buffer);
|
|
116
|
+
let optimizedBuffer;
|
|
108
117
|
|
|
109
118
|
if (ext === '.png') {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
119
|
+
await initializePNG();
|
|
120
|
+
|
|
121
|
+
// Decode → Re-encode with compression
|
|
122
|
+
const imageData = await pngDecode(originalBuffer);
|
|
123
|
+
|
|
124
|
+
// Encode with oxipng-level compression (quality 85, similar to oxipng -o 2)
|
|
125
|
+
optimizedBuffer = await pngEncode(imageData, {
|
|
126
|
+
quality: 85,
|
|
127
|
+
effort: 2 // 0-10, higher = better compression but slower
|
|
114
128
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (existsSync(tempOutput)) {
|
|
118
|
-
const optimized = await Bun.file(tempOutput).arrayBuffer();
|
|
119
|
-
// Cleanup
|
|
120
|
-
Bun.spawn(['rm', tempInput, tempOutput]);
|
|
121
|
-
return optimized;
|
|
122
|
-
}
|
|
129
|
+
|
|
123
130
|
} else if (ext === '.jpg' || ext === '.jpeg') {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
131
|
+
await initializeJPEG();
|
|
132
|
+
|
|
133
|
+
// Decode → Re-encode with quality 85 (mozjpeg-like quality)
|
|
134
|
+
const imageData = await jpegDecode(originalBuffer);
|
|
135
|
+
optimizedBuffer = await jpegEncode(imageData, { quality: 85 });
|
|
136
|
+
|
|
137
|
+
} else if (ext === '.webp') {
|
|
138
|
+
await initializeWebP();
|
|
139
|
+
|
|
140
|
+
// WebP optimization
|
|
141
|
+
const imageData = await webpDecode(originalBuffer);
|
|
142
|
+
optimizedBuffer = await webpEncode(imageData, { quality: 85 });
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
//
|
|
140
|
-
if (
|
|
141
|
-
|
|
145
|
+
// Only save if we actually reduced the size
|
|
146
|
+
if (optimizedBuffer && optimizedBuffer.byteLength < originalSize) {
|
|
147
|
+
await Bun.write(destPath, optimizedBuffer);
|
|
148
|
+
const saved = originalSize - optimizedBuffer.byteLength;
|
|
149
|
+
return { saved, originalSize, newSize: optimizedBuffer.byteLength };
|
|
150
|
+
}
|
|
142
151
|
|
|
152
|
+
// If optimization didn't help, just copy the original
|
|
153
|
+
cpSync(srcPath, destPath);
|
|
143
154
|
return null;
|
|
155
|
+
|
|
144
156
|
} catch (error) {
|
|
157
|
+
// If anything fails, just copy the original
|
|
158
|
+
logger.warn(`Optimization failed for ${srcPath.split('/').pop()}, copying original`);
|
|
159
|
+
cpSync(srcPath, destPath);
|
|
145
160
|
return null;
|
|
146
161
|
}
|
|
147
162
|
}
|
|
148
163
|
|
|
149
164
|
/**
|
|
150
|
-
* Check if optimization
|
|
165
|
+
* Check if optimization is available (always true with WASM! 🎉)
|
|
151
166
|
*/
|
|
152
167
|
export async function checkOptimizationTools() {
|
|
153
|
-
const tools = [];
|
|
154
|
-
|
|
155
168
|
try {
|
|
156
|
-
|
|
157
|
-
await
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
tools.push('mozjpeg');
|
|
168
|
-
}
|
|
169
|
-
} catch (e) {}
|
|
170
|
-
|
|
171
|
-
if (tools.length === 0) {
|
|
172
|
-
logger.warn('⚠️ No image optimization tools found. Install for better performance:');
|
|
173
|
-
logger.warn(' macOS: brew install oxipng mozjpeg');
|
|
174
|
-
logger.warn(' Ubuntu: apt install oxipng mozjpeg');
|
|
175
|
-
logger.warn(' Images will be copied without optimization.');
|
|
176
|
-
} else {
|
|
177
|
-
logger.success(`Found optimization tools: ${tools.join(', ')}`);
|
|
169
|
+
// Try to import the WASM modules
|
|
170
|
+
await import('@jsquash/png');
|
|
171
|
+
await import('@jsquash/jpeg');
|
|
172
|
+
await import('@jsquash/webp');
|
|
173
|
+
|
|
174
|
+
logger.success('✅ WASM image optimization available');
|
|
175
|
+
logger.info('📦 Using @jsquash (zero OS dependencies!)');
|
|
176
|
+
return ['png', 'jpeg', 'webp'];
|
|
177
|
+
} catch (error) {
|
|
178
|
+
logger.error('❌ WASM codecs not installed. Run: bun add @jsquash/png @jsquash/jpeg @jsquash/webp');
|
|
179
|
+
return [];
|
|
178
180
|
}
|
|
179
|
-
|
|
180
|
-
return tools;
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/**
|
|
@@ -198,7 +198,7 @@ export function copyImages(srcDir, outDir) {
|
|
|
198
198
|
if (!existsSync(destPath)) {
|
|
199
199
|
mkdirSync(destPath, { recursive: true });
|
|
200
200
|
}
|
|
201
|
-
processDirectory(srcPath,
|
|
201
|
+
processDirectory(srcPath, targetDir);
|
|
202
202
|
} else if (entry.isFile()) {
|
|
203
203
|
const ext = extname(entry.name).toLowerCase();
|
|
204
204
|
|
|
@@ -211,6 +211,6 @@ export function copyImages(srcDir, outDir) {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
processDirectory(srcDir, outDir);
|
|
214
|
-
logger.info(
|
|
214
|
+
logger.info(`📋 Copied ${copied} images without optimization`);
|
|
215
215
|
return copied;
|
|
216
216
|
}
|
package/src/build.js
CHANGED
|
@@ -3,6 +3,7 @@ import { existsSync, mkdirSync, rmSync, cpSync, readdirSync, statSync } from 'fs
|
|
|
3
3
|
import logger from './logger/logger.js';
|
|
4
4
|
import { buildCSS } from './build/css-builder.js';
|
|
5
5
|
import { loadEnvVariables, replaceEnvInCode } from './utils/env.js';
|
|
6
|
+
import { optimizeImages, checkOptimizationTools, copyImages } from './build/image-optimizer.js';
|
|
6
7
|
|
|
7
8
|
export async function buildProduction(options = {}) {
|
|
8
9
|
const root = options.root || process.cwd();
|
|
@@ -38,11 +39,20 @@ export async function buildProduction(options = {}) {
|
|
|
38
39
|
logger.info('Step 2: Building CSS with Lightning CSS...');
|
|
39
40
|
await buildAllCSS(root, outDir);
|
|
40
41
|
|
|
41
|
-
// ✅
|
|
42
|
-
logger.info('Step 3:
|
|
43
|
-
await
|
|
42
|
+
// ✅ NEW: Check if image optimization is available
|
|
43
|
+
logger.info('Step 3: Checking image optimization tools...');
|
|
44
|
+
const optimizationTools = await checkOptimizationTools();
|
|
44
45
|
|
|
45
|
-
logger.info('Step 4:
|
|
46
|
+
logger.info('Step 4: Copying and optimizing static assets...');
|
|
47
|
+
if (optimizationTools.length > 0) {
|
|
48
|
+
// Use WASM-powered optimization
|
|
49
|
+
await copyAllStaticAssets(root, outDir, true);
|
|
50
|
+
} else {
|
|
51
|
+
// Fallback: just copy images
|
|
52
|
+
await copyAllStaticAssets(root, outDir, false);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
logger.info('Step 5: Bundling JavaScript with Bun...');
|
|
46
56
|
const buildEntry = join(buildDir, 'main.js');
|
|
47
57
|
|
|
48
58
|
if (!existsSync(buildEntry)) {
|
|
@@ -85,7 +95,7 @@ export async function buildProduction(options = {}) {
|
|
|
85
95
|
|
|
86
96
|
logger.success('JavaScript bundled with tree-shaking');
|
|
87
97
|
|
|
88
|
-
logger.info('Step
|
|
98
|
+
logger.info('Step 6: Generating SEO-optimized HTML files...');
|
|
89
99
|
await generateProductionHTML(root, outDir, result, routes);
|
|
90
100
|
|
|
91
101
|
rmSync(buildDir, { recursive: true });
|
|
@@ -121,76 +131,42 @@ export async function buildProduction(options = {}) {
|
|
|
121
131
|
}
|
|
122
132
|
}
|
|
123
133
|
|
|
124
|
-
// ✅
|
|
125
|
-
async function copyAllStaticAssets(root, outDir) {
|
|
134
|
+
// ✅ UPDATED: Copy and optionally optimize static assets
|
|
135
|
+
async function copyAllStaticAssets(root, outDir, optimize = true) {
|
|
126
136
|
const publicDir = join(root, 'public');
|
|
127
137
|
const srcDir = join(root, 'src');
|
|
128
138
|
|
|
129
139
|
let assetsCopied = 0;
|
|
140
|
+
let assetsOptimized = 0;
|
|
130
141
|
|
|
131
142
|
// Copy from public/
|
|
132
143
|
if (existsSync(publicDir)) {
|
|
133
|
-
|
|
144
|
+
if (optimize) {
|
|
145
|
+
const result = await optimizeImages(publicDir, outDir);
|
|
146
|
+
assetsOptimized += result.optimized;
|
|
147
|
+
} else {
|
|
148
|
+
assetsCopied += copyImages(publicDir, outDir);
|
|
149
|
+
}
|
|
134
150
|
}
|
|
135
151
|
|
|
136
152
|
// Copy static assets from src/ (images, fonts, etc.)
|
|
137
153
|
if (existsSync(srcDir)) {
|
|
138
154
|
const assetsOutDir = join(outDir, 'assets');
|
|
139
155
|
mkdirSync(assetsOutDir, { recursive: true });
|
|
140
|
-
assetsCopied += await copyStaticAssetsFromDir(srcDir, assetsOutDir, 'src', true);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
logger.success(`Copied ${assetsCopied} static assets`);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// ✅ NEW FUNCTION: Recursively copy static assets
|
|
147
|
-
async function copyStaticAssetsFromDir(sourceDir, targetDir, label, skipStyles = false) {
|
|
148
|
-
const staticExtensions = [
|
|
149
|
-
'.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.avif', // Images
|
|
150
|
-
'.woff', '.woff2', '.ttf', '.otf', '.eot', // Fonts
|
|
151
|
-
'.mp4', '.webm', '.ogg', '.mp3', '.wav', // Media
|
|
152
|
-
'.pdf', '.zip', '.json', '.xml', '.txt' // Documents
|
|
153
|
-
];
|
|
154
|
-
|
|
155
|
-
let copiedCount = 0;
|
|
156
|
-
|
|
157
|
-
function copyRecursive(dir, targetBase) {
|
|
158
|
-
const entries = readdirSync(dir, { withFileTypes: true });
|
|
159
156
|
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (entry.isDirectory()) {
|
|
166
|
-
// Skip node_modules, .bertui, etc.
|
|
167
|
-
if (entry.name === 'node_modules' || entry.name.startsWith('.')) {
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Skip styles directory if requested
|
|
172
|
-
if (skipStyles && entry.name === 'styles') {
|
|
173
|
-
continue;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
mkdirSync(destPath, { recursive: true });
|
|
177
|
-
copyRecursive(srcPath, targetBase);
|
|
178
|
-
} else if (entry.isFile()) {
|
|
179
|
-
const ext = extname(entry.name);
|
|
180
|
-
|
|
181
|
-
// Copy static assets only
|
|
182
|
-
if (staticExtensions.includes(ext.toLowerCase())) {
|
|
183
|
-
mkdirSync(dirname(destPath), { recursive: true });
|
|
184
|
-
cpSync(srcPath, destPath);
|
|
185
|
-
logger.debug(`Copied ${label}/${relativePath}`);
|
|
186
|
-
copiedCount++;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
157
|
+
if (optimize) {
|
|
158
|
+
const result = await optimizeImages(srcDir, assetsOutDir);
|
|
159
|
+
assetsOptimized += result.optimized;
|
|
160
|
+
} else {
|
|
161
|
+
assetsCopied += copyImages(srcDir, assetsOutDir);
|
|
189
162
|
}
|
|
190
163
|
}
|
|
191
164
|
|
|
192
|
-
|
|
193
|
-
|
|
165
|
+
if (optimize && assetsOptimized > 0) {
|
|
166
|
+
logger.success(`🎨 Optimized ${assetsOptimized} images with WASM codecs`);
|
|
167
|
+
} else {
|
|
168
|
+
logger.success(`📋 Copied ${assetsCopied} static assets`);
|
|
169
|
+
}
|
|
194
170
|
}
|
|
195
171
|
|
|
196
172
|
async function buildAllCSS(root, outDir) {
|
|
@@ -447,7 +423,6 @@ async function compileBuildDirectory(srcDir, buildDir, root, envVars) {
|
|
|
447
423
|
code = replaceEnvInCode(code, envVars);
|
|
448
424
|
code = fixBuildImports(code, srcPath, outPath, root);
|
|
449
425
|
|
|
450
|
-
// ✅ FIX: Add React import if needed
|
|
451
426
|
if (usesJSX(code) && !code.includes('import React')) {
|
|
452
427
|
code = `import React from 'react';\n${code}`;
|
|
453
428
|
}
|
|
@@ -486,7 +461,6 @@ async function compileBuildFile(srcPath, buildDir, filename, root, envVars) {
|
|
|
486
461
|
|
|
487
462
|
let compiled = await transpiler.transform(code);
|
|
488
463
|
|
|
489
|
-
// ✅ FIX: Add React import if needed
|
|
490
464
|
if (usesJSX(compiled) && !compiled.includes('import React')) {
|
|
491
465
|
compiled = `import React from 'react';\n${compiled}`;
|
|
492
466
|
}
|