Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pabbec's avatar

Laravel Vite do not copy public folder

Hi.

I'm trying configuring Vite for my project. I'm using Laravel 9.40.1, Vite Plugin 0.6.1 and Vite 3.2.4

The main folder structure of my project is:

/
+--/we001/private/myapp/  (laravel app folder)
+--/we001/web (document root)

In my App\Providers\AppServiceProvider.php I have registered the path for my current public folder:

$this->app->bind('path.public', function() {
    return '/web001/web';
});

My vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import { viteStaticCopy } from 'vite-plugin-static-copy'

const path = require('path');

export default defineConfig({
    server: {
        https: false,
        host: 'website.local.ls'
    },
    base: '/',
    publicDir: 'resources/public',
    plugins: [
        laravel({
            input: [
                'resources/assets/scss/app.scss',
                'resources/assets/js/app.js',
            ],
            publicDirectory: '../../web',
            buildDirectory: '/assets/build',
            hotFile: 'storage/vite.hot', 
            refresh : true
        }),

        viteStaticCopy({
            targets: [
              {
                src: 'resources/public/**/*',
                dest: '../../web/assets/static'
              }
            ]
          })
    ],

    resolve: {
        alias: {
            '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
            '~lightGallery': path.resolve(__dirname, 'node_modules/lightgallery')
        },
    },

    css: {
        devSourcemap: true
    },

    build: {
        sourcemap: false,
        emptyOutDir: true,
        copyPublicDir: true,
    }
});

In my resources folder (inside Laravel project) I have:

resources/
+-- assets/
+----- js/app.js
+----- scss/app.scss
+-- public
+-----static/
+--------img/ (images for my website referenced in css and blade templates)
+--------fonts/ (fonts files)
+-- views/ (blade templates)

Problem 1: I want the image files in (resources/public/static) to be copied to the server's public folder: /web001/web/assets/static . But they are never copied by v vite, even making this setting explicit... that's why I'm using vite-plugin-static-copy to fix it.

Problem 2: All assets files from resources/public/static are copied into [document_root]/assets/static . When I'm using my website in production mode, works fine. But, in development mode, all assets images referenced from my css (scss) return 404 not found:

http : / / website.local.ls:5173/assets/static/img/home/bg.jpg

This file exists on /assets/static/img/home/bg.jpg but vite trying serve from development server.

According to the Vite documentation, all files in the public folder (publicDir) will be copied in the build process in the outDir, and in development mode, will be served from / . But none of this has happened. All css url() are referenced with absolute path '/static/img...'. (see the public directory in Vite docs (I can't publish a link).

Could someone help me with this?

Thanks!

Pablo

0 likes
0 replies

Please or to participate in this conversation.