Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Make Vite macroable #44198

Merged
merged 1 commit into from
Sep 19, 2022
Merged

[9.x] Make Vite macroable #44198

merged 1 commit into from
Sep 19, 2022

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Sep 18, 2022

This PR makes the Vite class Macroable. This is rather nice for creating aliases for the Vite class to match those in your JS config.

Documentation PR: laravel/docs#8232

In JS land, configuration:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
 
export default defineConfig({
    plugins: [
        laravel(/* ... */),
    ],
    resolve: {
        alias: {
            'image': '/resources/images',
            'page': '/resources/js/Pages',
        },
    },
});

Usage:

import Profile from '@image/profile.png'

In Laravel land, configuration:

<?php
 
namespace App\Providers;
 
use Illuminate\Support\Vite;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Vite::macro('image', fn ($asset) => $this->asset("resources/images/{$asset}"));
    }
}

Usage:

<img src="{{ Vite::image('profile.png') }}" ... >

You can also make more complicated alises based on things like the apps environment:

Vite::macro('localeAsset', function ($asset) {
    $locale = config('app.locale');

    // resources/jp/images/*
    // resources/en/images/*
    // resources/fr/images/*
    return $this->asset("resources/{$locale}/images/{$asset}");
});
<img src="{{ Vite::localeAsset('logo.png') }}" alt="...">

@timacdonald
Copy link
Member Author

timacdonald commented Sep 19, 2022

Using this approach, in userland you could also create your own internal standard to share aliases between you JS and you PHP.

e.g. you could have vite.alias.json

{
    "image": "resources/images"
}

And then read this file from your vite.config.js and also you service provider to generate the aliases dynamically. Of course if you needed loginc in here you could also come up with your own solution, such as string replacement:

{
    "image": "resources/{ENV:LOCALE}/images"
}

Then you could do a string replacement for ENV:LOCALE => `config('app.locale') and the same kinda thing in your JS.

@taylorotwell taylorotwell merged commit b9b5c5c into laravel:9.x Sep 19, 2022
@timacdonald timacdonald deleted the macro-vite branch September 19, 2022 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants