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

Basti0208's avatar

Vite not copying all static assets

Hi, I've switched to Vite in my current laravel project. I am adding my static assets with the following code:

import.meta.glob([
    '../../media/**/*'
]);

In my Vite config I have the following setting:

build: {
        rollupOptions: {
            output: {
                assetFileNames(assetInfo) {
                    const pathToFile = normalizePath(assetInfo.name);

                    // Check if the file is in the resources/assets/media folder
                    if (pathToFile.includes('resources/assets/media')) {
                        // Get path to the file relative to the resources/assets/media folder
                        const pathToMediaFile = pathToFile.split('resources/assets/media/')[1];

                        // Remove filename and extension
                        const pathToFileWithoutMediaAndFilename = pathToMediaFile.substring(0, pathToMediaFile.lastIndexOf("/"));

                        // Return the path to the media folder
                        return `assets/media/${pathToFileWithoutMediaAndFilename}/[name]-[hash].[ext]`;
                    }

                    return 'assets/[name]-[hash][extname]';
                },
            },
        },
    },

Most of the files are copied correctly but some are missing. I've already debugged the function "assetFileNames" and the files which are missing are processed and the right path is set. Altough they won't be copied to the folder.

Any ideas?

0 likes
14 replies
Sinnbeck's avatar

Curious why you are making your own hash? Be aware that vite ignores image files with the same content. This might be "fixed" in the next version

Basti0208's avatar

@Sinnbeck Thank you for the hint. I think I confused myself with the hashing. That fixed at least some errors. The error with the same content still exists. Do you know when the fix is gonna be applied?

Best regards

Basti0208's avatar

@Sinnbeck I wrote a little Python script which gives some more information about the problem. 64 of 1912 assets are missing in general (not copied to the build folder).

Then I tried to check if there are duplicated asset names and if there were any, if the content is also the same. 132 assets have the same name and 4 assets have the same content.

I am not sure what the problem is and if it is even related to the pr you've mentioned. For further information I have uploaded the log file.

Sinnbeck's avatar

@Basti0208 Be aware that name doesnt matter. It is the content only :) So if foo.jpg and bar.jpg has the same content, only one is copied over and versioned.

Basti0208's avatar

@Sinnbeck Thanks for the information. I've edited my python script accordingly. Now I can see the problem :)

If you have a look at the log file, all files which have not been copied have the same content in some other file.

Basti0208's avatar

@Sinnbeck It seems like the beta has fixed the issue. Thank you!

One more question: Can I use the Vite::asset() to pass a path to a folder? If I use Vite::asset('resources/assets/media/flags/') I get the error Unable to locate file in Vite manifest: resources/assets/media/flags/..

Sinnbeck's avatar

@Basti0208 I dont think so no. Not sure what you need it for though? Be aware that Vite is macroable so you can add your own methods if needed.

Basti0208's avatar

@Sinnbeck I use a theme which passes the path as a data attribute to an input field. data-kt-flags-path="{{ Vite::asset('resources/assets/media/flags/') }}"

Is there a way to get that code working? Or simply pass the resources/assets/media/flags as a string?

Sinnbeck's avatar

@Basti0208 You can easily pass it as a string data-kt-flags-path='resources/assets/media/flags/'. But do you mean you need it in javascript? If so that will be an issue. Instead you would need to compile the js with vite as well I would think.

Basti0208's avatar

@Sinnbeck I think it is used in the javascript. It's a theme so I haven't written that code myself. I will have a look into that and will give a feedback, thanks in advance :)

Sinnbeck's avatar

@Basti0208 Maybe for the flags you should just put them directly in public, so you can point directly to them

Please or to participate in this conversation.