leafsphp / mvc-core

Core functions and configurations for Leaf MVC, Leaf API and Skeleton
https://leafphp.dev/modules/mvc-core/
3 stars 4 forks source link

LeafPHP MVC Error: Vite manifest not found at: /build/build/manifest.json, when trying to run with build in production mode #15

Open shashankitsoft opened 9 months ago

shashankitsoft commented 9 months ago

Describe the bug Using leaf with MVC, created project using : leaf create <project-name> --mvc Windows 11, PHP 8.1, On command line, leaf --version = v2.11.1 As per composer json:

{ "leafs/leaf": "^3.3",
 "leafs/vite": "^0.1.0",
  "leafs/blade": "*",
   "leafs/mvc-core": "dev-main",
 ..
}

As per package.json:

"devDependencies": {
        "@leafphp/vite-plugin": "^0.1.2",
        "sass": "^1.69.5",
        "vite": "^4.4.9"
    },

Now leaf serve works fine and site visible on http://localhost:5500/ Creating a build usingleaf view:build generate successfully. But on running leaf serve give error Error: Vite manifest not found at: /build/build/manifest.json

To Reproduce

  1. In default Leaf-MVC installation and added sass as above, create a css app\views\scss\test.css (having just one line css and an inner import of scss

    @import './test-inner1.scss';
    div{background-color: red !important;}

    test-inner1.scss also has just div{color: green;}

  2. Go to app\views\index.blade.php and include that css in head, {{ vite('scss/test.css') }}

  3. In .env set, APP_ENV=production (earlier it was 'local')

  4. vite.config.js as below (just added css file name, rest as default):

    
    import { defineConfig } from 'vite';
    import leaf from '@leafphp/vite-plugin';

export default defineConfig({ plugins: [ leaf({ input: ['app/views/js/app.js', 'app/views/css/app.css','app/views/scss/test.css'], refresh: true, }), ], });


5. Go to command line, creating a build using` leaf view:build` generate successfully. The files assets and manifest.json generated in correct folder paths: 

> public/build/manifest.json 
> public/build/assets/app-e3b0c442.css 
> public/build/assets/test-35007d2a.css 
> public/build/assets/app-bc6e2fe4.js

6. Now running` leaf serve` give error on http://localhost:5500/ **Error: Vite manifest not found at: /build/build/manifest.json**. This obviously it is searching in wrong path /build/build. The path where manifest generated was public/build .

![error](https://github.com/leafsphp/leaf/assets/18620683/9f6b2c51-c427-4579-ae15-c5dd4b6e669d)

The path of css included with vite function in app\views\index.blade.php i.e. {{ vite('scss/test.css') }} should be able to look for manifest in correct folder path, in production using build or normal development mode.

Or I am missing something? How I can write the css or js paths in blade templates, which work in dev as well as in production mode. I want to test both modes on localhost.

**Expected behavior**
The css/js files included in blade templates should look for build manifest in correct path in production mode, and use normal/original paths in development mode (as build is not needed in dev mode)

How the css/js files to be included rightly with vite() in blade template head, to work in both modes?
Will like to know if something I am missing here, or to be set differently, as I have not found related in documentation of Leaf. 
mnismt commented 9 months ago

I'm facing problem too, try to modify the Vite.php in vendor but no luck.

ross-oreto commented 8 months ago

Can confirm this is an issue. I also modified Vite.php to test and it kinda worked if I hard coded the "public" directory in function manifestPath:

protected static function manifestPath($buildDirectory)
{
    return 'public' . '/' . ltrim($buildDirectory, '/') . '/' . ltrim(static::$manifestFilename, '/');
}

However this only eliminates the leaf exception. The paths to the bundled assets are broken and the browser is trying to fetch from /assets/build/assets/ and that should just be /build/assets.

Again a simple hardcoding will deal with this:

    protected static function assetPath($path, $secure = null)
    {
        return 'public' . '/' . ltrim($path, '/');
    }

but in the end, it will need to pull these values from variables since the public directory isn't a hard requirement.

reinhart1010 commented 6 months ago

Hi, I've also tried to investigate this by doing these, and none of them worked:

My best guess so far is this bug forces Leaf to find the desired /build/build directory from the system's root directory.

[!NOTE] I have also discovered a similar bug when assets('...') behaves differently between dev and production: dev returns /assets/... (the absolute path for loading asset URLs) while production returns /public/assets/.... This is reported in leafsphp/mvc-core#14.

juanulis3s commented 6 months ago

Did you were able to fix the problem? I have got the same problem. In local env works fine if the command leaf serve is executing. If you stop that command then I get the error Vite manifest not found at: /build/build/manifest.json

I think the issue in production is because there is a config missing to use cpanel or any other server. Because it is not posible to execute leaf serve.

I hope someone from LeafPHP team could help us.

ross-oreto commented 3 months ago

I also still have found no way to resolve this or find any viable workaround.

mychidarko commented 2 weeks ago

A new version will be released to fix this issue soon. You can fix it for now in your codebase by adding this snippet to your public/index.php file:

\Leaf\Vite::config('build', PublicPath('', false));
phederal commented 2 weeks ago

Скоро будет выпущена новая версия, исправляющая эту проблему. Вы можете исправить ее сейчас в своей кодовой базе, добавив этот фрагмент в свой public/index.phpфайл:

\Leaf\Vite::config('build', PublicPath('', false));

We're very much looking forward to it!