Bud Assets Hashing/Versioning

I have several assets that aren’t referenced in CSS or JS, but are used inside some Blade template files.

I’m attempting to set up my Bud config so these assets are copied and versioned/hashed the same way as the files that are referenced in CSS/JS are. I’ve tried various combinations, but none of them work exactly as intended. The two most noteworthy attempts are below.

  1. The below code results in all images being copied over, but without hashing/versioning. The images referenced in CSS/JS are then copied a second time with a hash. However, the manifest.json file ends up not referencing any of those hashed versions.
app
    .entry({
      app: ['@scripts/app', '@styles/app'],
    })
    .assets([
      [app.path('@src/images'), app.path('@dist')],
    ])
  1. The below code avoids the image duplication, but obviously results in zero hashing/versioning. Setting the hash parameter to true has the same outcome as in #1 above.
app
    .hash(false)
    .entry({
      app: ['@scripts/app', '@styles/app'],
    })
    .assets([
      [app.path('@src/images'), app.path('@dist')],
    ])

How do I properly configure this so that all images are only brought over once and they are all properly versioned/hashed and added to the manifest.json?

I noticed this as well a few days ago. It will be addressed in an update. Maybe surprisingly, bud.assets is one of the more complex functions in all of bud.

For now the best way to do it is to use object syntax along with the @file shorthand:

    .assets([
      {
        from: app.path("@src/images"),
        to: app.path("@dist/@file"),
      },
    ])

Just now realizing there is a need for documenting this, but the @file path handle will be replaced with the hashed filename if hashing is enabled, and the normal filename otherwise.

2 Likes

That works like a charm. Thanks so much!

Bud v5.8.6 includes a fix for the issue which necessitated this workaround :relieved:

I have a use case where inside images I place a folder called favicon with all the variations of favicons (using realfavicongenerator.net).

I would need that only this folder isn’t hashed, as it contains two files (browserconfig.xml and site.webmanifest) that reference the favicon files, and I cannot use asset function as it isn’t php.

I would like that the rest of the images remain hashed. Is there a way I can do it? Or is there another path I can follow to accomplish this (like generating this two files dynamically every time I yarn build)?

Thanks!

Please don’t follow-up with new questions on topics that are already solved. Feel free to create a new topic.