3

What is the best way to remove asserts (console.assert) from JavaScript code for production version? Maybe there is some software that can be configured to sort of build JavaScript and remove asserts?

UPDATE __________________________

I've installed GRUNT and groundskeeper plugin. This is what my Gruntfile.js has:

module.exports = function(grunt) {
    // Project configuration.
    grunt.initConfig({
    groundskeeper: {
        compile: {
            files: {
              'calculator/add.js': 'calculator/add.js'
            },
            options: {
                console: false
            }
        }
    }
});

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-groundskeeper');

The problem is with when I run grunt groundskeeper I get the following error:

Running "groundskeeper:compile" (groundskeeper) task
Warning: Line 2: Invalid left-hand side in assignment Use --force to continue.

I assume that the problem is with this line:

'calculator/add.js': 'calculator/add.js'

Since if I replace it with the following:

'path/to/result.js': 'path/to/source.js'

Everything works fine:

Running "groundskeeper:compile" (groundskeeper) task
>> File path/to/result.js created empty, because its counterpart was empty.
>> 1 files read, 0 cleaned.

What's wrong with the my original line?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • possible duplicate of [How to quickly and conveniently disable all console.log statements in my code?](http://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code) – Sarath Dec 04 '13 at 10:22

2 Answers2

3

make assert as an empty function in your production

console.assert = function(){}

if you can check it is a production version then put the code like,

if (production) {
   console.assert = function(){}
}

for old IE shim

  if (typeof console == "undefined" || typeof console.assert == "undefined"){
       var console = { assert : function() {} }; 
    }
Sarath
  • 9,030
  • 11
  • 51
  • 84
  • Thank, but this may not be an option for IE, since `console` object doesn't exist until console is opened. – Max Koretskyi Dec 04 '13 at 10:08
  • that you can check with some shim with out that it will work in IE10,9,8 only IE7 need that – Sarath Dec 04 '13 at 10:12
  • Thank you! Yet Remco Haszing options seems to be a best solution. I upvoted your answer anyway :). – Max Koretskyi Dec 04 '13 at 10:19
  • http://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code its similar question – Sarath Dec 04 '13 at 10:22
2

I am using the grunt-groundkeeper plugin for this:
https://github.com/Couto/grunt-groundskeeper

If you're not using Grunt yet, I really recommend using it for JavaScript projects. Explaining how to use Grunt itself is out of scope of this question.

The following example config sets removing all logging statements in the www/scripts.min.js file as the default task.

'use strict';

module.exports = function(grunt) {

    grunt.initConfig({
        groundskeeper: {
            dist: {
                files: {
                    'www/scripts.min.js': 'www/scripts.min.js'
                }
            }
        });

    grunt.loadNpmTasks('grunt-groundskeeper');

    grunt.registerTask('default', ['groundskeeper']);
};
Remco Haszing
  • 7,178
  • 4
  • 40
  • 83