1

I have tried to minify javascript files using uglifyjs, but it's only outputting the file name in the file output.min.js followed by a semicolon and it's telling me that "File was successfully saved."; Not the minified code of the js file geolocation.service.js. Could anyone please help? The code below is saved in a file that I named uglifyjs. I used $node uglifyjs.js to run the file.

var fs = require('fs');

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

var result = uglifyjs.minify(["geolocation.service.js"]);

console.log(result.code);

fs.writeFile("output.min.js", result.code, function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("File was successfully saved.");
    }
});

2 Answers 2

2

The API is minify(code) not minify(file paths)

uglifyjs.minify(fs.readFileSync('geolocation.service.js', 'utf8'))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This has worked for one file, but didn't work for multiple input files and one output file. I want to minify all my app_client files into one app.mini.js. Could you help me figure out how to do it?
1
var appClientFiles = {
"app" : fs.readFileSync('app_client/app.js', 'utf8'),
"controller": fs.readFileSync('app_client/home/home_controller.js', 'utf8'),
"geolocation": fs.readFileSync('app_client/common/services/geolocation.service.js', 'utf8'),
"data": fs.readFileSync('app_client/common/services/hubRadarData.service.js', 'utf8'),
"filters": fs.readFileSync('app_client/common/filters/formatDistance.filter.js', 'utf8'),
"about": fs.readFileSync('app_client/about/about.controller.js'),
"directives": fs.readFileSync('app_client/common/directives/ratingStars/ratingStars.directive.js', 'utf8'),
}

var uglified = uglifyJs.minify(appClientFiles, {compress: false});
if (uglified.error) throw uglified.error;

This is nice way to minify multiple JS file using uglifyjs remember to always use an object to hold the JavaScript files where the key is the name of the js file and the value is the location of the JavaScript file

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.