vite not recognizing app.js?
I'm using Laravel 10 in a Filament project with vite. I have pretty much the stock vite.config.js file:
I have this in my blade layout file in the head:
@vite(['resources/css/app.css', 'resources/js/app.js'])
In my resources/js/app.js
file I have this:
I've run npm run build, but I don't even get the simple "test" message from the console.log statement in app.js. It almost seems like vite isn't loading the app.js file at all. I should at the very least see the "test" message from app.js, right?
I upgraded the laravel-vite plugin to 1.0.1 and upgraded vite to 5.0.0, but that hasn't resolved it either. I've cleared the cache and hard refreshed. I've verified that jsConfetti is in my node_modules folder.
Also, I see no app.js in my sources in dev tools, which I'm thinking should be in the js folder?Solution:Jump to solution
I have this in my blade layout file in the head: @vite(['resources/css/app.css', 'resources/js/app.js'])Which layout? Did you override Filaments layout? You should use Filaments methods to add Javascript:...
10 Replies
@Jon Mason Are you sure Filament loads app.js? That's supposed to be where js for your non-Filament pages lives (eg web pages). You wouldn't want to use the same js build for both - things would get bloated. I haven’t tried using it in a panel so maybe someone else can confirm.
I register panel assets using Filament's asset system: https://filamentphp.com/docs/3.x/support/assets#registering-javascript-files.
The layout file isn't used by Filament either. That's just for views outside of Filament.
Solution
I have this in my blade layout file in the head: @vite(['resources/css/app.css', 'resources/js/app.js'])Which layout? Did you override Filaments layout? You should use Filaments methods to add Javascript: https://filamentphp.com/docs/3.x/support/assets#registering-javascript-files
This is because Filament uses Alpine.js and not regular js
Having just console.log wont work
JS is JS 🙂 Vanilla scripts will work fine if registered.
@Dennis Koch Do panels load
resources/js/app.js
at all? I'm pretty sure they don't : https://github.com/search?q=repo%3Afilamentphp%2Ffilament%20resources%2Fjs%2Fapp.js&type=code but just want to confirm I have that right.I somehow never saw this page in the documentation, so thank you for pointing it out..
I think I maybe marked this as answered too soon. It's obvious in the documentation how to register an asset like a custom js file or something into filament, but it's basically already looking in the public folder for those assets, so you'd still have to have a build step that would compile your assets into the public folder, and then register them with filament.
This even seems to be what should happen, as the vite.config.js references
/resources/js/app.js
file. If that file isn't used, why is it referenced, and/or why does it even exist? I'm just not understanding how I can use an external js library, which seems like something that probably a lot of people have done? Maybe I'm just completely misunderstanding how things work, but I feel like I understand the basic concept of the build step and compiling assets.
I've used alpine.js in an app before, albeit to a limited degree, but if I have to use alpine.js and I can't put anything in app.js, do I have to create an alpine component in order to use a javascript library?The
filament:assets
command will copy all registered assets from their src location into the public dir. Have a look for that in the docs.
If you want to minify your js before running that command you'll have to use your own build / minification process. I don't bother - your webserver will compress the files before serving them anyway which makes whitespace pretty much irrelevant for vanilla js. If you're building things with a framework you'll need to figure out how to configure an additional input / output in Vite.
resources/js/app.js
is not for your backend Filament admin panel. It's for your frontend (eg. website). If your app is only using Filament panels, just ignore that file.All the info you need is in the docs: https://filamentphp.com/docs/3.x/support/assets
Second paragraph talks about how assets are published into the public folder
ok, I got it working, thanks for that. The part I'm still not getting is lazy loading it so it doesn't load on every page. I've registered it in my service provider:
and then I'm gathering that I also need to have it on my div in my blade file:
<div x-data="{}" x-load-js="[@js(\Filament\Support\Facades\FilamentAsset::getScriptSrc('jsConfetti'))]"> </div>
When I do that, it gives me a reference error. I can see that the library is in my sources in dev tools, so it's still loading. Am I referencing the name of the plugin incorrectly, or am I misunderstanding the docs?