Helper Method to Generate Absolute Asset URLs to Vite Assets

Helper Method to Generate Absolute Asset URLs to Vite Assets cover image

A super simple Laravel helper to fill the void that Laravel Mix left. Mix had a helper, aptly named mix() that would return an absolute URL to the mixed resource. With the introduction of Vite (as of Laravel 9.19), there is no equivalent for Vite, at least, until now.

This was originally submitted as a PR to the core Laravel framework but unfortunately wasn’t deemed as a necessary addition.

Installation

You can install this package via composer by running this command:

composer require motomedialab/laravel-vite-helper

Usage

The usage for this helper is extremely simple, and directly replaces Laravel’s mix() helper.

// will return the absolute compiled asset path for 'resources/css/app.css'
vite('resources/css/app.css');

// will return the absolute compiled asset path for
// 'resources/css/app.css' with custom build directory 'dist'
vite('resources/css/app.css', 'dist');

// the third argument enforces a relative path to be returned
vite('resources/css/app.css', 'build', true);

For more details and source code, Visit Github

Note

The expert team of Laravel developers at Codebrisk is always ready to execute even your boldest ideas. Our expert team can design and develop any type of custom CRM solution, SAAS app, or e-commerce app to meet our customer’s needs and transform our customer’s experiences. Get in touch with our team to discuss your bespoke ideas and learn more about the next steps to launching cooperation.

Written by

Rizwan Aslam

Founder and CEO of CodeBrisk, with years of experience building web applications with Laravel and leading a team of expert developers.

More about Rizwan
Book a free call
Book a call with CodeBrisk