Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

Laravel Mix and Tree Shaking JavaScript

I'm curious to know if we need to explicitly enable tree shaking in Laravel Mix. Even with npm run prod, the size of my app.min.js is abou ~305 Kb!

I'm writing very minimal custom JS, and here''s all I'm using -

window.$ = window.jQuery = require('jquery');
window._ = require('lodash');
window.axios = require('axios');
import Echo from 'laravel-echo'; // I need this
window.Pusher = require('pusher-js');  // I need this

I think this is most of what Laravel uses. I was wondering if tree-shaking would bring the overall size down. Would appreciate suggestions / recommendations.

0 likes
3 replies
fylzero's avatar
fylzero
Best Answer
Level 67

@thebigk If you aren't using Bootstrap you could easily remove jQuery ...and Lodash. Both of those libraries should halve that size at lease. Granted 305k is nothing these days. Average webpage size as of 2019 is about 3MB. Yes, 3MB! If you dont need Axios, kill that too. Laravel doesn't use these... they just include them if you want to use them.

2 likes
thebigk's avatar
Level 13

@fylzero - I am using jquery for most of my site so I'll need it. I think I can try removing loadash. Still - does tree shaking happen automatically in mix?

Please or to participate in this conversation.