Search code examples
node.jsuglifyjs

uglify merging multi js files into single js file with minified


Here i am with new task where combining multiple file js into single js file and minify the js.

Steps i made,

  1. Installed nodeJS in my local machine
  2. checked node and npm is working
  3. Installed "uglify-js" by "npm install uglify-js " and installed

But i have minified single js with below code:

var UglifyJS = require('uglify-js');
var fs = require('fs');

var result = UglifyJS.minify('site.js', {
    mangle: true,
    compress: {
        sequences: true,
        dead_code: true,
        conditionals: true,
        booleans: true,
        unused: true,
        if_return: true,
        join_vars: true,
        drop_console: true
    }
});

fs.writeFileSync('site.min.js', result.code);

This working perfectly to me.

But ,

i have site1.js,site2.js and site3.js this should be converted into single minified file (eg.files.min.js)


Solution

  • Pass it an array of filenames

    var result = UglifyJS.minify([ "file1.js", "file2.js", "file3.js" ]);
    

    Doc: https://github.com/mishoo/UglifyJS2#the-simple-way