Lighter LoDash Builds for Laravel
Mike Healy

Mike Healy @mike_hasarms

About: Hey, I'm a contract web developer with a particular interest in Laravel & Vue web apps. Also working on my side project HadCoffee.

Location:
Brisbane
Joined:
Sep 15, 2018

Lighter LoDash Builds for Laravel

Publish Date: Jan 5 '21
3 0

Laravel projects often include the LoDash JavaScript utility library. It contains handy utilities, however if you don't need its entire suite you may be serving unnecessary JS to your users that you never run.

LoDash supports custom builds to avoid this problem, and it's fairly easy to modify your Laravel's bootstrap.js file trim down the features you're loading.

Default resources/js/bootstrap.js

window._ = require('lodash'); // 71.1K (gzipped: 24.6K)
Enter fullscreen mode Exit fullscreen mode

Customizing the build

In my case I was only using the throttle and debounce helpers from LoDash. This can be mapped to the window lodash object (typically _) like so:

window._ = require('lodash/core')  // 14K (gzipped: 5.3K)
window._.debounce = require('lodash/debounce')  // 3.5K (gzipped: 1.4K)
window._.throttle = require('lodash/throttle') // 3.7K (gzipped: 1.4K)
Enter fullscreen mode Exit fullscreen mode

My application JS can then call _.debounce() and _.throttle() for those functions with a significant saving to my JS bundle size.

Comments 0 total

    Add comment