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
133 stars 12 forks source link

Pass options into inject() ? #8

Closed toddheslin closed 1 year ago

toddheslin commented 1 year ago

I'm wondering how we actually pass options into the inject() method. I see the default options here: https://github.com/idleberg/php-wordpress-vite-assets/blob/befefe23eb91d84dc6bdd3e0c65a7d43060a729c/src/WordpressViteAssets.php#L32-L35

But when inject() calls getScriptTag() here: https://github.com/idleberg/php-wordpress-vite-assets/blob/main/src/WordpressViteAssets.php#L69

It doesn't pass anything through. Likely because it doesn't accept options as an argument.

My use case is that I want to disable integrity hashing as it's causing problems when I'm running Cloudflare in front of my server.

idleberg commented 1 year ago

Historically, the inject() method was meant for easy usage, going with the defaults and all. However, I think you have made a valid point and I will rethink my approach to support options.

idleberg commented 1 year ago

If you want to give it a try, have a look at the next branch

toddheslin commented 1 year ago

Thanks @idleberg! It looks like that approach mostly works, but I found a bug:

You forgot to add $options in these two places: https://github.com/idleberg/php-wordpress-vite-assets/blob/next/src/WordpressViteAssets.php#L89 https://github.com/idleberg/php-wordpress-vite-assets/blob/next/src/WordpressViteAssets.php#L71

Both should be:

add_action($action, function() use ($entries, $options) {...

This should work just fine :-)

idleberg commented 1 year ago

Good finds, thank you!

I want to invest some more time to add tests for the WordPress-specific parts, so it might take a little before I tag a new release.

toddheslin commented 1 year ago

Excellent. Instead of pulling from your working branch, I've been able to work around using inject() as follows:

$baseUrl = get_theme_file_uri('/assets/dist/manifest.json');
$manifest = get_theme_file_path('/assets/dist/manifest.json');
$entryPoint = 'assets/src/main.ts';
$options = [
  "crossorigin" => false,
  "integrity" => false,
];

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

$action = is_admin() ? 'admin_head' : 'wp_head';
add_action($action, function () use ($options, $entryPoint, $viteAssets) {
  $scriptTag = $viteAssets->getScriptTag($entryPoint, $options);
  if ($scriptTag) {
    echo $scriptTag . PHP_EOL;
  }
}, 0, 1);

add_action($action, function () use ($options, $entryPoint, $viteAssets) {
  foreach ($viteAssets->getPreloadTags($entryPoint, $options) as $preloadTag) {
    echo $preloadTag . PHP_EOL;
  }
}, 0, 1);

add_action($action, function () use ($options, $entryPoint, $viteAssets) {
  foreach ($viteAssets->getStyleTags($entryPoint, $options) as $styleTag) {
    echo $styleTag . PHP_EOL;
  }
}, 0, 1);

Note that I've made a small modification to the original code where I am not assuming an array of $entries as I know I only have a single $entryPoint. But this allows me to add the options using the current 0.8.0 version.

Hopefully this will help anyone else having a similar issue.

idleberg commented 1 year ago

Added in v0.9.0