Looking for a CSS and for a js file merger and minimizer [closed]

泪湿孤枕 提交于 2019-12-12 01:58:15

问题


I'm working on a public project and one of the things we are after is a CSS file merger and minimizer and a js file merger and minimizer. The requisites are as follows:

  • No beta code. Only stable code.
  • Must be written in php.
  • Must work in php 5.4.
  • Must be compatible with php 5.3
  • The CSS minimizer program may be completely different from the js minimizer (it does not have to be a 2 in 1)
  • The minimized result must work somewhat so that it can be dumped to a file. Browsers only access the dumped file. This must be able to work with loads of work so minify, great tool but not an option.

If the system is only a minimizer I can do the merging in the server myself (string concatenation) and use the minimizer later.

When there's something new to compress, I use the system (e.g. a class, a function, etc...) that I call when I want to minify the js or the CSS. Its output is supposed to be placed to a file, so if it's output can go directly to a file great(!), if it's just a string returned to the program, good(!) My program is the one who knows when the compressed file is supposed to be substituted and the one who deals the access to the file.

This is for a project that must work where all outside communication by the php's initiative is disabled and where most functionalities are disabled to somewhat sound like. That means no url includes, no curl, no url file_get_contents(), no exec(), etc...

Note: There are lots of questions in this website about this subject. The problem is that most of them are to generalist and do not answer what I need (Ex: is more interested in suggesting YUI or compressors that are currently known to have bugs).

I already tried to find this in the internet but I can only find old abandoned projects or I get compressors that require the use of the command line interface.


回答1:


For CSS, I personally use CSSMin with great results. It is a plain PHP solution, and it is even able to simulate CSS variables, with a @variable declaration.

For JS files, I call Google Closure Compiler with curl which does not meet your needs.




回答2:


If you want to get away from CLI your best bet is probably using an online tool (accessing it using cURL) or a Java-based one (using exec() method).

It seems you'd be more comfortable using something web-based and for that I recommend UglifyJS for Javascript files and Prefixr for CSS.

Note that Prefixr won't compress the input by default: its focus is on adding vendor prefixes to the newest CSS properties (which is essential these days). But by sending a POST parameter 'compress_option' you'll get the output compressed. Anyway, as [Prefixr's API usage page][5] doesn't supply a PHP cURL example, here you go:

$concatenated_content = '.any_css {color: #000;}';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://prefixr.com/api/index.php");
curl_setopt($curl, CURLOPT_POSTFIELDS, array('css' => $concatenated_content, 'compress_option' => 'on'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$compressed_content = curl_exec($curl);
curl_close($curl);

If you otherwise want to use a local tool, I recommend YUI compressor (http://developer.yahoo.com/yui/compressor/) and Google Closure (https://developers.google.com/closure/, use it only for JS, the Google Closure Stylesheets is yet immature in my opinion). In this case you can use PHP exec() function as mentioned above and the CLI won't hurt you.



来源:https://stackoverflow.com/questions/9633829/looking-for-a-css-and-for-a-js-file-merger-and-minimizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!