idleberg / php-wordpress-vite-assets

Injects assets from a Vite manifest to the Wordpress head, supports themes and plugins
https://packagist.org/packages/idleberg/wordpress-vite-assets
MIT License
130 stars 12 forks source link

Feature request: function which returns just the URL of the assets #11

Closed cbergen closed 1 year ago

cbergen commented 1 year ago

It would be handy to have a way to get at the URLs for any specific asset instead of just inserting into the head or returning the html tag.

If there were a function getStyleUrls() I would use this like so:

/*
    Rich text editor style
*/
use Idleberg\WordpressViteAssets\WordpressViteAssets;

$baseUrl = get_stylesheet_directory_uri() . '/dist/';
$manifest = get_stylesheet_directory() . "/dist/manifest.json";
$viteAssets = new WordpressViteAssets($manifest, $baseUrl);

add_action('admin_init', 'theme\editor_style');
function editor_style() use ($viteAssets)
{
    foreach ($viteAssets->getStyleUrls("src/editor.js") as $url) {
        add_editor_style($url);
    }
}

This would also give me the flexibility to use wp_enqueue() as normal.

If there's another way to achieve the same thing, please let me know!

cbergen commented 1 year ago

I found a solution using the package: https://github.com/idleberg/php-vite-manifest

use Idleberg\ViteManifest\ViteManifest;;

$baseUrl = get_stylesheet_directory_uri() . '/dist/';
$manifest = get_stylesheet_directory() . "/dist/manifest.json";
$vm = new ViteManifest($manifest, $baseUrl);

/*
    Rich text editor style
*/
add_action('admin_init', function () use ($vm) {
    foreach ($vm->getStyles("src/editor.js") as $style) {
        add_editor_style($style['url']);
    }
});