module

LuckyVite::AssetHelpers

Constants

ASSET_MANIFEST = {} of String => NamedTuple(file: String) | NamedTuple(file: String, css: Array(String))
CONFIG = {has_loaded_manifest: false}

Class methods

dynamic_asset(path : String) : String

Return the string path to an asset (allows string interpolation).

# In a page or component
# Will find the asset in `public/images/logo.a1b2c3d4.png`
img src: dynamic_asset("images/logo.png")

# Can also be used elsewhere by prepending LuckyVite::AssetHelpers
LuckyVite::AssetHelpers.dynamic_asset("images/logo.png")

NOTE: This method does not check assets at compile time. The asset path is found at runtime so it is possible the asset does not exist. Be sure to manually test that the asset is returned as expected.

Source

Macros

asset(path)

Return the string path to an asset.

# In a page or component:
# Will find the asset in `public/images/logo.a1b2c3d4.png`
img src: asset("images/logo.png")

# Can also be used elsewhere by prepending LuckyVite::AssetHelpers
LuckyVite::AssetHelpers.asset("images/logo.png")

Note that assets are checked at compile time so if it is not found, Lucky will let you know. It will also let you know if you had a typo and suggest an asset that is close to what you typed.

NOTE: This macro requires a StringLiteral. That means you cannot interpolate strings like this: asset("images/icon-#{modifier}.png"). instead use dynamic_asset if you need string interpolation.

Source
load_manifest(config_file = "")

Runs a macro which parses the manifest.json file generated by Vite.

# In src/app.cr comment out the native manifest loader and add:
LuckyVite::AssetHelpers.load_manifest

Note: Vite generates its manifest inside the assets dir.

Source
manifest_entry(path)

Safely gets an entry from the Vite manifest and raises a compile time error. It will also let you know if you had a typo and suggest an asset that is close to what you typed.

Source