Reads asset locations from a json file outputted by webpack. Works well with asset-webpack-plugin or webpack-manifest-plugin, which stores the hashed filenames in a json file. With this package you can easily get the hashed filenames
composer require kapoko/asset-json-reader:dev-main
Output by the asset-webpack-plugin may look something like this (dist/assets.json
):
{
"one": {
"js": "/js/one_2bb80372ebe8047a68d4.bundle.js"
},
"two": {
"js": "/js/two_2bb80372ebe8047a68d4.bundle.js"
}
}
Usage is:
use Kapoko\AssetJsonReader\AssetJsonReader;
$assets = new AssetJsonReader($pathToJson);
$assets->get('one.js')); // Returns '/js/one_2bb80372ebe8047a68d4.bundle.js'
$assets->get('two.js')); // Returns '/js/two_2bb80372ebe8047a68d4.bundle.js'
If the manifest doesn't exists it returns the given string, which might be handy in development mode when the files aren't hashed.