asset!() macro, enabling automatic optimization, cache-busting, and type-safe asset references.
Asset Macro Basics
Theasset!() macro resolves and bundles assets at compile time:
- Compile-time path validation
- Automatic optimization
- Cache-busting via content hashing
- Type-safe references
How It Works
Compilation Flow
- Macro Expansion:
asset!()resolves path at compile time - Link Section: Embeds asset metadata in binary via linker symbol
- CLI Processing:
dxreads symbols, optimizes assets, updates paths - Runtime Resolution: Asset resolves to optimized bundled path
packages/manganis/manganis-macro/src/asset.rs:60-128
Link Section Generation
The macro creates two link sections for backward compatibility:packages/manganis/manganis-core/src/asset.rs:22-98
Asset Options
Image Assets
Optimize images with format conversion and resizing:ImageFormat::PngImageFormat::JpgImageFormat::Webp(recommended for web)ImageFormat::Avif(best compression, limited browser support)
packages/manganis/manganis-core/src/images.rs:22-243
CSS Assets
Optimize stylesheets:with_minify(bool): Minify CSS (default: true)with_preload(bool): Add preload hintwith_static_head(bool): Include in static HTML head
JavaScript Assets
with_minify(bool): Minify JavaScript (default: true)with_preload(bool): Add preload hintwith_static_head(bool): Include in static HTML head
Folder Assets
Bundle entire directories:CSS Modules
CSS modules provide scoped styling with compile-time class name generation:component.module.css):
notes/architecture/08-ASSETS.md:80-95
Asset Resolution
Assets resolve differently based on environment:Development Mode
Production Mode
packages/manganis/manganis-core/src/asset.rs:185-216
Advanced Usage
Optional Assets
Handle assets that may not exist:Dynamic Asset Loading
For runtime-determined assets, use standard file loading:asset!() macro since they’re not known at compile time.
Asset Variants
Generate multiple variants of the same asset:Cache Busting
Manganis automatically implements cache-busting:Controlling Hashing
Performance Optimization
Preloading Critical Assets
Image Format Optimization
Choose formats based on use case:Responsive Images
Generate multiple sizes:Platform-Specific Assets
Mobile Assets (Android/iOS)
Manganis integrates with mobile platform APIs:Web Assets
For web targets, assets are served from the assets directory:Troubleshooting
Asset Not Found
- Path is relative to crate root
- File exists at compile time
- Path is within crate directory (no
..escaping)
Hash Mismatch
If you see hash mismatches, ensure:- Using matching versions of
dxCLI andmanganis - Not stripping symbols from binary
- Not using
--no-mangleor similar flags
Memory Issues
If bundling many large assets:Best Practices
- Use
const: Define assets as constants for compile-time checking - Optimize Images: Always specify format options for images
- Preload Sparingly: Only preload critical above-the-fold assets
- CSS Modules: Use for component-scoped styles
- Cache Busting: Let manganis handle it automatically
- Development: Assets work without
dx serve, using source paths
Additional Resources
- Asset architecture:
notes/architecture/08-ASSETS.md - Manganis source:
packages/manganis/ - Asset types:
packages/manganis/manganis-core/src/