nystudio107 / craft-plugin-vite

Plugin Vite is the conduit between Craft CMS plugins and Vite, with manifest.json & HMR support
MIT License
12 stars 11 forks source link

public assets v4 #9

Closed samuelbirch closed 2 years ago

samuelbirch commented 2 years ago

adds a boolean as a second param to the craft.vite.asset('url', true) so that assets in the vite public folder can be referenced correctly.

khalwat commented 2 years ago

Is this something that isn't working correctly currently? Shouldn't anything in /public be served by the dev server if you're running it?

samuelbirch commented 2 years ago

If i have assets that are referenced in the css or js then it all works fine.

Its when i want to use a public asset, the dev server is working but not when its been built as the asset is not in the manifest.

This tweak doesn't use the manifest and return the path prefixed with the destination folder

khalwat commented 2 years ago

Ah I see. They are solving this in Vite 3 via this PR: https://github.com/vitejs/vite/pull/6649#issuecomment-1159278169

Are you able to wait until then, or...?

samuelbirch commented 2 years ago

Yes i could wait until then (although don't know when "then" is?)

I'm assuming that PR is about being able to add assets to the manifest (outside of js/css), but wouldn't we still have the same issue with public assets (that we don't want hashed) not being able to correctly reference?

khalwat commented 2 years ago

Ah yes, you're right on both fronts. How about this for an idea -- since they have hard-coded public what if instead of a flag, we looked for public in the first segment of the incoming URL, and did it automagically?

samuelbirch commented 2 years ago

"public" can be configured using the "publicDir" property, I have it set to "/src/assets"

instead of a flag, we could add another function: craft.vite.publicAsset('url'). I thought a flag might be cleaner.

khalwat commented 2 years ago

Roger that. Will merge it in and update the docs -- I'm assuming this works with the devServer as well as in production?

samuelbirch commented 2 years ago

yes it works with both.

khalwat commented 2 years ago

Thx for the PR!