Web_Designer
Web_Designer

Reputation: 74650

UglifyJS 2 browser build download

Where can I download the browser build of UglifyJS 2? I'm having trouble building it myself.

I installed uglify-js with npm. I just don't know where to run the uglifyjs --self command to build the browser version. I keep getting this error:

'uglifyjs' is not recognized as an internal or external command, operable program or batch file.

Also in the browser build, I'm not sure how to minify a string of javascript code. I figured out that I can do UglifyJS.parse(scriptEl.innerHTML) to create an object tree of the parsed JS, then I can use the .print_to_string(); method to convert it back to a string but without the whitespace, but I can't figure out what method to use to fully minify it including variable renaming and such.

Upvotes: 4

Views: 1588

Answers (2)

Aniket
Aniket

Reputation: 9758

Head over to http://lisperator.net/uglifyjs/

Once you installed it for NodeJS, UglifyJS2 provides a quick way to build itself for the browser:

uglifyjs --self -c -m -o ./tmp/uglifyjs.js

The command worked for me. Now, you can use it in your browser straight away.

For specifying the options for the methods, you should look at:

Note: the error you're getting when you run uglifyjs in your command-line seems like your terminal is not able to locate the installed file. Try to do a global install using npm -g install uglify-js or use the command from the directory where you installed it in.

Upvotes: 3

Chandranshu
Chandranshu

Reputation: 3669

I think you installed uglifyjs which is what I also installed the first time. Use:

npm install -g uglify-js

to install it. Note the extra hyphen. Also, note the "-g" option which is needed if you want to use it as a command-line application. Without the "-g" flag, you can still use it in node programs but not on the command line. After that running uglifyjs --self worked like a charm.

To answer the second part of your problem, do this:

var result = scripts.join("\n\n");  # concat
result = UglifyJS.minify(result, {fromString: true});
console.log(result.code);

Hope this helps.

Upvotes: 1

Related Questions