Search code examples
javascriptnode.jsuglifyjs2

Can I beautify javascript with uglifyjs via API?


Can I programmaticaly beautify JavaScript code with UglifyJS library? When I try to use something like

var uglifyjs = require('uglify-js');

...

var options = { compress: false, mangle: false, beautify: true };
var code = uglifyjs(source, options);

I get an error

{ error:
   { [DefaultsError: `beautify` is not a supported option]
     message: '`beautify` is not a supported option',
     ...

I don't want to use extra libraries, because I already use uglifyjs for minifying javascript in my code, and command-line version of uglifyjs can do this with command

uglifyjs --beautify -o script.js script.min.js

Solution

  • From the docs here (https://github.com/mishoo/UglifyJS2#minify-options) it looks like that needs to be in the Output Options

    var options = { 
      compress: false, 
      mangle: false, 
      output: {
        beautify: true 
      }
    };
    var code = uglifyjs(source, options);