如何轻松地在PHP中简化JS ......或者别的什么 [英] How to minify JS in PHP easily...Or something else

查看:134
本文介绍了如何轻松地在PHP中简化JS ......或者别的什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些环顾四周,但我仍然感到困惑。



我尝试过Crockford的JSMin,但是Win XP无法解压缩可执行文件出于某种原因。



我真正想要的是一个简单易用的JS minifier,它使用PHP来缩小JS代码 - 并返回结果。 / p>

原因是因为:
我有2个文件(例如)我正在处理它们:scripts.js和scripts_template.js



scripts_template是我写的普通代码 - 然后我必须缩小它并将缩小的脚本粘贴到scripts.js中 - 我实际在我的网站上使用的那个。



我想通过在我的页面上做这样的事情来消除中间人:

 < script type =text / javascriptsrc =scripts.php>< / script> 

然后是scripts.php的内容:

 <?php include(include.inc);标题( 内容类型:文本/ JavaScript的);回波(minify_js(的file_get_contents( scripts_template.js))); 

这样,每当我更新我的JS时,我都不必经常去网站缩小它并将其重新粘贴到scripts.js中 - 一切都会自动更新。



是的,我也尝试过Crockford的PHP Minifier,我看了一下PHP Speedy,但我还不了解PHP类......那里有猴子可以理解的东西,也许是RegExp的东西?



我们怎么样让这更简单?



我只想删除制表符空格 - 我仍然希望我的代码可读。



这不像剧本使我的网站显得滞后,只是一切都比没有好。



Tab删除,有人吗?如果可能的话,如何完全删除BLANK行?

解决方案

我使用了 PHP实现 Douglas Crockford 已经有一段时间了。连接文件时可能会有点风险,因为在闭包结束时可能会丢失分号。



缓存缩小的文件是明智的想法输出和回显缓存的内容,只要它比源文件更新。

  require'jsmin.php'; 

if(filemtime('scripts_template.js')< filemtime('scripts_template.min.js')){
read_file('scripts_template.min.js');
} else {
$ output = JSMin :: minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js',$ output);
echo $ output;
}

您也可以尝试 JShrink 。我之前从未使用过它,因为之前我没有遇到过JSMin的困难,但下面的代码应该可以解决问题。我没有意识到这一点,但JShrink需要PHP 5.3和命名空间。

  require'JShrink / Minifier.php'; 

if(filemtime('scripts_template.js')< filemtime('scripts_template.min.js')){
read_file('scripts_template.min.js');
} else {
$ output = \ JShrink \ Minin :: minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js',$ output);
echo $ output;
}


I've done some looking around, but I'm still confused a bit.

I tried Crockford's JSMin, but Win XP can't unzip the executable file for some reason.

What I really want though is a simple and easy-to-use JS minifier that uses PHP to minify JS code--and return the result.

The reason why is because: I have 2 files (for example) that I'm working between: scripts.js and scripts_template.js

scripts_template is normal code that I write out--then I have to minify it and paste the minified script into scripts.js--the one that I actually USE on my website.

I want to eradicate the middle man by simply doing something like this on my page:

<script type="text/javascript" src="scripts.php"></script>

And then for the contents of scripts.php:

<?php include("include.inc"); header("Content-type:text/javascript"); echo(minify_js(file_get_contents("scripts_template.js")));

This way, whenever I update my JS, I don't have to constantly go to a website to minify it and re-paste it into scripts.js--everything is automatically updated.

Yes, I have also tried Crockford's PHP Minifier and I've taken a look at PHP Speedy, but I don't understand PHP classes just yet...Is there anything out there that a monkey could understand, maybe something with RegExp?

How about we make this even simpler?

I just want to remove tab spaces--I still want my code to be readable.

It's not like the script makes my site lag epically, it's just anything is better than nothing.

Tab removal, anyone? And if possible, how about removing completely BLANK lines?

解决方案

I have used a PHP implementation of JSMin by Douglas Crockford for quite some time. It can be a little risky when concatenating files, as there may be a missing semicolon on the end of a closure.

It'd be a wise idea to cache the minified output and echo what is cached so long as it's newer than the source file.

require 'jsmin.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
  read_file('scripts_template.min.js');
} else {
  $output = JSMin::minify(file_get_contents('scripts_template.js'));
  file_put_contents('scripts_template.min.js', $output);
  echo $output;
}

You could also try JShrink. I haven't ever used it before, since I haven't had difficulty with JSMin before, but this code below should do the trick. I hadn't realized this, but JShrink requires PHP 5.3 and namespaces.

require 'JShrink/Minifier.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
  read_file('scripts_template.min.js');
} else {
  $output = \JShrink\Minifier::minify(file_get_contents('scripts_template.js'));
  file_put_contents('scripts_template.min.js', $output);
  echo $output;
}

这篇关于如何轻松地在PHP中简化JS ......或者别的什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆