kucrut / vite-for-wp

Vite integration for WordPress plugins and themes development.
GNU General Public License v2.0
260 stars 29 forks source link

Fix duplicate stylesheet registration when a CSS is imported by more than one entrypoints #90

Open kucrut opened 5 months ago

kucrut commented 5 months ago

See https://github.com/kucrut/vite-for-wp/pull/86#issuecomment-2165710180

netlas commented 5 months ago

My solution #86 relied on wordpress wp_enqueue_style removing duplicate registered handles once that is called. Was that not working properly? Or would you rather have it removed from the internal asset array?

kucrut commented 5 months ago

From your example manifest file, I registered resources/js/admin.ts and resources/js/admin_menu.ts as two different assets with Vite\enqueue_asset(). Since they have different handles, the assets/Menu-c4e73a0f.css they load have different handles as well, hence the duplication.

netlas commented 5 months ago

I see what you mean now. A possible solution would be to remove the handle option as a prefix. replace row 347

$style_handle = "{$options['handle']}-{$slug}";

with

$style_handle = $slug;

Vite is making filenames unique, so an extra prefix in the handles are not necessary for any other reason than semantics.