[英]Packing, caching, JS and CSS in PHP that differentiate between development and production environment
I am trying to make development easy and have highly optimized output in production. 我正在努力使开发变得简单并且在生产中具有高度优化的输出。
The goals of what I am trying to do is: 我想要做的目标是:
Note: it is OK to use Apachee modules and .htaccess files if they significantly speed up the process. 注意:如果Apachee模块和.htaccess文件显着加快了进程,则可以使用它们。 But it should be able to set them up quicky, ideally with just a setup command. 但它应该能够快速设置它们,理想情况下只需要一个设置命令。
Is there something that do this? 有没有做到这一点? Or what are the best resources to start? 或者什么是最好的资源开始?
I would prefer a solution that consists of a PHP script (eventually few .php files, .htaccess and compressing executables) that compresses the .JS with the Google Closure Compiler and compress/compile the CSS/LESS files stripping out useless comments and white spaces. 我更喜欢一个由PHP脚本(最终几个.php文件,.htaccess和压缩可执行文件)组成的解决方案,它使用Google Closure Compiler压缩.JS并压缩/编译CSS / LESS文件,删除无用的注释和空格。 A special cookie could be used on production server to output the development version. 可以在生产服务器上使用特殊cookie来输出开发版本。
I would like to have: 我想拥有:
A php function usable like this: pack_js(['first.js', 'second.js', 'third.js']) that write something like: 一个像以下那样可用的php函数: pack_js(['first.js','second.js','third.js']) ,它们写成:
On developments servers: 在开发服务器上:
<script type="text/javascript" src="static/js/first.js"></script>
<script type="text/javascript" src="static/js/second.js"></script>
<script type="text/javascript" src="static/js/third.js"></script>
On production servers (if the special cookie is not present): 在生产服务器上(如果不存在特殊cookie):
<script type="text/javascript" src="cache/12a42323bfe339ea9w.js"></script>
Another function: pack_css(['first.less', 'second.less', 'third.css'], true) that write something like: 另一个函数: pack_css(['first.less','second.less','third.css'],true)写下:
On developments servers: 在开发服务器上:
<link rel="stylesheet/less" href="/static/css/first.less" type="text/css" />
<link rel="stylesheet/less" href="/static/css/second.less" type="text/css" />
<link href="/static/css/third.css" type="text/css" />
<script src="http://lesscss.googlecode.com/files/less-1.0.21.min.js"></script>
<script type="text/javascript" charset="utf-8">
less.env = "development";
less.watch();
</script>
On production servers (if the special cookie is not present): 在生产服务器上(如果不存在特殊cookie):
<link href="/cache/46537a8b8e876f7a8e7.css" type="text/css" />
the second parameter specify if the less.js should be output on the development server 第二个参数指定是否应在开发服务器上输出less.js
Obviously 12a42323bfe339ea9w.js and 46537a8b8e876f7a8e7.css are the optimized, packed and compiled version of the script. 显然12a42323bfe339ea9w.js和46537a8b8e876f7a8e7.css是脚本的优化,打包和编译版本。 This solution should be able to detect when a source file change and recompile the scripts for production. 此解决方案应该能够检测源文件何时更改并重新编译脚本以进行生产。 It should be configurable to regarding locations of the script and type of caching (disk is fine). 它应该可以配置为关于脚本的位置和缓存类型(磁盘很好)。 Ideally the pack_js should probably have an option to make possible lazy load of the js in production. 理想情况下,pack_js应该可以选择在生产中对js进行延迟加载。
Every suggestion is welcomed. 每个建议都受到欢迎。
Still working on exploring the best solution leveraging the resource already available. 仍在努力探索利用现有资源的最佳解决方案。
CSS-JS-Booster and Turbine so far looks like the best starting point: http://github.com/Schepp/CSS-JS-Booster and http://turbinecss.org/ 到目前为止CSS-JS-Booster和Turbine看起来是最好的起点: http : //github.com/Schepp/CSS-JS-Booster和http://turbinecss.org/
Why don't you use a project build system to deploy your application to the production server that does just that? 为什么不使用项目构建系统将应用程序部署到生产服务器上呢? For PHP you might like Phing , since it would allow you to write extra plugins in PHP, that you can execute while deploying. 对于PHP,您可能会喜欢Phing ,因为它允许您在PHP中编写额外的插件,您可以在部署时执行。 Other options you might want to consider of you take this route are Ant and Capistrano (and there's lots of others), but these require knowledge of other languages (granted, you could start the php parser from them if you really wanted to...). 你可能想要考虑的其他选择是Ant和Capistrano (还有很多其他的),但这些需要其他语言的知识(如果你真的想要,你可以从他们那里启动php解析器...... )。
Great Question! 好问题!
I've worked with a process with similar requirements, but we took somewhat of a different approach. 我使用过具有类似要求的流程,但我们采取了一些不同的方法。 I realise its maybe not exactly what you're after, but would still make some recommendations: 我意识到它可能并不完全是你所追求的,但仍会提出一些建议:
I realise its a bit different from what you were intending, but in my experience it leads to a more robust development promotion model. 我意识到它与你的意图有点不同,但根据我的经验,它会导致更强大的开发促销模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.