Automatic Minification With Node.js and RequireJS

Last time we talked about how to use NPM and some of the command line utilities that you can install that are built using Node.js. Today, we’re actually going to build a small command line utility ourselves that can be a bit of a time saver. We’re going to use the RequireJS optimizer to automatically concatenate and minify our CSS files the moment any of them are changed.

In the video I mention a tutorial done by Jeffrey Way. You can find that video on Youtube. There is a caveat to using the automatic minification described in this video: debugging via the inspector. If you’re using an inspector in the browser it’ll always tell you the style is in style.min.css on line 1, which makes it a lot more difficult to find the problematic rules and almost completely removes the benefits of having your styles in separate stylesheets.

If you want to learn more about using the RequireJS Optimizer or the fs.watchFile function, use the links below. There’s also a link to the source code that I used.

Conclusion

There are tons of cool things that you can do with Node.js and just thinking about the fact that there’s a watchFile method can inspire tons of ideas by itself. You can use web sockets to update an online file manager when someone else uses FTP to upload their own files. I hope you enjoyed this. God bless and happy coding!

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.