Is there any module in NodeJS to concatenate and minify JavaScript files?
UglifyJS is a Node module that is all about minifying javascript. I don't think it also joins files, but there might be an option I missed.
Edit: With UglifyJS 2, it has built in concatenation as well.
If you want to do this inline in your node app it's really easy. This allows you to dynamically generate your minified/concatenated js script at runtime without using the grunt or yeoman way.
npm install uglify-js
and in your module:
var fs = require('fs'); var uglify = require("uglify-js"); var uglified = uglify.minify(['file1.js', 'file2.js', 'file3.js']); fs.writeFile('concat.min.js', uglified.code, function (err){ if(err) { console.log(err); } else { console.log("Script generated and saved:", 'concat.min.js'); } });
I recommend using UglifyJS which is a JavaScript parser / mangler / compressor / beautifier library for NodeJS.
If you are interested in automation tools that do more than simply concatenate and minify files, there are the following solutions:
GruntJS is a task-based command line build tool for JavaScript projects. The current version has the following built-in tasks:
Besides this tasks there's a lot of plugins available.
Gulp is a toolkit that will help you automate painful or time-consuming tasks in your development workflow. For web development (if that's your thing) it can help you by doing CSS preprocessing, JS transpiling, minification, live reloading, and much more. Integrations are built into all major IDEs and people are loving gulp across PHP, .NET, Node.js, Java, and more. With over 1700 plugins (and plenty you can do without plugins), gulp let's you quit messing with build systems and get back to work.
Yeoman is a robust and opinionated set of tools, libraries, and a workflow that can help developers quickly build beautiful, compelling web apps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With