module
Lucky::AssetHelpers
Overview
Methods for returning the path to assets
These methods will return fingerprinted paths, check assets at compile time, and allow for setting a CDN.
For an in-depth guide check: https://luckyframework.org/guides/frontend/asset-handling
Direct including types
Defined in:
lucky/asset_helpers.crConstant Summary
-
ASSET_MANIFEST =
{} of String => String -
CONFIG =
{has_loaded_manifest: false}
Class Method Summary
-
.css_entry_points : Array(String)
Returns all the CSS entrypoints from the manifest.
Macro Summary
-
asset(path)
Returns the string path to an asset.
-
load_manifest(manifest_file = "", from = :bun)
Loads the asset manifest at compile time.
Instance Method Summary
-
#dynamic_asset(path : String) : String
Returns the string path to an asset (allows string interpolation).
Class Method Detail
Returns all the CSS entrypoints from the manifest.
NOTE This method is used by the CSS HMR implementation for Bun.
Macro Detail
Returns the string path to an asset.
# In a page or component:
# Will find the asset in `public/assets/images/logo.png`
img src: asset("images/logo.png")
# Can also be used elsewhere by prepending Lucky::AssetHelpers
Lucky::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-#{service_name}.png").
Instead use #dynamic_asset if you need string interpolation.
Loads the asset manifest at compile time.
Call this once in src/app.cr:
# Bun (default):
Lucky::AssetHelpers.load_manifest
# Laravel Mix:
Lucky::AssetHelpers.load_manifest(from: :mix)
# Vite:
Lucky::AssetHelpers.load_manifest(from: :vite)
# Custom manifest path:
Lucky::AssetHelpers.load_manifest("public/custom-manifest.json", from: :mix)
NOTE The custom manifest path is only considered by Mix or Vite. Bun's is
defined in the shared config/bun.json.
Instance Method Detail
Returns the string path to an asset (allows string interpolation).
# In a page or component
# Will find the asset in `public/assets/images/logo.png`
img src: dynamic_asset("images/logo.png")
# Can also be used elsewhere by prepending Lucky::AssetHelpers
Lucky::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.