简体   繁体   English

PHP中的打包,缓存,JS和CSS,区分开发和生产环境

[英]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: 我想要做的目标是:

  • Make production pages fast! 快速制作生产页面! I would like that the Google Page Speed and YSlow return the best scores. 我希望Google Page SpeedYSlow能够获得最佳分数。 This means: 这意味着:
    1. Combine and compress JS files and CSS and position the group in the right place (bottom or top of the page) in the HTML. 组合并压缩 JS文件和CSS, 并将组定位在HTML中的正确位置(页面的底部或顶部)。 For .js Google Closure seems the best choice. 对于.js谷歌关闭似乎是最好的选择。
    2. .JS and .CSS are smartly cached but be sure that they get reloaded when of the .JS or CSS componet is updated. .JS和.CSS是智能缓存的,但请确保在更新.JS或CSS组件时重新加载它们。 301 File not changed etc. 301文件未更改等
    3. Cache type : I think cache on disk is fine. 缓存类型 :我认为磁盘上的缓存很好。 Consider APC and Memcache or Redis if they significantly improve speed. 如果它们显着提高速度,请考虑APC和Memcache或Redis。
    4. Capable to specify and use lazy load of .JS when necessary or at least not to block the page rendering. 能够在必要时指定和使用.JS的延迟加载 ,或者至少不阻止页面呈现。
    5. (Optional) Compress the HTML too. (可选)也压缩HTML。
  • Make website development easy : 简化网站开发
    1. Use short command in the .php file when you want to include .js or .css and compress them only in the production environment 如果要包含.js或.css并在生产环境中仅压缩它们,请在.php文件中使用short命令
      • Use syntax like pack_js(['first.js', 'second.js' 'third.js']) and pack_css(['first.less', 'second.less' 'third.css'], true) 使用类似pack_js(['first.js','second.js''third.js'])pack_css(['first.less','second.less''third.css'],true的语法)
      • It is easy to configure develpment or production environment. 很容易配置开发或生产环境。 Maybe just a call to SetDebug(true or false) . 也许只是调用SetDebug(true或false) Default production 默认生产
      • Easy to set up cache folders and source folders 轻松设置缓存文件夹源文件夹
    2. Use of LESS to make CSS development sucks less. 使用LESS使CSS开发变得更少。 Automatically compile LESS files in CSS in production but use of LESS.js in development so that each time you change the .less file in development it is updated on the server. 在生产中自动编译CSS中的LESS文件,但在开发中使用LESS.js ,这样每次在开发中更改.less文件时,它都会在服务器上更新。
    3. (optional) In development it includes a JS and a LESS console similar to the shell at https://www.squarefree.com/bookmarklets/webdevel.html (可选)在开发中,它包括一个JS和一个LESS控制台,类似于https://www.squarefree.com/bookmarklets/webdevel.html上的shell

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-BoosterTurbine看起来是最好的起点: http//github.com/Schepp/CSS-JS-Boosterhttp://turbinecss.org/

Other JS/CSS Combiners solutions and articles 其他JS / CSS Combiners解决方案和文章

Resource for JS compression and caching: JS压缩和缓存的资源:

LESS compilers/tutorials/tools: 较少的编译器/教程/工具:

At Deployment time options: 在部署时选项:

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...). 你可能想要考虑的其他选择是AntCapistrano (还有很多其他的),但这些需要其他语言的知识(如果你真的想要,你可以从他们那里启动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: 我意识到它可能并不完全是你所追求的,但仍会提出一些建议:

  1. Make the compression/compilation part of the shipping process.(You may already be doing this, but it wasn't clear from the above). 制作运输过程中的压缩/编译部分。(您可能已经这样做了,但从上面不清楚)。
  2. Compress/compile it on the development servers too. 在开发服务器上压缩/编译它。 It might be a hassle for debugging/testing but you want to be able ensure the production version and test versions are as similar as possible. 它可能是调试/测试的麻烦,但您希望能够确保生产版本和测试版本尽可能相似。 If you have the luxury of multiple development stages, then you could possibly compress on one of them. 如果你有多个开发阶段的奢侈品,那么你可以压缩其中一个。
  3. Only do the compression/compilation if it passes some sort of quality scan (eg jslint ) 只有在通过某种质量扫描时才进行压缩/编译(例如jslint
  4. Don't combine the modules - keep them separate. 不要组合模块 - 将它们分开。 The performance benefits you will gain will be so negligible, as to be almost pointless. 您将获得的性能优势可以忽略不计,几乎毫无意义。
  5. Don't change the HTML, just change the dependant modules content. 不要更改HTML,只需更改依赖模块内容即可。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM