How to import CSS/JS files for Laravel Vite Builds
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Asset Imports: How to Use CSS/JS Files in Laravel Vite Builds
As a senior developer working within the Laravel ecosystem, managing frontend assets efficiently is crucial. When you integrate tools like Vite for asset bundling, you gain incredible performance benefits through features like code splitting and asset fingerprinting (cache busting). However, this process introduces a specific challenge: how do we reliably link those dynamically generated files—which contain unique hashes—back into our Blade views without manually managing the changing filenames?
This post dives deep into your specific scenario regarding importing CSS and JavaScript files built by Vite within a Laravel application. We will demystify the @vite directive and show you the correct, scalable way to handle asset linking.
Understanding the Vite Build Process
When you run your Vite build command (npm run build), Vite processes your source files (like SCSS or plain CSS) and outputs them into a specific directory, typically public/build/assets/. The filenames are intentionally hashed (e.g., top.d0g7acf3.css). This hashing is essential for cache busting: whenever you change the content of the file, the hash changes, forcing browsers to download the new version instead of relying on stale cached data.
Your vite.config.js confirms this setup:
import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import vue from '@vitejs/plugin-vue2'
export default defineConfig({
plugins: [
vue(),
laravel({
input: ['resources/sass/_top/top.scss'], // Vite watches this input
refresh: true
})
],
// ... other configurations
})
The key takeaway here is that the actual file names are generated during the build process and placed in the public directory, not directly referenced from your resources folder in a standard way.
The Solution: Leveraging the @vite Directive
The confusion often arises when developers try to manually construct the path based on the output hash. Fortunately, Laravel’s official integration with Vite provides an elegant abstraction layer designed precisely to solve this problem: the @vite Blade directive.
The @vite() function is a powerful helper provided by the Laravel Vite plugin that automatically inspects your vite.config.js, determines which entry points you have defined, and outputs the correct, current path for the compiled assets. You do not need to manually manage the hash; the directive handles the dynamic resolution for you.
Correct Implementation in Blade Files
Instead of trying to reference the exact hashed file directly (which is brittle), you should simply call the directive pointing to the entry point defined in your configuration. Based on your setup, where top.scss was the input, the resulting asset path is correctly resolved by Vite when using this helper:
<!-- Correct way to import assets using the @vite directive -->
@vite('resources/sass/_top/top.scss')
Or, if you are importing a specific entry point that has been built into the public directory structure (which is often cleaner):
<!-- If your Vite configuration points to an asset in the build folder -->
@vite('build/assets/top.d0g7acf3.css')
Note: While using the full hash works, relying on the entry point defined in input is generally safer and more maintainable, as it guarantees you are referencing a known source.
Why Other Methods Fail
Let's look at why your other attempts do not work:
Failing Example 1:
@vite('build/assets/top.css')
This fails because the filename insidepublic/build/assets/is dynamic (e.g.,top.d0g7acf3.css), and Vite needs context to know which file you mean. Simply referencing a static name will result in a 404 error unless that exact file exists, which it won't after every build.Failing Example 2:
@vite('resources/sass/_top/top.scss')
This fails because the@vitedirective is designed to compile and reference built assets located in thepublicdirectory, not source files inresources. It doesn't know how to perform the Vite compilation step itself in this context.
Conclusion: Best Practices for Laravel Vite Assets
To summarize, the core principle when working with Laravel Vite builds is to trust the tooling and use the provided abstractions.
- Rely on
@vite: Always use the@vite()Blade directive as your primary method for linking compiled assets. It ensures that you are referencing the correct, cache-busted file path regardless of what hash Vite generates. - Define Inputs Clearly: Ensure your
inputarray invite.config.jsaccurately reflects all the entry points you intend to use in your application. - Keep Assets Organized: Keep your source files neatly organized in
resources, and let Vite handle moving and hashing them into thepublic/builddirectory.
By adopting this approach, you ensure that your frontend assets are always linked correctly, efficiently, and robustly, aligning perfectly with modern Laravel development practices. For more insights into optimizing your Laravel stack, remember that resources like those provided by laravelcompany.com offer excellent guidance on structuring robust applications.