May 9, 2013 - 前端    No Comments

用minify合并,压缩js和css

drupal可以把所有

<?php print $styles ?>
<?php print $scripts ?>

输出的css和js都合并压缩成一个文件以减少http request数量,但有时候项目里的某个页面是一个游离于drupal外的静态页面,js和css是不受drupal管理的,那么如果有合并压缩css和js的需求的时候,就需要用到minify

首先下载minify,解压,把解压后的min文件夹复制到apache的根目录。
min/config.php 只需要配置一个地方,去掉前面的注释:

//$min_cachePath = '/tmp';

这个时候访问http://localhost/min, 会报403的错,显示没有权限。
当然即便又是简单粗暴的777后,还是会报这个错!

检查apache配置后,发现一个小细节。目前我的httpd-vhosts.conf的配置如下:

<Directory "/Users/ares/Sites/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

修改成:

<Directory "/Users/ares/Sites">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

后重启apache,http://localhost/min 可以正常访问了。
改成上面的配置后,整个~/Sites都可以支持.htaccess文件了。

minify的使用很简单,可以通过http://localhost/min的界面一个一个添加。也可以直接改代码。
MIN.txt里的两个例子如下:

Let’s say you have CSS files at these URLs:
http://example.com/scripts/jquery-1.2.6.js
http://example.com/scripts/site.js

You can combine these files through Minify by requesting this URL:
http://example.com/min/?f=scripts/jquery-1.2.6.js,scripts/site.js

SIMPLIFYING URLS WITH A BASE PATH

If you’re combining files that share the same ancestor directory, you can use
the “b” argument to set the base directory for the “f” argument. Do not include
the leading or trailing “/” characters.

E.g., the following URLs will serve the exact same content:
http://example.com/min/?f=scripts/jquery-1.2.6.js,scripts/site.js,scripts/home.js
http://example.com/min/?b=scripts&f=jquery-1.2.6.js,site.js,home.js

Got anything to say? Go ahead and leave a comment!