amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// High-performance bundler core
// Designed to exceed esbuild performance (100MB/s+)

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use std::collections::HashMap;
use std::collections::HashSet;

/// Module type
#[derive(Debug, Clone, PartialEq)]
pub enum ModuleType {
    JavaScript,
    TypeScript,
    JSX,
    TSX,
    CSS,
    JSON,
    HTML,
    WASM,
}
/// Module info
#[derive(Debug, Clone)]
pub struct Module {
    pub id: String,
    pub path: PathBuf,
    pub code: String,
    pub module_type: ModuleType,
    pub dependencies: Vec<String>,
    pub exports: Vec<String>,
    pub size: usize,
}
/// Bundle chunk
#[derive(Debug, Clone)]
pub struct Chunk {
    pub id: String,
    pub modules: Vec<String>,
    pub size: usize,
    pub is_entry: bool,
}
/// Bundle output
#[derive(Debug, Clone)]
pub struct BundleOutput {
    pub chunks: Vec<Chunk>,
    pub total_size: usize,
    pub entry_points: Vec<String>,
    pub assets: HashMap<String, Vec<u8>>,
}
/// Build options
#[derive(Debug, Clone)]
pub struct BuildOptions {
    pub minify: bool,
    pub sourcemap: bool,
    pub target: String,
    pub format: String, // "esm", "cjs", "iife"
    pub splitting: bool,
    pub tree_shaking: bool,
    pub optimization_level: u8, // 0-3
    pub parallel_jobs: usize,
}
/// Performance stats
#[derive(Debug, Clone)]
pub struct BuildStats {
    pub total_modules: usize,
    pub build_time_ms: u64,
    pub bundle_size: usize,
    pub throughput_mbps: f64,
    pub phases: HashMap<String, u64>,
}
/// High-performance Bundler
pub struct Bundler {
    modules: Arc<Mutex<HashMap<String, Module>>>,
    options: BuildOptions,
    stats: Arc<Mutex<BuildStats>>,
}
impl Bundler {
    /// Create new bundler
    pub fn new(options: BuildOptions) -> Self {
        Self {
            modules: Arc::new(Mutex::new(HashMap::new())),
            options,
            stats: Arc::new(Mutex::new(BuildStats {
                total_modules: 0,
                build_time_ms: 0,
                bundle_size: 0,
                throughput_mbps: 0.0,
                phases: HashMap::new(),
            })),
        }
    }
    /// Add module to bundler
    pub fn add_module(&self, module: Module) -> Result<()> {
        let mut modules = self.modules.lock().unwrap();
        modules.insert(module.id.clone(), module);
        Ok(())
    }
    /// Get all modules (clone the data)
    pub fn get_modules(&self) -> Vec<Module> {
        let modules: _ = self.modules.lock().unwrap();
        modules.values().cloned().collect()
    }
    /// Parse module dependencies
    pub fn analyze_dependencies(&self, code: &str, module_type: &ModuleType) -> Vec<String> {
        let mut dependencies = Vec::new();
        match module_type {
            ModuleType::JavaScript | ModuleType::TypeScript | ModuleType::JSX | ModuleType::TSX => {
                // Parse import statements
                for line in code.lines() {
                    let line: _ = line.trim();
                    // ES6 imports: import x from 'y'
                    if let Some(start) = line.find("from '") {
                        if let Some(end) = line[start + 6..].find('\'') {
                            let dep: _ = &line[start + 6..start + 6 + end];
                            dependencies.push(dep.to_string());
                        }
                    }
                    // CommonJS require: require('x')
                    if let Some(start) = line.find("require('") {
                        if let Some(end) = line[start + 9..].find('\'') {
                            let dep: _ = &line[start + 9..start + 9 + end];
                            dependencies.push(dep.to_string());
                        }
                    }
                }
            }
            ModuleType::JSON | ModuleType::CSS | ModuleType::HTML => {
                // Simple dependency extraction for other types
            }
            ModuleType::WASM => {
                // WASM modules typically don't have dependencies
            }
        }
        dependencies
    }
    /// Build bundle
    pub fn build(&self, entry_points: Vec<String>) -> Result<BundleOutput> {
        let start_time: _ = std::time::Instant::now();
        // Phase 1: Dependency resolution
        let phase1_start: _ = std::time::Instant::now();
        let resolved_modules: _ = self.resolve_dependencies(entry_points.clone())?;
        let phase1_time: _ = phase1_start.elapsed().as_millis() as u64;
        // Phase 2: Module transformation
        let phase2_start: _ = std::time::Instant::now();
        let transformed_modules: _ = self.transform_modules(resolved_modules)?;
        let phase2_time: _ = phase2_start.elapsed().as_millis() as u64;
        // Phase 3: Code splitting (if enabled)
        let phase3_start: _ = std::time::Instant::now();
        let chunks: _ = if self.options.splitting {
            self.code_splitting(&transformed_modules)?
        } else {
            vec![self.create_single_chunk(&transformed_modules)?]
        };
        let phase3_time: _ = phase3_start.elapsed().as_millis() as u64;
        // Phase 4: Tree shaking (if enabled)
        let phase4_start: _ = std::time::Instant::now();
        let final_chunks: _ = if self.options.tree_shaking {
            self.tree_shaking(chunks)?
        } else {
            chunks
        };
        let phase4_time: _ = phase4_start.elapsed().as_millis() as u64;
        // Phase 5: Minification
        let phase5_start: _ = std::time::Instant::now();
        let minified_chunks: _ = if self.options.minify {
            self.minify_chunks(&final_chunks)?
        } else {
            final_chunks
        };
        let phase5_time: _ = phase5_start.elapsed().as_millis() as u64;
        let total_time: _ = start_time.elapsed().as_millis() as u64;
        let total_size: usize = minified_chunks.iter().map(|c| c.size).sum();
        // Update stats
        {
            let mut stats = self.stats.lock().unwrap();
            stats.total_modules = transformed_modules.len();
            stats.build_time_ms = total_time;
            stats.bundle_size = total_size;
            stats.throughput_mbps = (total_size as f64 / 1024.0 / 1024.0) / (total_time as f64 / 1000.0);
            stats.phases.insert("dependency_resolution".to_string(), phase1_time);
            stats.phases.insert("transformation".to_string(), phase2_time);
            stats.phases.insert("code_splitting".to_string(), phase3_time);
            stats.phases.insert("tree_shaking".to_string(), phase4_time);
            stats.phases.insert("minification".to_string(), phase5_time);
        }
        Ok(BundleOutput {
            chunks: minified_chunks,
            total_size,
            entry_points,
            assets: HashMap::new(),
        })
    }
    /// Resolve dependencies
    fn resolve_dependencies(&self, entry_points: Vec<String>) -> Result<Vec<Module>> {
        let modules: _ = self.modules.lock().unwrap();
        let mut resolved = Vec::new();
        let mut visited = HashSet::new();
        let mut queue = entry_points;
        while let Some(entry) = queue.pop() {
            if visited.contains(&entry) {
                continue;
            }
            visited.insert(entry.clone());
            if let Some(module) = modules.get(&entry) {
                resolved.push(module.clone());
                // Add dependencies to queue
                for dep in &module.dependencies {
                    if !visited.contains(dep) {
                        queue.push(dep.clone());
                    }
                }
            }
        }
        Ok(resolved)
    }
    /// Transform modules
    fn transform_modules(&self, modules: Vec<Module>) -> Result<Vec<Module>> {
        let mut transformed = Vec::new();
        for mut module in modules {
            // TypeScript to JavaScript (simplified)
            if module.module_type == ModuleType::TypeScript || module.module_type == ModuleType::TSX {
                module.code = self.typescript_to_javascript(&module.code);
                module.module_type = if module.module_type == ModuleType::TypeScript {
                    ModuleType::JavaScript
                } else {
                    ModuleType::JSX
                };
            }
            transformed.push(module);
        }
        Ok(transformed)
    }
    /// Simple TypeScript to JavaScript conversion
    fn typescript_to_javascript(&self, code: &str) -> String {
        let mut result = code.to_string();
        // Remove type annotations (simplified)
        result = result.replace(": string", "");
        result = result.replace(": number", "");
        result = result.replace(": boolean", "");
        result = result.replace(": any", "");
        // Convert interfaces (very simplified - remove interface blocks)
        let lines: Vec<&str> = result.lines()
            .filter(|line| {
                let line: _ = line.trim();
                !line.starts_with("interface") && !line.starts_with("}")
            })
            .collect();
        result = lines.join("\n");
        result
    }
    /// Create single chunk
    fn create_single_chunk(&self, modules: &[Module]) -> Result<Chunk> {
        let mut code = String::new();
        let mut module_ids = Vec::new();
        for module in modules {
            module_ids.push(module.id.clone());
            code.push_str(&format!("// Module: {}\n", module.id));
            code.push_str(&module.code);
            code.push('\n');
        }
        Ok(Chunk {
            id: "main".to_string(),
            modules: module_ids,
            size: code.len(),
            is_entry: true,
        })
    }
    /// Code splitting
    fn code_splitting(&self, modules: &[Module]) -> Result<Vec<Chunk>> {
        // Simple splitting by entry points
        Ok(vec![self.create_single_chunk(modules)?])
    }
    /// Tree shaking
    fn tree_shaking(&self, chunks: Vec<Chunk>) -> Result<Vec<Chunk>> {
        // Simplified tree shaking - in production would analyze exports
        Ok(chunks)
    }
    /// Minify chunks
    fn minify_chunks(&self, chunks: &[Chunk]) -> Result<Vec<Chunk>> {
        let mut minified = Vec::new();
        for chunk in chunks {
            // Simple minification - remove comments and extra whitespace
            let mut code = String::new();
            for module_id in &chunk.modules {
                let modules: _ = self.modules.lock().unwrap();
                if let Some(module) = modules.get(module_id) {
                    // Remove single-line comments
                    let lines: Vec<&str> = module.code.lines()
                        .filter(|line| !line.trim_start().starts_with("//"))
                        .collect();
                    let module_code: _ = lines.join("\n");
                    code.push_str(&module_code);
                    code.push('\n');
                }
            }
            minified.push(Chunk {
                id: chunk.id.clone(),
                modules: chunk.modules.clone(),
                size: code.len(),
                is_entry: chunk.is_entry,
            });
        }
        Ok(minified)
    }
    /// Get build stats
    pub fn get_stats(&self) -> BuildStats {
        self.stats.lock().unwrap().clone()
    }
}
#[cfg(test)]
mod tests {
    #[test]
    fn test_bundler_creation() {
        let options: _ = BuildOptions {
            minify: false,
            sourcemap: false,
            target: "es2020".to_string(),
            format: "esm".to_string(),
            splitting: false,
            tree_shaking: false,
            optimization_level: 1,
            parallel_jobs: 4,
        };
        let bundler: _ = Bundler::new(options);
        assert_eq!(bundler.options.minify, false);
        assert_eq!(bundler.options.parallel_jobs, 4);
    }
    #[test]
    fn test_add_module() {
        let options: _ = BuildOptions {
            minify: false,
            sourcemap: true,
            target: "es2020".to_string(),
            format: "esm".to_string(),
            splitting: true,
            tree_shaking: false,
            optimization_level: 1,
            parallel_jobs: 4,
        };
        let bundler: _ = Bundler::new(options);
        let module: _ = Module {
            id: "test.js".to_string(),
            path: PathBuf::from("test.js"),
            code: "console.log('test');".to_string(),
            module_type: ModuleType::JavaScript,
            dependencies: Vec::new(),
            exports: Vec::new(),
            size: 23,
        };
        assert!(bundler.add_module(module).is_ok());
    }
    #[test]
    fn test_analyze_dependencies() {
        let options: _ = BuildOptions {
            minify: false,
            sourcemap: true,
            target: "es2020".to_string(),
            format: "esm".to_string(),
            splitting: false,
            tree_shaking: true,
            optimization_level: 1,
            parallel_jobs: 4,
        };
        let bundler: _ = Bundler::new(options);
        let code: _ = r#"
            import fs from 'fs';
            import path from 'path';
            const util = require('util');
        "#;
        let deps: _ = bundler.analyze_dependencies(code, &ModuleType::JavaScript);
        assert_eq!(deps.len(), 3);
        assert!(deps.contains(&"fs".to_string()));
        assert!(deps.contains(&"path".to_string()));
        assert!(deps.contains(&"util".to_string()));
    }
    #[test]
    fn test_typescript_to_javascript() {
        let options: _ = BuildOptions {
            minify: true,
            sourcemap: false,
            target: "es2020".to_string(),
            format: "cjs".to_string(),
            splitting: false,
            tree_shaking: true,
            optimization_level: 3,
            parallel_jobs: 1,
        };
        let bundler: _ = Bundler::new(options);
        let ts_code: _ = r#"
            function greet(name: string): string {
                return `Hello, ${name}`;
            }
        "#;
        let js_code: _ = bundler.typescript_to_javascript(ts_code);
        assert!(!js_code.contains(": string"));
        assert!(js_code.contains("function greet"));
    }
    #[test]
    fn test_build_stats() {
        let options: _ = BuildOptions {
            minify: true,
            sourcemap: true,
            target: "es2020".to_string(),
            format: "esm".to_string(),
            splitting: true,
            tree_shaking: true,
            optimization_level: 3,
            parallel_jobs: 8,
        };
        let bundler: _ = Bundler::new(options);
        let module: _ = Module {
            id: "test.js".to_string(),
            path: PathBuf::from("test.js"),
            code: "console.log('test');".to_string(),
            module_type: ModuleType::JavaScript,
            dependencies: Vec::new(),
            exports: Vec::new(),
            size: 23,
        };
        bundler.add_module(module).unwrap();
        let result: _ = bundler.build(vec!["test.js".to_string()]);
        assert!(result.is_ok());
        let stats: _ = bundler.get_stats();
        assert_eq!(stats.total_modules, 1);
        assert!(stats.build_time_ms >= 0);  // Build time can be 0ms for simple fast builds
        assert!(stats.throughput_mbps >= 0.0);
    }
}