How to remove console.log from production?
Asked Answered
E

1

6

How can I remove all console.log from production. This code isn't working on laravel-mix 4.x:

webpack.mix.js

mix
    .js('resources/js/app.js', 'public/js')

if (mix.inProduction()) {
    mix.options({
      uglify: {
        uglifyOptions: {
          warnings: false,
          comments: false,
          beautify: false,
          compress: {
            drop_console: true,
          }
        }
      }
    });

    mix.version();
}
Evolutionist answered 18/1, 2019 at 15:51 Comment(0)
E
21

With laravel-mix 4.x they replaced uglify with terser (Changelog). You have to change your webpack.mix.js:

if (mix.inProduction()) {
    mix.options({
        terser: {
            terserOptions: {
                compress: {
                   drop_console: true
                }
            }
        }
    });

    mix.version();
}
Evolutionist answered 18/1, 2019 at 15:51 Comment(2)
Bug: All terserOptions get ignored for development builds (at least in Laravel-mix 5.0.9) - above's if (mix.inProduction()) check is useless if said is intended (and not a bug).Goya
Thanks, it's works ("laravel-mix": "^6.0.49"). Note that mix.version() is part of code from original question and can be removed if not needed.Icaria

© 2022 - 2024 — McMap. All rights reserved.