简体   繁体   English

YUIC压缩器或类似的PHP?

[英]YUICompressor or similar in PHP?

I've been using yuicompressor.jar on my test server for on-the-fly minimisation of changed JavaScript files. 我一直在我的测试服务器上使用yuicompressor.jar来快速最小化已更改的JavaScript文件。 Now that I have deployed the website to the public server, I noticed that the server's policies forbid the use of exec() or its equivalents, so no more java execution for me. 现在我已经将网站部署到公共服务器,我注意到服务器的策略禁止使用exec()或其等价物,所以不再需要java执行。

Is there a decent on-the-fly JS compressor implemented in PHP? 用PHP实现了一个不错的即时JS压缩器吗? The only thing resembling this that I was able to find was Minify, but it's more of a full-blown compression solution with cache and everything. 我能找到的唯一类似的东西是Minify,但它更像是一个带有缓存和一切的全面压缩解决方案。 I want to keep the files separate and have the minimised files follow my own naming conventions, so Minify is a bit too complex for this purpose. 我想保持文件分离,并使最小化的文件遵循我自己的命名约定,因此Minify对于此目的来说有点过于复杂。

The tool, like yuicompressor, should be able to take either a filename or JavaScript as input and should either write to a file or output the compressed JavaScript. 该工具,如yuicompressor,应该能够采用文件名或JavaScript作为输入,并应该写入文件或输出压缩的JavaScript。

EDIT: To clarify, I'm looking for something that does not have to be used as a standalone (ie it can be called from a function, rather than sniffing my GET variables). 编辑:为了澄清,我正在寻找一些不必作为独立使用的东西(即它可以从函数中调用,而不是嗅探我的GET变量)。 If I just wanted a compressor, Minify would obviously be a good choice. 如果我只想要一台压缩机,Minify显然是一个不错的选择。

EDIT2: A lot has changed in the five years since I asked this question. 编辑2:自从我提出这个问题以来的五年里,情况发生了很大变化。 Today I would strongly recommend separating the front-end workflow from the server code. 今天我强烈建议将前端工作流程与服务器代码分开。 There are plenty of good tools for JS development around and except for the most trivial jQuery enhancements it's a better idea to have a full workflow with automated bundling, testing and linting in place and just deploy the minified bundles rather than the raw files. 有很多很好的JS开发工具,除了最简单的jQuery增强功能之外,最好有一个完整的工作流程,自动捆绑,测试和linting,只需部署缩小的bundle而不是原始文件。

Yes there is, it's called minify . 是的,它被称为缩小

The only thing in to worry about in the way of complexity is setting up a group , and there's really nothing to it. 在复杂性方面唯一需要担心的是建立一个小组 ,而且实际上并没有什么。 Edit the groupsConfig.php file if you want multiple JS/CSS in one <script> or <link> statement: 如果您想在一个<script><link>语句中使用多个JS / CSS,请编辑groupsConfig.php文件:

return array(
     'js-common' => array('//js/jquery/jquery-1.3.2.min.js', '//js/common.js', '//js/visuals.js',
'//js/jquery/facebox.js'),
     'css-common' => array('//css/main.css', '//css/layout.css','//css/facebox.css')
);

To include the above 'js-common' group, do this: 要包含上述'js-common'组,请执行以下操作:

<script type="text/javascript" src="/min/g=js-common"></script>

(i know i was looking for the exact same thing not knowing how to deal directly with the jar file using php - that's how i ended up here so i'm sharing what i found) (我知道我正在寻找完全相同的东西,不知道如何使用PHP直接处理jar文件 - 这就是我如何在这里结束所以我分享我发现的东西)

Minify is a huge library with tons of functionalities. Minify是一个拥有大量功能的庞大图书馆。 However the minifying part is a very tiny class : http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/YUICompressor.php 然而,缩小部分是一个非常小的类: http//code.google.com/p/minify/source/browse/trunk/min/lib/Minify/YUICompressor.php

& very very easy to use : 非常容易使用:

//set the path to the jar file
Minify_YUIcompressor::$jarFile=_ROOT.'libs/java/yuicompressor.jar';
//set the path to a writable temp folder
Minify_YUIcompressor::$tempDir=_ROOT.'temp/';

//minify
$yourcssminified=Minify_YUIcompressor::minifyCss($yourcssstringnotminified,$youroptions)

same process for js, if you need more functionalities just pick from the library & read the source to see how you can make direct call from your app. js的流程相同,如果您需要更多功能,只需从库中选择并阅读源代码,了解如何从您的应用程序直接调用。

I didn't read the question well, since minify is based on using the jar files, the op can't use it anyway with his server config 我没有很好地阅读这个问题,因为minify基于使用jar文件,op无论如何都不能使用它的服务器配置

Minify also include other minifying methods than yui, for example: Minify还包括除yui之外的其他缩小方法,例如:

http://code.google.com/p/minify/source/browse/trunk/min/lib/JSMinPlus.php?r=443&spec=svn468 http://code.google.com/p/minify/source/browse/trunk/min/lib/JSMinPlus.php?r=443&spec=svn468

Try Lissa : 试试Lissa

Lissa is a generic CSS and JavaScript loading utility. Lissa是一个通用的CSS和JavaScript加载实用程序。 Lissa is an extension of the YUI PHP Loader aimed at solving one of the current loader limitations; Lissa是YUI PHP Loader的扩展,旨在解决当前加载器限制之一; combo loading. 组合加载。 YUI PHP Loader ships with a combo loader that is capable of reducing HTTP requests and increasing performance by outputting all the YUI JavaScript and/or CSS requirements as a single request per resource type. YUI PHP Loader附带了一个组合加载器,它能够通过将每个YUI JavaScript和/或CSS要求作为每个资源类型的单个请求输出来减少HTTP请求并提高性能。 Meaning even if you needed 8 YUI components which ultimately boil down to say 13 files you would still only make 2 HTTP requests; 意思是即使您需要8个YUI组件,最终归结为13个文件,您仍然只能发出2个HTTP请求; one for the CSS and another for the JavaScript. 一个用于CSS,另一个用于JavaScript。 That's great, but what about custom non-YUI resources. 这很好,但是自定义非YUI资源呢。 YUI PHP Loader will load them, but it loads them as separate includes and thus they miss out on benefits of the combo service and the number of HTTP requests for the page increases. YUI PHP Loader将加载它们,但是它将它们作为单独的包含加载,因此它们错过了组合服务的好处以及页面的HTTP请求数量的增加。 Lissa works around this limitation by using the YUI PHP Loader to handle the loading and sort of YUI and/or custom resource dependencies and pairs that functional with Minify. Lissa通过使用YUI PHP Loader处理YUI和/或自定义资源依赖关系的加载和排序以及与Minify一起使用的对来解决此限制。

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

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