Search code examples
laravelgulplaravel-5.1laravel-elixir

Laravel Elixir: How to execute a task only in production mode?


I am using a bunch of "console.log" calls for debugging my JS application. However, I'd like to remove these automatically when sending the code to production.

Using a traditional gulpfile, I would use the "gulp-strip-debug" plugin that will be only triggered when I run the "production" main task.

When using Laravel Elixir, though, it's a little bit different. I use the "--production" flag to minimize my assets, but I'd like to execute the strip-debug task afterwards. I could not find how to do that in the Laravel 5.1 documentation.


Solution

  • After checking the elixir.config object, it seems that the --production flag sets the elixir.config.production variable to true.

    It is therefore possible to wrap the execution of a specific task in an if(elixir.config.production) statement, allowing you to trigger them when the environment is "production" or if you manually specified it by using the --production flag from the command line.