Search code examples
javascriptgulplaravel-5.3laravel-elixir

Stop Gulp Removing Unused Javascript Functions



I am using Gulp via a Laravel project and I have javascript functions that I keep in one file that I then reference from the template when the page loads.

The issue is Gulp removes unused functions when it complies the javascript files.

Is there a way to stop this behaviour.

Thanks in advance.


Solution

  • Finally, found the answer.

    Edit configuration in /node_modules/laravel-elixir/dist/Config.js

    Lines 283 - 300:

        /*
     |----------------------------------------------------------------
     | UglifyJS Parser/Compressor/Beautifier
     |----------------------------------------------------------------
     |
     | UglifyJS is a JavaScript parser/compressor/beautifier.
     | It'll minify your JavaScript with ease and has an option to
     | mangle your code.
     |
     */
    
      uglify: {
        options: {
          compress: {
            drop_console: Elixir.inProduction
          }
        }
      }
    


    To stop the removal of unused functions add unused: false to the compress options.

    Change to:

        /*
     |----------------------------------------------------------------
     | UglifyJS Parser/Compressor/Beautifier
     |----------------------------------------------------------------
     |
     | UglifyJS is a JavaScript parser/compressor/beautifier.
     | It'll minify your JavaScript with ease and has an option to
     | mangle your code.
     |
     */
    
    uglify: {
      options: {
        compress: {
          drop_console: Elixir.inProduction,
          unused: false
        }
      }
    }
    


    For more information on available options go to the UglifyJS Documentation.