Reputation: 474
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.");
}
});
Upvotes: 1
Views: 5374
Reputation: 11
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
Upvotes: 1