Reputation: 19247
I am used to using Rails, and I really enjoy the asset pipeline. The ease that CSS & JS just is minified and bundled automatically when you upload your app.
What is the best way to have something similar in NodeJS, or should the deployment process be a bit different from what I am used to in Rails?
Upvotes: 1
Views: 1847
Reputation: 139
PS: I hate to use different frameworks and libraries where they are not needed. So, I want propose my easiest way that allows to minify and simplify CSS.
function minifyCSS (css) {
return String(css)
.replace(/\/\*[\s\S]*?\*\//g, ' ') // Comments
.replace(/\s+/g, ' ') // Extra spaces
.replace(/([\(\)\{\}\:\;\,]) /g, '$1') // Extra spaces
.replace(/ \{/g, '{') // Extra spaces
.replace(/\;\}/g, '}') // Last semicolon
.replace(/ ([+~>]) /g, '$1') // Extra spaces
.replace(/([^{][,: \(\)]0)(%|px|pt|pc|rem|em|ex|cm|mm|in)([, };\(\)])/g, '$1$3') // Units for zero values
.replace(/([: ,=\-\(])0\.(\d)/g, '$1.$2') // Lead zero for float values
.replace(/([^\}]*\{\s*?\})/g, '') // Empty rules
.replace(/([,: \(])#([0-9a-f]{6})/gi, function(m, pfx, clr) { // HEX code reducing
if (clr[0] == clr[1] && clr[2] == clr[3] && clr[4] == clr[5]) return pfx + '#' + clr[0] + clr[2] + clr[4];
return pfx + '#' + clr;
});
}
Upvotes: 2
Reputation: 427
You can use grunt.js as a task runner that you run to take care of all the minification/preprocessing you want to do.
For minifying js you can use https://github.com/gruntjs/grunt-contrib-uglify
For minifying css you can use https://github.com/gruntjs/grunt-contrib-cssmin
Upvotes: 4
Reputation: 151
The deployment process will be different but there are lots of minifiers out there for javascript. I recommend using connect, then you can use this handy guy:
https://github.com/mape/connect-assetmanager
Upvotes: 0