更新后自动缩小 Javascript/CSS 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4030028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto minify Javascript/CSS files after update?
提问by Legend
I know of ways to minify JS and CSS files but is there a way to auto-minify these files in the production system? For instance, I modify something inside the original JS files and update the production environment. However, while using the JS files, it should use an auto-minified version of the JS file (preferably update the current minified version if the source files changed).
我知道缩小 JS 和 CSS 文件的方法,但是有没有办法在生产系统中自动缩小这些文件?例如,我修改了原始 JS 文件中的某些内容并更新了生产环境。但是,在使用 JS 文件时,它应该使用 JS 文件的自动缩小版本(如果源文件更改,最好更新当前的缩小版本)。
Considering that I work in both Unix and Windows environment, writing a bash script did not seem like a plausible idea. So anything that is OS independent or maybe something that uses PHP initially to do this would be great. Any suggestions?
考虑到我在 Unix 和 Windows 环境中工作,编写 bash 脚本似乎不是一个合理的想法。所以任何独立于操作系统的东西,或者最初使用 PHP 来做到这一点的东西都会很棒。有什么建议?
回答by Chetan
You might find value in minify. It uses PHP5, and can sit well on your production server.
您可能会在minify 中找到价值。它使用 PHP5,可以很好地安装在您的生产服务器上。
From the website:
从网站:
It combines multiple CSS or Javascript files, removes unnecessary whitespace and comments, and serves them with gzip encoding and optimal client-side cache headers.
它结合了多个 CSS 或 Javascript 文件,删除了不必要的空格和注释,并使用 gzip 编码和最佳客户端缓存标头为它们提供服务。
回答by Ozten
I agree with @RC, read those existing Q/As.
我同意@RC,阅读那些现有的问答。
Also other options for your specific question:
您的特定问题的其他选项:
- Post-Commit hook - minify step
- Build step during development, commit minified files
- Server side filter that minifies on file change (Rails and other frameworks have this built in)
- Enhance you deployment script to do the minify during deployment
- 提交后挂钩 - 缩小步骤
- 在开发过程中构建步骤,提交缩小的文件
- 最小化文件更改的服务器端过滤器(Rails 和其他框架内置了此过滤器)
- 增强您的部署脚本以在部署期间进行缩小
Write these hooks in a cross platform language instead of bash. (JavaScript, Python, Ruby, etc).
用跨平台语言而不是 bash 编写这些钩子。(JavaScript、Python、Ruby 等)。

