Created
March 16, 2018 09:05
-
-
Save elsassph/a029b798964401f72c8c613b60777ad9 to your computer and use it in GitHub Desktop.
Run uglify-js on all the javascript files in a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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