vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.52k stars 6.19k forks source link

Multiple and aliased `publicDirs` #16138

Open danielroe opened 8 months ago

danielroe commented 8 months ago

Description

Currently, Vite supports a single publicDir. However, there are situations where we might want to merge multiple public directories. For example, in Nuxt we support an arbitrary number of public directories, each with a possible prefix (e.g. /foo points to /some/dir)

We can implement this within Nuxt itself (e.g. https://github.com/nuxt/nuxt/pull/26163) but we obviously lose out on some of the cool features Vite has, like rendering relative URLs, and in general the entire asset pipeline.

Suggested solution

publicDir could also accept an array of strings or objects of { dir: string, prefix: string } (to support alias).

Alternative

No response

Additional context

No response

Validations

patak-dev commented 8 months ago

I took a look at this one thinking it will be easy to implement, but if we go with publicDir: string | string[] | false it will be a breaking change. I did a code search and there are a lot of plugins and downstream projects expecting config.publicDir to be a string. So we could only do this in a major and still there, it may be too big of a change. We could add another option like config.extraPublicDirs that at least won't be breaking, but it will be hard to push the ecosystem to properly support it (there will be silent fails in this case). Maybe others have a better idea of how to tackle this. For now, it may not be a bad idea that you move forward with the PR you already have for Nuxt and we keep this issue open and gather more feedback. If there are other frameworks and projects in need of this feature, we could make a stronger case.

bluwy commented 8 months ago

This looks similar to https://github.com/vitejs/vite/issues/3428, which proposes support for snowpack mounts. I think it's an interesting idea, but I've not hit many cases that requires it (including for Astro and SvelteKit), and a single public directory has served well. I also think Nuxt can go ahead with its PR at the meantime. Following https://github.com/vitejs/vite/issues/3428, there only seems a few users who wanted it.

zhixiaotong commented 2 weeks ago

In the application with Monorepo, I need to flexibly combine various modules to generate the application. Although putting the static resources of all modules in a shared package is a solution, it will cause shared to maintain many assets that do not need to exist originally. I hope there is a way to implement different sub-applications to maintain their own assets. This problem can be solved by passing a static resource directory array during dev or build. If publicDir supports arrays, it is the solution I want.

nguyenbatranvan commented 1 week ago

I encountered this problem while working with monorepo. So I created a plugin for it. You can check out my library vite-multiple-assets Document for it