简体   繁体   English

在php中组合和压缩多个JavaScript文件

[英]Combining and Compressing multiple JavaScript files in php

I am working on a PHP app that requires eight javascript files (hello web2.0). 我正在开发一个需要8个javascript文件的PHP应用程序(hello web2.0)。

I am wondering what the best way combine and compress all of the files dynamically. 我想知道什么是动态组合和压缩所有文件的最佳方式。 Am I phrasing the question correctly? 我是否正确地表达了这个问题?

The end result is that I would include one .js file in the header, and that .js file would include of the .js files in my "includes/js" directory. 最终结果是我在头文件中包含一个.js文件,而.js文件将在我的“includes / js”目录中包含.js文件。

Thanks. 谢谢。

You can use jsmin-php 你可以使用jsmin-php

Their sample code is: 他们的示例代码是:

require 'jsmin-1.1.1.php';

// Output a minified version of example.js.
echo JSMin::minify(file_get_contents('example.js'));

You may easily join several js files by doing something like: 您可以通过以下操作轻松加入多个js文件:

require 'jsmin-1.1.1.php';

// Output a minified version of example.js.
echo JSMin::minify(file_get_contents('example.js') . file_get_contents('example2.js'));

I've used Minify with my jQuery/PHP projects with lots of success. 我使用Minify和我的jQuery / PHP项目取得了很大的成功。 It includes caching too so there isn't much overhead. 它也包括缓存,因此没有太多开销。

I ended up changing things around and using .htaccess to route all requests to my javascript folder to the minify script, so for example: 我最后改变了一些事情并使用.htaccess将所有请求路由到我的javascript文件夹到minify脚本,例如:

<script type="text/javascript" src="/js/jquery.js,js/jquery-levitip.js,js/jquery-facebox.js,js/datepicker.js,js/ga.js"></script>

with my front-end HTML routes into my minify script and it returns all those scripts as one, compressed and minified. 将我的前端HTML路由放入我的minify脚本中,它将所有这些脚本作为一个脚本返回,压缩和缩小。 That way I can define the includes normally and everything happens behind the scenes. 这样我就可以正常定义包含,一切都在幕后发生。

But anyway check the user guide on their site, it's very well documented, and you should be able to pull things off the way you want too. 但是无论如何要检查他们网站上的用户指南 ,这是非常好的文档记录,你应该能够以你想要的方式拉出你的东西。 Oh and it works for CSS too. 哦,它也适用于CSS。

您可以使用Yahoo JavaScript Compressor这是一个命令行工具,您可以从PHP脚本开始。

You can try PHP Speedy This will combine and compress your JS Files for you into 1 single file. 您可以尝试PHP Speedy这将把您的JS文件组合并压缩为1个单独的文件。 It will also compress your HTML and CSS and your page will load a lot faster. 它还会压缩您的HTML和CSS,您的页面加载速度会快得多。

 function compress($buffer) {
        /* remove comments */
        $buffer = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", "", $buffer);
        /* remove tabs, spaces, newlines, etc. */
        $buffer = str_replace(array("\r\n","\r","\t","\n",'  ','    ','     '), '', $buffer);
        /* remove other spaces before/after ) */
        $buffer = preg_replace(array('(( )+\))','(\)( )+)'), ')', $buffer);
        return $buffer;
    }

Source: http://castlesblog.com/2010/august/14/php-javascript-css-minification 资料来源: http//castlesblog.com/2010/august/14/php-javascript-css-minification

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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