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

Use package in plugin development #7

Closed torbentschechne closed 1 year ago

torbentschechne commented 1 year ago

Hello there,

I just want to use site to bundle my js/node_modules. Then I want to enqueue the built js file in my Wordpress plugin. How can I do this with this package?

I did this:

public function __construct()
{
    add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
}

public function scripts() {

    $baseUrl = plugin_dir_url( __FILE__ );
    $manifest = "dist/manifest.json";
    $entryPoint = "index.js";

    $viteAssets = new WordpressViteAssets($manifest, $baseUrl);
    $viteAssets->addAction($entryPoint);

}

I did bundle the js file with Vite's Library mode as I do not need a index.html. Now my structure looks like this:

- plugin-dir
-- src
-- PluginBootstrap.php
-- index.js
--- dist
--- js

Inside src/dist I have the built js and manifest.json. Now I do this as above, and get Manifest file does not exist: dist/manifest.json. This also happens when I do /dist/manifest.json or src/dist/manifest.json or /src/dist/manifest.json

How does this work with this package? Thanks!

torbentschechne commented 1 year ago

Update: I needed to use

$basePath = plugin_dir_path( __FILE__);
$manifest = $basePath."/dist/manifest.json";

But the .js is not included on my site. So maybe the hook is the wrong one?

torbentschechne commented 1 year ago

I digged a bit deeper and I have on entry in $entries in the function add_action. It's index.js. But the code as this:

$scriptTag = $this->getScriptTag($entries[0]); is returning an empty string. When I do this:

add_action('wp_head', function () use ($entries) {
die('test');
            foreach ($entries as $entry) {
                $scriptTag = $this->getScriptTag($entry);

It's not called.

torbentschechne commented 1 year ago

I got it working when I place the code in the Bootstrap of my plugin. When I use it somewhere where I use another action like this one in my first post it seems that the code from add_action is not fired properly, because the registration of wp_head does not work. I tried several hooks like plugins_loaded, admin_init, wp none of them work to register the script tag correctly inside wp_head :/ Any ideas?

torbentschechne commented 1 year ago

I figured it out - I need to use admin_head as wp_head is not fired in the backend. So I can not use addAction, but need to retrieve the script and push it into the head.

idleberg commented 1 year ago

I'm very sorry for the late reply, but you caught me at a bad time. Not supporting WordPress plugins has been oversight and I plan to add this in the next release. Thanks for providing your helpful learnings!

idleberg commented 1 year ago

@torbentschechne Hopefully, the new version works better for your needs

jremen commented 1 year ago

@idleberg Please can you update your documentatation on how to use this with plugin? I still can't get to import manifest.json when in admin.

First of all, your method inject() in documentation is old and not used anymore. Using addAction in admin does nothing at all.