我习惯使用Rails,我真的很喜欢资产管道。 CSS&当您上传应用程序时,JS会自动缩小并自动捆绑。
在NodeJS中有类似内容的最佳方法是什么,或者部署过程是否与我在Rails中习惯的有点不同?
答案 0 :(得分:4)
您可以使用grunt.js作为您运行的任务运行器来处理您想要进行的所有缩小/预处理。
对于缩小js,您可以使用https://github.com/gruntjs/grunt-contrib-uglify
要缩小css,您可以使用https://github.com/gruntjs/grunt-contrib-cssmin
答案 1 :(得分:2)
PS:我讨厌在不需要的地方使用不同的框架和库。 因此,我想提出一种最简单的方法,允许缩小和简化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;
});
}
答案 2 :(得分:0)
部署过程会有所不同,但javascript有很多缩小器。我建议使用connect,然后你可以使用这个方便的人: