clubstudio / craft-asset-rev

A Craft CMS plugin to help with cache busting
MIT License
109 stars 7 forks source link

[Feature Request] Allow manifest to return array #36

Open tomkiss opened 4 days ago

tomkiss commented 4 days ago

When using Vite to generate a manifest, the format is slightly different, e.g:

{
  "src/fonts/Helvetica-Medium.woff2": {
    "file": "fonts/Helvetica-Medium-CIKPJROY.woff2",
    "src": "src/fonts/Helvetica-Medium.woff2"
  }
}

This results in an error when using Asset Rev

club\assetrev\services\AssetRev::getAssetFilename(): Return value must be of type string, array returned

...because it expects a string.

public function getAssetFilename(string $file): string

Could the getAssetFilename function be updated to be less restrictive?

scottwakefield commented 23 hours ago

Hi @tomkiss,

You should be able to write a short custom closure-based strategy in your config file to support Vite's manifest format.

Here's an example:

<?php

use club\assetrev\exceptions\ContinueException;

return [
    'pipeline' => 'vite|querystring|passthrough',
    'strategies' => [
        'vite' => function ($filename, $config) {
            $manifest = json_decode(file_get_contents($config['manifestPath']), true);

            return $manifest[$filename]['file'] ?? throw new ContinueException("Asset `$filename` not found in manifest file");
        },
        'querystring' => \club\assetrev\utilities\strategies\QueryStringStrategy::class,
        'passthrough' => function ($filename, $config) {
            return $filename;
        },
    ],
    'manifestPath' => '@webroot/mix-manifest.json',
    'assetsBasePath' => '@webroot',
    'assetUrlPrefix' => '@web',
];

Let me know if that helps at all!

tomkiss commented 22 hours ago

Thanks, that's super helpful!