motomedialab / laravel-vite-helper
A helper method to generate absolute asset URL's to Vite assets
Installs: 452 554
Dependents: 2
Suggesters: 0
Security: 0
Stars: 28
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.5.8|^9.5.21|^10.0.7|^10.5|^11.0
This package is auto-updated.
Last update: 2024-11-17 14:02:47 UTC
README
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 mix 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 the package via composer:
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); // the final argument allows you to force disable hot server mode and always return the manifest path vite('resources/css/app.css', 'build', true, true); // you can also use named arguments as below vite('resources/css/app.css', buildDirectory: 'dist', relative: true, hotServer: true); // and even supply an array to get an array of paths/URLs vite(['resources/css/app.css', 'resources/js/app.js']);
Mocking in your own tests
Using this helper may cause some tests that directly interact with your web pages to break if you don't have the compiled assets available (e.g. in a CI/CD flow).
To overcome this, there's a MocksViteHelper
trait that can be used within your tests:
class ExampleTest extends TestCase { use MocksViteHelper; public function a_request_to_a_view_using_vite_helper() { $this->withoutViteHelper(); $response = $this->get('/'); $response->assertStatus(200); } public function restore_vite_helper_functionality() { $this->withViteHelper(); $response = $this->get('/'); $response->assertStatus(500); } }
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email chris@motocom.co.uk instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.