XSkinner's avatar

npm run prod still logging console.log

Hi.

I Have been reading for a while and It is supposed console.log statements to be stripped out when running npm run production... however, I still get my console logs shown up after running it.

I have Laravel mix 5.7.1 installed. @JeffreyWay should I do something additional?

Thank you very much!!

0 likes
6 replies
ejdelmonico's avatar

Why would you expect Mix to remove your console.log() statements? You need to remove them or comment them out so uglify removes them as long as comments are set to true which is default I believe for the webpack plugin. You can feed the plugin an options file as well.

Here are the default settings in Mix:

/**
         * Uglify-specific settings for Webpack.
         *
         * See: https://github.com/mishoo/UglifyJS2#compressor-options
         *
         * @type {Object}
         */
        uglify: {
            sourceMap: true,
            uglifyOptions: {
                compress: {
                    warnings: false
                },
                output: {
                    comments: false
                }
            }
        },
1 like
XSkinner's avatar

Well @ejdelmonico thank you for your answer, if I'm not wrong console logging is used for debug while dev... isn't it?

Thank you for pointing Uglify config... Some issue in GitHub was posted some time ago, and the fix was adding drop_console option to true... I think it would fix it... But still I would like to ask if the default behavior was not stripping out console logs when running production

ejdelmonico's avatar

Judging from the configuration, I would say that it is not a default option. In fact, I am not aware of the webpack plugin even offering to remove console statements.

Here are the available options from the plugin:

https://cl.ly/qLrC

You will have to insert additional webpack.config to add to the Uglify options it looks like. In webpack.mix.js you do something like:

mix.webpackConfig(webpack => {
    return {
        plugins: [
            uglify: {
              uglifyOptions: {
                compress: { drop_console: true }
              }
            }
        ]
    };
});
ejdelmonico's avatar
Level 53

@xskinner Ok, I got the config modification to remove all console.* except those with other imported libraries. IN your mix file and before you call mix.js()

mix.options({
  uglify: {
    uglifyOptions: {
      compress: {
        drop_console: true
      }
    }
  }
});

I have it working in Laravel 5.6 and Mix version 2.1.11

1 like
kuttumiah's avatar

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
                }
            }
        }
    });
}

I have it working in Laravel 5.6 and Mix version 4.0.7

1 like

Please or to participate in this conversation.