39

Is it possible to compress multiple files with UglifyJS?

Something like...

uglifyjs -o app.build.js appfile1.js appfile2.js ...

Also, I am running Uglify via NodeJS in Windows Command Prompt

3 Answers 3

52

Actually what you want to do (trick it into thinking its just 1 file) is just cat it

Linux

cat file1.js file2.js file3.js file4.js | uglifyjs -o files.min.js

Windows (untested)

type file1.js file2.js > uglifyjs -o files.min.js

OR

type file1.js file2.js > merged.files.js
uglifyjs -o merged.files.js
6
  • Guess I should have mentioned I am on windows... :( Commented Jan 11, 2012 at 0:43
  • Err... this might work then (not tested by me of course) stackoverflow.com/a/60254/158014
    – Jakub
    Commented Jan 11, 2012 at 0:44
  • I updated my answer with windows syntax, I hope that works, as I haven't tested it, but that should be it...
    – Jakub
    Commented Jan 11, 2012 at 2:07
  • U just made my day! Works perfectly. Commented Jul 24, 2012 at 23:33
  • This approach is bad, because it doesn't allow you to have source maps, pointing at your original sources. Luckily, with UglifyJS2, we don't need this anymore. Commented Feb 19, 2017 at 10:51
42

For future readers of this question, w/ UglifyJS2, this is trivial now...

uglifyjs file1.js file2.js -o foo.min.js

There is also support for source maps too.

uglifyjs file1.js file2.js -o foo.min.js --source-map foo.min.js.map --source-map-root http://foo.com/src
2
  • 3
    You can also use * to combine all JS files from a directory into a single file" uglifyjs -o test.js /someTestDir/* The docs say that the options should be first, and the files last; but these examples show the opposite. To quote github.com/mishoo/UglifyJS "filename should be the last argument and should name the file from which to read the JavaScript code" Does it not matter, or is this answer incorrect? Commented Jan 10, 2014 at 14:20
  • 2
    Just to add a a clarification to my previous comment, the * trick appears to be specific to Git Bash and is unrelated to UglifyJS. Commented Jan 11, 2014 at 1:10
4

No.

but as other answers states, in it current form, it can concatenate all of them in the same output. Which is not the same as is being asked.

If you have a dozen files that are individual modules. You can not uglify them all at the same time. Which is desirable because you save build time to start nodejs every time.

yuicompressor allows this, and the time saved by not starting up a jvm for each individual file is great. And in the end you do get file1-min.js, file2-min.js, etc... not one concatenated blob.

With uglify you will have to spawn one process per file if you do not want to concatenate output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.