kapoko/asset-json-reader

Reads asset locations from a json file outputted by webpack

dev-main 2021-03-09 08:24 UTC

This package is auto-updated.

Last update: 2024-04-09 15:23:55 UTC


README

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

Install

composer require kapoko/asset-json-reader:dev-main

Usage

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.