If used properly, Laravel Mix and webpack should take care of all the necessary module bundling and minification for you. However, you may have some legacy code or vendor libraries that need to be concatenated and minified. Not a problem.
Consider the following snippet:
This will naturally merge one.js
and two.js
into a single file, called merged.js
. As always, during development, that merged file will remain uncompressed. However, for production (export NODE_ENV=production
), this command will additionally minify merged.js
.
If you need to concatenate JavaScript files that have been written in ES2015, you may update your mix.combine()
call to mix.babel()
. The method signature is identical. The only difference is that, after the files have been concatenated, Laravel Mix will perform Babel compilation on the result to transform the code to vanilla JavaScript that all browsers can understand.
Squeezer helps you compile, minify and compress your web sources and images - html, css, javascript, coffeescript, less, typescript, sass, scss - with just one click. Minify, Compile and Compress your web dev files and images on Mac OSX.
Similarly, you may also minify one or more files with the mix.minify()
command.
There are a few things worth noting here:
*.min.ext
file. So minifying app.js
will generate app.min.js
.export NODE_ENV=production
).mix.combine(['one.js', 'two.js'], 'merged.js').minify('merged.js');
Just stick with the single mix.combine()
call. It'll take care of both.{note} Please note that minification is only available for CSS and JavaScript files. The minifier will not understand any other provided file type.
Minify CSS Code
Find out which CSS code on the page is better to compress
If the site uses CSS (Cascading Style Sheets), the server must pass them to the browser each time a resource is requested. And this affects the website speed. The more complex the style files, the longer the visitor will wait until the site is fully loaded. Using CSS compression can reduce code size and improve site performance.
Check whether your page needs CSS compression using the tool above. If the page has some unoptimized parts of CSS you will see similar message as on the picture below.
After getting this info, try to use tools recommended by Google, like CSSNano and csso.