AssetManager in MoonShine provides a convenient way to manage CSS and JavaScript assets for your admin panel.
It supports various types of assets, including external files, inline code, and versioning.
AssetManager allows managing the loading order of assets.
We recommend using DI to start interacting with AssetManager, for the service is responsible through the interface MoonShine\Contracts\AssetManager\AssetManagerContract.
Also, MoonShine provides convenient methods to interact with AssetManager in different entities such as CrudResource, Page, Layout, Component and Field.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:1]
use MoonShine\AssetManager\Js;
// Add assets to the end
$assetManager->append([
Js::make('/js/last.js')
]);
// Add assets to the beginning
$assetManager->prepend([
Js::make('/js/first.js')
]);
// Add assets in the order of addition
$assetManager->add([
Js::make('/js/middle.js')
]);
namespaces
useMoonShine\AssetManager\Js;
// Add assets to the end
$assetManager->append([
Js::make('/js/last.js')
]);
// Add assets to the beginning
$assetManager->prepend([
Js::make('/js/first.js')
]);
// Add assets in the order of addition
$assetManager->add([
Js::make('/js/middle.js')
]);
use MoonShine\AssetManager\Js;
// Add assets to the end
$assetManager->append([
Js::make('/js/last.js')
]);
// Add assets to the beginning
$assetManager->prepend([
Js::make('/js/first.js')
]);
// Add assets in the order of addition
$assetManager->add([
Js::make('/js/middle.js')
]);
The append() method will always add assets before the main list from CrudResource, Page, Layout, Component, Field, while prepend() will add them after.
The add() method will depend on the lifecycle of the application. Suppose you are adding assets to ModelResource,
but before the page is displayed, Layout will be called, which will also add assets, thus the assets from Layout will be added last.
You can also use the helper moonshine()->getAssetManager()
AssetManager supports versioning of assets to manage caching,
by default it will use the version of MoonShine, but you can override it for a specific asset:
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:1]
use MoonShine\AssetManager\Js;
// Adding a version to an individual asset
Js::make('/js/app.js')->version('1.0.0');
// Result: /js/app.js?v=1.0.0
namespaces
useMoonShine\AssetManager\Js;
// Adding a version to an individual asset
Js::make('/js/app.js')->version('1.0.0');
// Result: /js/app.js?v=1.0.0
use MoonShine\AssetManager\Js;
// Adding a version to an individual asset
Js::make('/js/app.js')->version('1.0.0');
// Result: /js/app.js?v=1.0.0
Versioning automatically adds a v parameter to the asset URL. If the URL already contains query parameters, the version will be added with &.
// MoonShineServiceProvider
use MoonShine\AssetManager\Js;
use MoonShine\Contracts\AssetManager\AssetManagerContract;
use MoonShine\Contracts\Core\DependencyInjection\ConfiguratorContract;
use MoonShine\Contracts\Core\DependencyInjection\CoreContract;
public function boot(
CoreContract $core,
ConfiguratorContract $config,
AssetManagerContract $assets,
): void
{
$assets->add(Js::make('/js/app.js'));
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use Illuminate\Support\Facades\Vite;
use MoonShine\AssetManager\Js;
final class MoonShineLayout extends CompactLayout
{
protected function assets(): array
{
return [
Js::make(Vite::asset('resources/js/app.js'))
];
}
}
namespaces
useIlluminate\Support\Facades\Vite;
useMoonShine\AssetManager\Js;
finalclassMoonShineLayoutextendsCompactLayout
{
protectedfunctionassets():array
{
return [
Js::make(Vite::asset('resources/js/app.js'))
];
}
}
use Illuminate\Support\Facades\Vite;
use MoonShine\AssetManager\Js;
final class MoonShineLayout extends CompactLayout
{
protected function assets(): array
{
return [
Js::make(Vite::asset('resources/js/app.js'))
];
}
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\AssetManager\InlineJs;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
$this->getAssetManager()
->prepend(InlineJs::make('alert(1)'))
->append(Js::make('/js/app.js'));
}
namespaces
useMoonShine\AssetManager\InlineJs;
useMoonShine\AssetManager\Js;
protectedfunctiononLoad():void
{
$this->getAssetManager()
->prepend(InlineJs::make('alert(1)'))
->append(Js::make('/js/app.js'));
}
use MoonShine\AssetManager\InlineJs;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
$this->getAssetManager()
->prepend(InlineJs::make('alert(1)'))
->append(Js::make('/js/app.js'));
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:2]
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
parent::onLoad();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
namespaces
useMoonShine\AssetManager\Css;
useMoonShine\AssetManager\Js;
protectedfunctiononLoad():void
{
parent::onLoad();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
protected function onLoad(): void
{
parent::onLoad();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\Layout\Box;
Box::make()->addAssets([
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
]);
namespaces
useMoonShine\AssetManager\Css;
useMoonShine\AssetManager\Js;
useMoonShine\UI\Components\Layout\Box;
Box::make()->addAssets([
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
]);
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\Layout\Box;
Box::make()->addAssets([
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
]);
When Creating a Component
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\MoonShineComponent;
final class MyComponent extends MoonShineComponent
{
/**
* @return list
*/
protected function assets(): array
{
return [
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
];
}
}
namespaces
useMoonShine\AssetManager\Css;
useMoonShine\AssetManager\Js;
useMoonShine\UI\Components\MoonShineComponent;
finalclassMyComponentextendsMoonShineComponent
{
/**
* @returnlist<AssetElementContract>
*/
protectedfunctionassets():array
{
return [
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
];
}
}
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\MoonShineComponent;
final class MyComponent extends MoonShineComponent
{
/**
* @return list<AssetElementContract>
*/
protected function assets(): array
{
return [
Js::make('/js/custom.js'),
Css::make('/css/styles.css')
];
}
}
When Creating a Component via AssetManager
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:3]
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\MoonShineComponent;
final class MyComponent extends MoonShineComponent
{
protected function booted(): void
{
parent::booted();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
}
namespaces
useMoonShine\AssetManager\Css;
useMoonShine\AssetManager\Js;
useMoonShine\UI\Components\MoonShineComponent;
finalclassMyComponentextendsMoonShineComponent
{
protectedfunctionbooted():void
{
parent::booted();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
}
use MoonShine\AssetManager\Css;
use MoonShine\AssetManager\Js;
use MoonShine\UI\Components\MoonShineComponent;
final class MyComponent extends MoonShineComponent
{
protected function booted(): void
{
parent::booted();
$this->getAssetManager()
->add(Css::make('/css/app.css'))
->append(Js::make('/js/app.js'));
}
}