Automatic Minification with Node.js and RequireJS

Automatic CSS Minification with Node.js and RequireJSLast 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!

About the Author

Author: Joe Zim

Joe Zim

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.


  • Mohamed Jama

    Hows this different than grunt? or compass? nevertheless very useful
    thanks

    • http://www.joezimjs.com Joe Zimmerman

      Well, first off, I made it. =) Seriously, though, it was made more as an experiment and this article and video was just to show you how you could do something similar yourself and how simple it is.