wasm-slim 0.1.1

WASM bundle size optimizer
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Database of known heavy dependencies and their lighter alternatives
//!
//! Based on real-world WASM optimization case studies (Warp.dev).

use std::collections::HashMap;
use std::sync::OnceLock;

/// A known heavy dependency with metadata
#[derive(Debug, Clone)]
pub struct HeavyDependency {
    /// Typical size range in KB
    pub size_kb: (u32, u32),
    /// Percentage of typical bundle
    pub bundle_percent: Option<(u8, u8)>,
    /// Description of why it's heavy
    pub reason: &'static str,
    /// Recommended alternatives
    pub alternatives: Vec<DependencyAlternative>,
}

/// An alternative to a heavy dependency
#[derive(Debug, Clone)]
pub struct DependencyAlternative {
    /// Type of alternative
    pub alt_type: AlternativeType,
    /// Crate name (if replacement)
    pub crate_name: Option<&'static str>,
    /// Expected size in KB
    pub size_kb: Option<(u32, u32)>,
    /// Estimated savings percentage
    pub savings_percent: u8,
    /// Description
    pub description: &'static str,
}

/// Type of dependency alternative recommendation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AlternativeType {
    /// Replace with different crate
    Replacement,
    /// Minimize features with default-features = false
    FeatureMinimization,
    /// Split monolith into components
    Split,
    /// Make optional via feature flag
    Optional,
    /// Fix WASM-specific issue
    WasmFix,
}

/// Global database of heavy dependencies
pub static HEAVY_DEPS_DATABASE: OnceLock<HashMap<&'static str, HeavyDependency>> = OnceLock::new();

/// Initialize and return the heavy dependencies database
fn init_heavy_deps_database() -> HashMap<&'static str, HeavyDependency> {
    let mut db = HashMap::new();

    // swc_core - Known to be very large dependency
    db.insert(
        "swc_core",
        HeavyDependency {
            size_kb: (1000, 1500),
            bundle_percent: Some((30, 45)),
            reason: "Full compiler suite with all features",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::Split,
                crate_name: Some("swc_ecma_parser"),
                size_kb: Some((400, 600)),
                savings_percent: 60,
                description: "Split into minimal components (parser + AST only)",
            }],
        },
    );

    // printpdf
    db.insert(
        "printpdf",
        HeavyDependency {
            size_kb: (800, 1000),
            bundle_percent: Some((15, 25)),
            reason: "Full-featured PDF library with many formats",
            alternatives: vec![
                DependencyAlternative {
                    alt_type: AlternativeType::Replacement,
                    crate_name: Some("pdf-writer"),
                    size_kb: Some((200, 400)),
                    savings_percent: 50,
                    description: "Lightweight PDF writer for simple use cases",
                },
                DependencyAlternative {
                    alt_type: AlternativeType::Replacement,
                    crate_name: Some("lopdf"),
                    size_kb: Some((400, 600)),
                    savings_percent: 30,
                    description: "PDF library with minimal features (no rayon)",
                },
            ],
        },
    );

    // lopdf with rayon
    db.insert(
        "lopdf",
        HeavyDependency {
            size_kb: (400, 800),
            bundle_percent: Some((10, 20)),
            reason: "Default includes rayon (doesn't work in WASM)",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::FeatureMinimization,
                crate_name: None,
                size_kb: Some((400, 600)),
                savings_percent: 25,
                description: "Disable rayon and unused features",
            }],
        },
    );

    // tokio
    db.insert(
        "tokio",
        HeavyDependency {
            size_kb: (500, 1000),
            bundle_percent: Some((15, 30)),
            reason: "Async runtime doesn't work in WASM",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::WasmFix,
                crate_name: Some("wasm-bindgen-futures"),
                size_kb: Some((50, 100)),
                savings_percent: 80,
                description: "WASM-specific async runtime",
            }],
        },
    );

    // async-std
    db.insert(
        "async-std",
        HeavyDependency {
            size_kb: (400, 800),
            bundle_percent: Some((10, 25)),
            reason: "Async runtime doesn't work in WASM",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::WasmFix,
                crate_name: Some("wasm-bindgen-futures"),
                size_kb: Some((50, 100)),
                savings_percent: 80,
                description: "WASM-specific async runtime",
            }],
        },
    );

    // rustybuzz
    db.insert(
        "rustybuzz",
        HeavyDependency {
            size_kb: (400, 600),
            bundle_percent: Some((10, 15)),
            reason: "Full text shaping library",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::Optional,
                crate_name: None,
                size_kb: Some((0, 0)),
                savings_percent: 100,
                description: "Make optional via feature flag (removes from default build)",
            }],
        },
    );

    // chrono
    db.insert(
        "chrono",
        HeavyDependency {
            size_kb: (300, 500),
            bundle_percent: Some((8, 15)),
            reason: "Feature-rich date/time library",
            alternatives: vec![
                DependencyAlternative {
                    alt_type: AlternativeType::Replacement,
                    crate_name: Some("time"),
                    size_kb: Some((150, 250)),
                    savings_percent: 40,
                    description: "Lighter date/time library",
                },
                DependencyAlternative {
                    alt_type: AlternativeType::WasmFix,
                    crate_name: Some("js-sys"),
                    size_kb: Some((50, 100)),
                    savings_percent: 70,
                    description: "Use browser Date API (WASM only)",
                },
            ],
        },
    );

    // regex with unicode
    db.insert(
        "regex",
        HeavyDependency {
            size_kb: (300, 600),
            bundle_percent: Some((8, 15)),
            reason: "Large unicode tables included by default",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::FeatureMinimization,
                crate_name: None,
                size_kb: Some((100, 200)),
                savings_percent: 60,
                description: "Disable unicode support if not needed",
            }],
        },
    );

    // image
    db.insert(
        "image",
        HeavyDependency {
            size_kb: (400, 800),
            bundle_percent: Some((10, 20)),
            reason: "Includes all image format codecs by default",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::FeatureMinimization,
                crate_name: None,
                size_kb: Some((150, 300)),
                savings_percent: 50,
                description: "Enable only needed image formats",
            }],
        },
    );

    // getrandom
    db.insert(
        "getrandom",
        HeavyDependency {
            size_kb: (50, 100),
            bundle_percent: None,
            reason: "Default doesn't work in WASM (missing OS entropy)",
            alternatives: vec![DependencyAlternative {
                alt_type: AlternativeType::WasmFix,
                crate_name: None,
                size_kb: Some((50, 100)),
                savings_percent: 0,
                description: "Enable WASM support (required for functionality, not size)",
            }],
        },
    );

    db
}

/// Get information about a heavy dependency
pub fn get_heavy_dependency_info(name: &str) -> Option<&HeavyDependency> {
    HEAVY_DEPS_DATABASE
        .get_or_init(init_heavy_deps_database)
        .get(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_heavy_dependency_info_swc_core_has_split_alternatives() {
        let swc = get_heavy_dependency_info("swc_core").expect("should have swc_core info");
        assert!(!swc.alternatives.is_empty());
        assert!(swc
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::Split));
    }

    #[test]
    fn test_get_heavy_dependency_info_printpdf_has_replacement_alternatives() {
        let printpdf = get_heavy_dependency_info("printpdf").expect("should have printpdf info");
        // printpdf has Replacement alternatives (pdf-writer, lopdf)
        assert!(printpdf
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::Replacement));
        assert!(printpdf
            .alternatives
            .iter()
            .any(|a| a.crate_name == Some("pdf-writer")));
    }

    #[test]
    fn test_get_heavy_dependency_info_lopdf_has_lighter_alternative() {
        let lopdf = get_heavy_dependency_info("lopdf").expect("should have lopdf info");
        assert!(!lopdf.alternatives.is_empty());
        // lopdf has feature minimization alternative
        assert!(lopdf
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::FeatureMinimization));
    }

    #[test]
    fn test_get_heavy_dependency_info_tokio_has_wasm_alternative() {
        let tokio = get_heavy_dependency_info("tokio").expect("should have tokio info");
        // Should have WasmFix (wasm-bindgen-futures)
        assert!(tokio
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::WasmFix));
        assert!(tokio
            .alternatives
            .iter()
            .any(|a| a.crate_name == Some("wasm-bindgen-futures")));
    }

    #[test]
    fn test_get_heavy_dependency_info_async_std_has_wasm_alternative() {
        let async_std = get_heavy_dependency_info("async-std").expect("should have async-std info");
        // Should have WasmFix (wasm-bindgen-futures)
        assert!(async_std
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::WasmFix));
        assert!(async_std
            .alternatives
            .iter()
            .any(|a| a.crate_name == Some("wasm-bindgen-futures")));
    }

    #[test]
    fn test_get_heavy_dependency_info_rustybuzz_can_be_made_optional() {
        let rustybuzz = get_heavy_dependency_info("rustybuzz").expect("should have rustybuzz info");
        // Should be Optional type (make it optional via feature flag)
        assert!(rustybuzz
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::Optional));
        // Should achieve 100% savings when disabled
        assert!(rustybuzz
            .alternatives
            .iter()
            .any(|a| a.savings_percent == 100));
    }

    #[test]
    fn test_get_heavy_dependency_info_chrono_has_lighter_alternatives() {
        let chrono = get_heavy_dependency_info("chrono").expect("should have chrono info");
        // Should have replacement alternatives (time crate)
        assert!(chrono
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::Replacement));
    }

    #[test]
    fn test_get_heavy_dependency_info_regex_supports_feature_minimization() {
        let regex = get_heavy_dependency_info("regex").expect("should have regex info");
        // Should have FeatureMinimization (disable unicode)
        assert!(regex
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::FeatureMinimization));
        // Should save significant space (~60%)
        assert!(regex.alternatives.iter().any(|a| a.savings_percent >= 50));
    }

    #[test]
    fn test_get_heavy_dependency_info_image_supports_feature_minimization() {
        let image = get_heavy_dependency_info("image").expect("should have image info");
        // Should have feature minimization
        assert!(image
            .alternatives
            .iter()
            .any(|a| a.alt_type == AlternativeType::FeatureMinimization));
    }

    #[test]
    fn test_database_all_alternative_types_represented() {
        // Ensure all alternative types are used in the database
        let all_deps = vec![
            "swc_core",
            "printpdf",
            "lopdf",
            "tokio",
            "async-std",
            "rustybuzz",
            "chrono",
            "regex",
            "image",
            "getrandom",
        ];

        let mut found_types = std::collections::HashSet::new();
        for dep in all_deps {
            let info = get_heavy_dependency_info(dep).unwrap();
            for alt in &info.alternatives {
                found_types.insert(alt.alt_type);
            }
        }

        // Should have representation of different alternative types
        assert!(found_types.contains(&AlternativeType::FeatureMinimization));
        assert!(found_types.contains(&AlternativeType::WasmFix));
        // May also have Replacement, Split, Optional
    }

    #[test]
    fn test_database_size_ranges_consistent_with_savings() {
        // Alternative sizes should be smaller than original
        let all_deps = vec![
            "swc_core",
            "printpdf",
            "lopdf",
            "tokio",
            "async-std",
            "rustybuzz",
            "chrono",
            "regex",
            "image",
            "getrandom",
        ];

        for dep in all_deps {
            let info = get_heavy_dependency_info(dep).unwrap();
            let original_min = info.size_kb.0;

            for alt in &info.alternatives {
                if let Some((alt_min, alt_max)) = alt.size_kb {
                    // Alternative should generally be smaller
                    assert!(
                        alt_max <= original_min * 2,
                        "{} alternative size ({}-{}) not significantly smaller than original ({})",
                        dep,
                        alt_min,
                        alt_max,
                        original_min
                    );
                }
            }
        }
    }

    #[test]
    fn test_database_bundle_percentages_within_valid_range() {
        // Bundle percentages should be reasonable
        let all_deps = vec![
            "swc_core",
            "printpdf",
            "lopdf",
            "tokio",
            "async-std",
            "rustybuzz",
            "chrono",
            "regex",
            "image",
            "getrandom",
        ];

        for dep in all_deps {
            let info = get_heavy_dependency_info(dep).unwrap();
            if let Some((min_pct, max_pct)) = info.bundle_percent {
                assert!(
                    min_pct > 0 && min_pct <= 100,
                    "{} min bundle % must be 1-100",
                    dep
                );
                assert!(
                    max_pct >= min_pct && max_pct <= 100,
                    "{} max bundle % must be >= min and <= 100",
                    dep
                );
            }
        }
    }

    #[test]
    fn test_database_initialization_has_entries() {
        // Ensure database actually has entries
        let db = HEAVY_DEPS_DATABASE.get_or_init(init_heavy_deps_database);
        assert!(db.len() >= 10, "Database should have at least 10 entries");
    }
}