Registering JavaScript File that imports from moment-timezone package in Laravel 11 project #15278
PackagePanel builder Package Versionv3.2.132 How can we help you?AppServiceProvider.php // ...
/**
* Bootstrap any application services.
*/
public function boot(): void
{
FilamentAsset::register([
Js::make('timezone', base_path('resources/js/timezone.js')),
]);
}
// ...timezone.jsimport moment from "moment-timezone";
const timezone = moment.tz.guess();
document.cookie = `timezone=${timezone};`;
console.log("Timezone set to: ", timezone);Expected BehaviorI run When I refresh a Filament page, I want it to run the code in What Actually HappensAt
But I don't know what the problem is. |
Replies: 1 comment
|
Hello, SolutionYou need to run the Vite build process first: Use Vite::asset() to get the URL of the compiled asset: <?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Filament\Support\Facades\FilamentAsset;
use Illuminate\Support\Facades\Vite;
use Filament\Support\Assets\Js;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
FilamentAsset::register([
Js::make('timezone', Vite::asset('resources/js/timezone.js')),
]);
}
} |
Hello,
php artisan filament:assetsonly copies the selected file to the public directory. However, if you use import statement in your JavaScript code, it will not work.Solution
You need to run the Vite build process first:
Use Vite::asset() to get the URL of the compiled asset: