Pranav Singh
Pranav Singh

Reputation: 20091

uglifyjs-folder remove console.log & alert from minified files

I am minifying multiple files in a folder using uglifyjs-folder in npm package.json like :

"uglifyjs": "uglifyjs-folder js -eyo build/js"

It is working as intended & minify all files in folder.

I want to remove any console.log & alert while minify but not able to find any option with uglifyjs-folderhttps://www.npmjs.com/package/uglifyjs-folder

Please help.

Upvotes: 1

Views: 717

Answers (2)

kontur
kontur

Reputation: 5220

uglify-folder (in 2021, now?) supports passing in terser configs like so:

$ uglify-folder --config-file uglifyjs.config.json ...other options...

and with uglifyjs.config.json:

{
  "compress": {
    "drop_console": true
  }
}

And all options available here from the API reference.

Upvotes: 0

RobC
RobC

Reputation: 24992

Short Answer

Unfortunately, uglifyjs-folder does not provide an option to silence the logs.


Solution

You could consider writing a nodejs utility script which utilizes shelljs to:

  • Invoke the uglifyjs-folder command via the shelljs exec() method.
  • Prevent logging to console by utilizing the exec() methods silent option.

The following steps further explain how this can be achieved:

  1. Install

    Firstly, cd to your project directory and install/add shelljs by running:

    npm i -D shelljs
  1. node script

    Create a nodejs utility script as follows. Lets name the file: run-uglifyjs-silently.js.

    var path = require('path');
    var shell = require('shelljs');
    
    var uglifyjsPath = path.normalize('./node_modules/.bin/uglifyjs-folder');
    
    shell.exec(uglifyjsPath + ' js -eyo build/js', { silent: true });
    

    Note: We execute uglifyjs-folder directly from the local ./node_modules/.bin/ directory and utilize path.normalize() for cross-platform purposes.

  2. package.json

    Configure the uglifyjs script inside package.json as follows:

    {
      ...
      "scripts": {
        "uglifyjs": "node run-uglifyjs-silently"
        ...
      },
      ...
    }
    
  3. Running

    Run the script as per normal via the command line. For example:

    npm run uglifyjs
    

    Or, for less logging to the console, add the npm run --silent or shorthand equivalent -s option/flag. For example:

    npm run uglifyjs -s
    

Notes:

  • The example gist above assumes that run-uglifyjs-silently.js is saved at the top-level of your project directory, (i.e. Where package.json resides).

  • Tip: You could always store run-uglifyjs-silently.js in a hidden directory named .scripts at the top level of your project directory. In which case you'll need to redefine your script in package.json as follows:

    {
      ...
      "scripts": {
        "uglifyjs": "node .scripts/run-uglifyjs-silently"
        ...
      },
      ...
    }
    

Upvotes: 1

Related Questions