Skip to content

Instantly share code, notes, and snippets.

@elsassph
Created March 16, 2018 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elsassph/a029b798964401f72c8c613b60777ad9 to your computer and use it in GitHub Desktop.
Save elsassph/a029b798964401f72c8c613b60777ad9 to your computer and use it in GitHub Desktop.
Run uglify-js on all the javascript files in a folder
// NOTE: run with `NODE_ENV=production` for optimal results
const fs = require('fs');
const path = require('path');
const UglifyJS = require('uglify-js');
// Path to process
const bin = './bin';
function onlyJS(files) {
return files.filter(function(file) {
return path.extname(file) == '.js';
}).map(function(file) {
return path.join(bin, file);
});
}
function minify(file) {
console.log('Minify ' + file);
const result = UglifyJS.minify(file, {
mangle:true,
compress: {
dead_code: true
}
});
if (result && result.code) {
fs.writeFile(file, result.code);
}
}
fs.readdir(bin, function(err, files) {
onlyJS(files).forEach(minify)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment