DEV Community

Mike Healy
Mike Healy

Posted on

Lighter LoDash Builds for Laravel

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.

Top comments (0)