laravel / vite-plugin

Laravel plugin for Vite.
MIT License
799 stars 151 forks source link

Custom manifest filename #110

Closed arthvrian closed 2 years ago

arthvrian commented 2 years ago

Laravel Vite Plugin Version: 0.5.2 Laravel Version: 9.19 Node Version: 16.16.0 NPM Version: 8.11.0 Vite : 3.0.3 Host operating system: Windows 10 Web Browser & Version: Brave 1.41 (Chromium 103) Running in Sail / Docker: No

Description

Vite allows setting a custom manifest filename https://vitejs.dev/config/build-options.html#build-manifest because the default filename may conflict if you are building a PWA or Chrome extension or whatever

This option is overridden by this plugin (maybe here? https://github.com/laravel/vite-plugin/blob/main/src/index.ts#L105) and always overrides the manifest.json file

Config

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    build: {
        manifest: 'vite-manifest.json',
        emptyOutDir: false,
        outDir: "public/",
    },
    plugins: [
        laravel({
            buildDirectory: "./",
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
        }),
    ],
});

Output with laravel plugin

> build
> vite build --config vite.config.js

vite v3.0.3 building for production...
✓ 58 modules transformed.
public/manifest.json             0.23 KiB
public/assets/app.4ee680d5.css   0.02 KiB / gzip: 0.04 KiB
public/assets/app.d81e8d40.js    90.63 KiB / gzip: 33.07 KiB

Output without laravel plugin

> build
> vite build --config vite.config.js

vite v3.0.3 building for production...
✓ 58 modules transformed.
public/vite-manifest.json        0.23 KiB
public/assets/app.4ee680d5.css   0.02 KiB / gzip: 0.04 KiB
public/assets/app.f223d706.js    90.63 KiB / gzip: 33.07 KiB
jessarcher commented 2 years ago

Hi @arthvrian,

If we were to do this, we'd also need to provide a way to specify the custom manifest location on the Laravel side of things too.

It should be fairly straight-forward, especially after https://github.com/laravel/framework/pull/43442 is merged.

Thoughts @timacdonald?

timacdonald commented 2 years ago

@arthvrian are you actually experiencing this conflict? Is there any specific reason you are trying to put things in the root /public directory?

If you are doing this, isn't it also deleting your index.php, robots.txt, favicon.ico` and everything else that is static in the public directory (as Vite clears everything out of the build directory when it builds the assets)? I would imagine doing this renders your Laravel application un-usable as it can no longer receive requests?

arthvrian commented 2 years ago

@jessarcher you are right

@timacdonald I'm just trying to mimic the behaviour of -mix- using -vite-, taking advantage of the speed of -vite- to compile, but leaving the resulting files in the same path that -mix- does/did, so I don't have to modify (for now, a lot of) blade templates

After a few tries, I realized that it's much easier to do it with the plain -vite- config, without involving the laravel plugin (in a new test project, I'll try the actual target project later), but it would be a good enhancement for the plugin

For your last question, that's what this -vite- option emptyOutDir is for, update your compiled assets without removing anything from external sources

jessarcher commented 2 years ago

It sounds like you've got a workaround, so I will close this for now. I'm not keen on adding a new configuration option to this plugin and to the framework to mimic Mix. We can always revisit it if another use case requires a different manifest name.