swashata / wp-webpack-script

šŸ’„šŸ”„šŸ“¦šŸ‘©ā€šŸ’» An easy to use, pre configured, hackable webpack setup & development server for WordPress themes and plugins.
https://wpack.io
MIT License
407 stars 57 forks source link

Strange enqueue behaviour, with nothing being enqueued at all. Needs fix in Enqueue.php:297 source #1247

Open lnilya opened 2 years ago

lnilya commented 2 years ago

I am not sure what I am doing wrong, but I had numerous plugins and themes already developed with wpack.io (version 5.x) with no issues. This time however whenever I try to enqueue my ts/scss into the backend nothing shows up. I debugged the issue and possibly solved it.

in my wpproject:

name:'referral',
entry: {
    reftable:'./src/js/reftable.tsx',
    reftablestyles:'./src/scss/main.scss',
},

in the plugins main file:

include_once 'vendor/autoload.php';
    global $enq;
    $enq = new \WPackio\Enqueue('dbireferral', 'dist', $ver, 'plugin', __FILE__);

and in the hook to enqueue scripts

add_action('admin_enqueue_scripts', function(){
    global $enq;
    $res = $enq->enqueue('referral', 'reftable', []);
    $enq->enqueue('referral', 'reftablestyles', []);
});

So far pretty standard. However with the current setup it did not work for me. I debugged it and compared it with other plugins I had developed with wpack.io. The reason seems to be in wpacks main php file: Enqueue.php in the getAssets function It loads the part of the manifest where the js files are all linked. $enqueue = $manifest['wpackioEp'][ $entryPoint ]; For some reason the manifest looks different for my older plugins vs my current one. And it differs by adding an assets node around the js/css nodes. That way the above code in $enqueue doesn't give any results and nothing is enqueued: wpackioEp:{ reftable: { assets: { js: [....] .... } } }

If in line 297 you change the enqueue variable it seems to work: $enqueue = $manifest['wpackioEp'][ $entryPoint ]['assets'];

Maybe it has something to do with versions and how webpack delivers data. or maybe it is specific to backend. But someone should look at it, why this happens.

lnilya commented 2 years ago

Confirmed that behaviour.

With "@wpackio/scripts": "6.4.0" (npm) and "wpackio/enqueue": "2.2" (composer) I get the behaviour described above. So I believe something changed in the web pack config and it doesnt work with the php side anymore, which needs a change as described above. Maybe there is a setting to prevent this "assets" node to be created.

With "@wpackio/scripts": "5.0.0" (npm) and "wpackio/enqueue": "2.2" (composer) everything works fine and as expected.

It's a shame that wpack is not really being supported developed anymore. Was such a great library! : /

swashata commented 2 years ago

Hello,

With @wpackio/script v6.4.0, you have to use wpackio/enqueue v3.4.0. There has been a change in how manifest json files are generated from the upstream package and the PHP package has been updated accordingly. You can safely upgrade both to the latest version and it should work.

I would appreciate if you'd like to add this to the website documentation.