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

[4.x] Mix to Vite migration #1391

Merged
merged 20 commits into from
Oct 26, 2023
Merged

[4.x] Mix to Vite migration #1391

merged 20 commits into from
Oct 26, 2023

Conversation

esurov
Copy link
Contributor

@esurov esurov commented Oct 14, 2023

Migrated from laravel mix to vite following official migration guide: https://laravel.com/docs/10.x/vite

vite

Migrated from laravel mix to vite following official migration guide: https://laravel.com/docs/10.x/vite

vite
@esurov esurov mentioned this pull request Oct 14, 2023
@driesvints driesvints changed the title Mix to Vite migration [4.x] Mix to Vite migration Oct 17, 2023
@taylorotwell taylorotwell marked this pull request as draft October 18, 2023 18:55
Signed-off-by: Mior Muhammad Zaki <[email protected]>
resources/js/app.js Outdated Show resolved Hide resolved
resources/js/app.js Outdated Show resolved Hide resolved
resources/js/routes.js Outdated Show resolved Hide resolved
esurov and others added 12 commits October 19, 2023 08:02
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
@esurov esurov marked this pull request as ready for review October 19, 2023 06:58
@taylorotwell taylorotwell merged commit 46b85cd into laravel:4.x Oct 26, 2023
10 of 11 checks passed
@esurov esurov deleted the vite branch October 26, 2023 16:53
@francoism90
Copy link

francoism90 commented Nov 1, 2023

Hmm, I'm keep getting the following error after upgrading:

GET http://localhost:5173/resources/sass/app.scss net::ERR_ABORTED 404 (Not Found)

I've run npm run build and use Vite. For the record, it should be resources/css/app.css.

Horizon, etc. work fine, but Telescope is broken.

@taylorotwell
Copy link
Member

taylorotwell commented Nov 1, 2023

@esurov Telescope actually doesn't even work in Laravel applications anymore when the application is running npm run dev?

@taylorotwell
Copy link
Member

#1399

@wfern
Copy link

wfern commented Nov 1, 2023

@esurov the problem is the "hot file" path...

It need to be set in place or before of the @vite directive or called somewhere before rendering.

The problem is because the default hot path is relative to the main application not telescope, so when we run npm run dev it gets the hot file from the main application: https://github.com/laravel/framework/blob/d0a6080db78a7e8a3316ff9c41eb86aa0866742d/src/Illuminate/Foundation/Vite.php#L170C54-L170C54

Example how to fix:

// .../telescope/resources/view/layout.blade.php

// Maybe it's better to set it in the TelescopeServiceProvider but this works for test purposes 
{{ Vite::useHotFile('public/hot') }}
    
@vite('resources/sass/' . $cssFile, '/vendor/telescope/build')

@timacdonald
Copy link
Member

If we revisit this, we should consider: laravel/framework#48964

@daniser
Copy link

daniser commented Nov 17, 2023

As a person who extensively uses Vite in my own packages, I already have solutions for this problem.

Define hot file path in template

layout.blade.php

{{ Vite::useHotFile('vendor/my-package/hot')
    ->useBuildDirectory('vendor/my-package/build')
    ->withEntryPoints(['resources/js/app.js', "resources/js/pages/{$page['component']}.vue"]) }}

The problem with this solution is, the Vite class is singleton, so it may affect main app entry points/build dir/hot file path etc in unpredictable ways.

Use macro or mixin

MyPackageServiceProvider.php

public function boot()
{
    Vite::mixin(new Support\ViteAliasMixin);
}

ViteAliasMixin.php

<?php

namespace MyVendor\MyPackage\Support;

use Illuminate\Foundation\Vite;

class ViteAliasMixin
{
    protected function myPackageEntryPoint(): \Closure
    {
        return fn (array $page) => $this
            ->useHotFile('vendor/my-package/hot')
            ->useBuildDirectory('vendor/my-package/build')
            ->withEntryPoints(['resources/js/app.js', "resources/js/pages/{$page['component']}.vue"]);
    }
}

layout.blade.php

{{ Vite::myPackageEntryPoint($page) }}

I use this solution in my packages, but it has same flaws as the previous one.

Implement some kind of Vite config manager and configure its instance in package's service provider

I already had PR for this, but unfortunately it was closed, so I implemented this functionality as a package.
I use this in my project, but not in my packages yet, as I don't want excess dependencies. I hope maybe one day we'll revisit this PR and it'll made its way into core?

MyPackageServiceProvider.php

public function boot()
{
    Vite::app('my-package')
        ->useHotFile('vendor/my-package/hot')
        ->useBuildDirectory('vendor/my-package/build')
        ->withEntryPoints(['resources/js/app.js']);
}

layout.blade.php

@viteApp('my-package')

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.

7 participants