Ligonsker's avatar

Please help me understand how am I supposed to work with Vite

Currently the app has 2 entry points for the JS and CSS files in the blade layout file:

 @vite(['resources/css/app.css', 'resources/js/app.js'])

But, I have many other JS and CSS files inside /resources/js and /resources/css.

How am I supposed to deal with it?

Let's say I have another blade view that uses the above layout:

{{-- my_view.blade.php --}}
@extends('layouts.app')
@section('content')
@endsection

And I want to use some JS scripts and CSS files only for this page, what is the way to do it alongside Vite? Because currently the layout takes only the 2 main entry points.

What I did was to use glob to add all the css files and js files:

// app.js
import.meta.glob('../css/*.css', { eager: true });
import.meta.glob('../js/*.js', { eager: true });

But that's clearly not how I am supposed to work with vite, because right now, every Blade view that uses this layout loads all the CSS and all the JS files

So how am I supposed to let's say, add only myscript.js and mystyle.css to my_view.blade.php while using Vite best practices?

Thanks

0 likes
1 reply
aryanmalla19's avatar

You should not import all JS and CSS files globally because that defeats Vite’s purpose. Instead, create separate entrypoints like @vite(['resources/js/custom.js']). If that file is needed globally, include it in your shared layout. If it’s only needed on one page, include it using @push('scripts') inside that specific page.

Please or to participate in this conversation.