What is the advantage of using asset-builder?

Hi All.
What is the advantage of using asset-builder rather then enqueueing scripts as usual?

I’m initialising a new project and I’ll be using bootstrap.
I’m about to pull in the bootstrap js. I figured I should try and do things the Sage way.

I can see that there is a file called ‘manifest.json’ in ‘assets/scripts’. Further rabbitholing leads me to the documentation for asset-builder, where I discover that the asset-builder module “assembles and orchestrates your dependencies so you can run them through your asset pipeline”.

That sounds like something I should be doing.
After all, I am using an asset pipeline.

So, I’m guessing that I should make bootstrap.js a dependency, and declare it as such in manifest.json.

That’s all well and good.
But I’m still not sure why I’m doing this.

Looking back at the description of asset-builder, I see this: “Feed it a manifest file and it will give you globs”.
At this point I’m left scratching my head.

Can someone give me a description of what asset-builder does for the developer? And why I should declare scripts as deps rather then simply enqueueing them?

Thanks.

The asset builder itself does exactly what you said: takes a manifest file and spits out globs.

Glob in this sense is basically an array of files. For each asset type (images, scripts, styles, and fonts), a glob is produced and fed to gulp, which then does its magic.

In the case of scripts (which seems to be what you’re asking), gulp takes the glob and concatenates and minifies the files into one single file.

If you have a properly configured server and you’re making full use of the asset builder in Sage, the result is…

  • fewer HTTP calls
  • smaller file size
  • better gzip compression
  • faster load times

You might consider checking out Ben’s screencast if you want a complete walkthrough on how it all comes together.

4 Likes

Thanks!
That clarifies things wonderfully.

This actually raises another couple of questions about dependency management.
I won’t ask those questions yet: I’ll look into how to use composer with Sage first.

I’ve purchased the screencast and I’ll watch it as soon as I can.

Thank you.