Search code examples
laravel-5laravel-elixir

Laravel - Elixir - Concat and Minify assets


I have a project with a lot of js and css and I want to concat and minify them.
The problem is that I do not need all the scripts and css in all the pages so in my view I have a section which load the right assets for the page.

How can I use Elixir to minify resources for each page? is it possibile? Can I concat, minify and versioning different assets and references to them in my view?

How can I do that?


Solution

  • ......
    
    //main js 
    mix.scripts([
        ...
        ...
        ...
    ], publicJs + 'main.js');
    
    //some js that are for about page
    mix.scripts([
    
    ], publicJs + 'about.js);
    
    
    mix.version([
        publicJs + 'main.js',publicJs + 'about.js'
    ]);
    

    and for example if there are some files that are common for about and pricing pages i.e., then you can group them toghether too

    mix.scripts([
       ...
       ...
    ],publicJs + 'someGrouped.js);
    
    mix.scripts([
       ...
       ...
    ],publicJs + 'someOtherGrouped.js);
    
    mix.scripts([
       '../../' + publicJs + 'someGrouped.js,
       '../../' + publicJs + 'someOtherGrouped.js,
       ...
    ],publicJs + 'grouped.js);
    

    also you need to run

    gulp --production
    

    to minify all css and js files