转载请注明出处^_^

一般写前端的难免会同时用到好几个js,js一多页面就各种崩溃了,今天无聊尝试了下php实现js的压缩,拿出来和大家分享一下

首先秉承面向对象复用的实现,当然要先google+github一番先

结果是确实有人做了类似的东西,所以嘛,挑一个来用,嗯,我挑这个 https://github.com/rgrove/jsmin-php

这个库压缩起来很简单,基本就JSMin::minify($jsStr)就哦了·

然后就是自己封装一下,让js文件访问能够智能地被压缩成一个文件返回

有了这个出发点,我们假设我要请求1.js 2.js两个文件,如果我请求http://xxx.com/jss.php?jss=1.js,2.js 就行了岂不是就能达到想要的效果了咩~?

ok,就这样开始

首先取参数

define(DIR_CACHE, './cache/'); //cache files dir

define(DIR_JS, './jss/');//jss files dir

$params=trim($_GET['jss']);//get js file list

$list=explode(',',$params);

foreach ($list as $key => &$value) {

if (!trim($value)) {

unset($list[$key]);

}else{

$value=eregi_replace('\.+/', '', $value);

}

}

为了防止非法访问上层目录,加上了个正则替换,把./等给过滤了··

然后为了下次请求的时候能够省去再次压缩的时间(压缩js还是挺慢的,经过测试,压缩jquery的源文件就花去了接近一秒,天啊···),必须做个缓存

那就建个cache目录被,然后就是md5一下文件名存一份压缩后的数据

但还有一个问题是js加载的先后是能影响页面最终的效果的,所以····你懂的,得把最终压缩后的单个文件和拼串后的文件都缓存一份,例如再次访问http://xxx.com/jss.php?jss=2.js,1.js我们就不直接取上次缓存的最终文件了,而是取出2.js的压缩文件和1.js的压缩文件进行拼接,然后再缓存一份2.js1.js的副本

代码大概如下

$path=DIR_CACHE.$product;

if (file_exists($path)) {//if exist product cache

$contents=file_get_contents($path);

echo $contents;

}else{

$contents='';

foreach ($list as $file) {

$path=DIR_CACHE.md5($file).'.js';

if (file_exists($path)) {//if exist file cache

$contents.=file_get_contents($path);

$contents.=' ';

}else{

$path=DIR_JS.$file;

if(file_exists($path)){

$temp=JSMin::minify(file_get_contents($path));

write2cache($temp,$file);

$contents.=$temp;

$contents.=' ';

}

}

}

if ($contents) {

write2cache($contents,$product);

echo $contents;

}

}

然后就是写缓存,这个简单了···直接上代码吧·

function write2cache($contents,$name){

$name=md5($name).'.js';

$fh=fopen(DIR_CACHE.$name,"w");

if ($fh) {

fwrite($fh, $contents);

fclose($fh);

return true;

}

return false;

}

好,基本就这样了··经测试效果杠杠的~~

对了,目录下自己创建两个目录,一个是jss,用来放需要压缩的js文件(里面可以再建文件夹分类),另一个是cache,用来缓存压缩后的文件。

结构基本如下

0818b9ca8b590ca3270a3433284dd417.png

完整代码:

require 'jsmin.php';

define(DIR_CACHE, './cache/'); //cache files dir

define(DIR_JS, './jss/');//jss files dir

header("Content-Type: text/javascript; charset=UTF-8");

$params=trim($_GET['jss']);//get js file list

$list=explode(',',$params);

foreach ($list as $key => &$value) {

if (!trim($value)) {

unset($list[$key]);

}else{

$value=eregi_replace('\.+/', '', $value);

}

}

$product=md5(json_encode($list)).'.js';//create product file name for cache

$path=DIR_CACHE.$product;

if (file_exists($path)) {//if exist product cache

$contents=file_get_contents($path);

echo $contents;

}else{

$contents='';

foreach ($list as $file) {

$path=DIR_CACHE.md5($file).'.js';

if (file_exists($path)) {//if exist file cache

$contents.=file_get_contents($path);

$contents.=' ';

}else{

$path=DIR_JS.$file;

if(file_exists($path)){

$temp=JSMin::minify(file_get_contents($path));

write2cache($temp,$file);

$contents.=$temp;

$contents.=' ';

}

}

}

if ($contents) {

write2cache($contents,$product);

echo $contents;

}

}

function write2cache($contents,$name){

$name=md5($name).'.js';

$fh=fopen(DIR_CACHE.$name,"w");

if ($fh) {

fwrite($fh, $contents);

fclose($fh);

return true;

}

return false;

}

github地址

文件就不传了,过两天把css的压缩也写一起再直接github ·^_^

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐