Search code examples
javascriptuglifyjs

UglifyJS inline script instead of filename


I'm wondering how I can compress an inline script with UglifyJS from the command line.

All the examples everywhere show file names, i.e.

uglifyjs somefile.js -o somefile.min.js

What I want to do is:

uglifyjs -option "function hello(){ var world='world'; alert(world) }"

How can I achieve this?


Solution

  • Use what is known as "heredocs" or "herestrings" (see http://en.wikipedia.org/wiki/Here_document). This technique works with just about any program that takes a file as an argument (in this case the shell is piping the string for you onto stdin). Should work on most shells / OSes.

    As a herestring:

    uglifyjs <<<"function hello(){ var world='world'; alert(world) }"
    

    or, as a heredoc:

    uglifyjs <<_END_
    function hello() {
      var world='world';
      alert(world)
    }
    _END_