Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove asserts from javascript code for production version

Tags:

javascript

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?

like image 632
Max Koretskyi Avatar asked Aug 03 '26 21:08

Max Koretskyi


1 Answers

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() {} }; 
    }
like image 175
Sarath Avatar answered Aug 06 '26 11:08

Sarath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!