Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify Scripts/CSS in production mode with node.js

I have a web app that runs in node. All the (client) Javascript/CSS files are not minified at the moment to make it easier to debug.

When I am going into production, I would like to minify these scripts. It would be nice to have something like:

node app.js -production

How do I serve the minified version of my scripts without changing the script tags in my html files? There should be something like: if I am in production, use these 2 minified(combined) scripts, else use all my unminified scripts..

Is this possible? Maybe I am thinking too complicated?

like image 863
Christian Avatar asked Feb 12 '13 20:02

Christian


1 Answers

You might be interested in Piler. It's a Node.js module that delivers all the JavaScript (and CSS) files you specify as usual when in debug mode, but concatenated and minified when in production mode.

As a special feature, you can force CSS updates via Socket.io in real-time to appear in your browser (called "CSS Live Updated" in Piler), which is quite awesome :-).

The trick is that inside your template you only have placeholders for the script and link elements, and Piler renders these elements at runtime - as single elements in debug mode, and as a dynamically generated single element in production mode.

This way you can forget about creating concatenated and minified versions of your assets manually or using a build tool, it's just there at runtime, but you always have the separated, full versions when developing and debugging.

like image 146
Golo Roden Avatar answered Sep 30 '22 18:09

Golo Roden