[英]php javascript minifier
I'm using a shared hosting plan on Hostgator. 我在Hostgator上使用共享主机方案。 As such I can't run any java from the command line. 因此我无法从命令行运行任何java。
Is there any pure PHP minifiers out there that I can use? 有没有我可以使用的纯PHP缩小器? Minify uses YUICompressor.jar in the background so that won't work. Minify在后台使用YUICompressor.jar,因此不起作用。
Anyone know of something that uses just PHP to minify javascript that I can run from the command line? 有人知道只使用PHP来缩小我可以从命令行运行的javascript吗? I would also like it to shrink variable names. 我也希望它缩小变量名称。
You could use the google js minifier. 你可以使用谷歌js minifier。 Here's a python script which uses it to compress a bunch of javascript files with it: 这是一个python脚本,用它来压缩一堆javascript文件:
import httplib
import simplejson as json
import urllib
import sys
def combine_js(js_files, minify=False):
files = list(js_files) # create a copy which we can modify
if not minify:
code = []
for path in files:
f = open(path, 'r')
code.append(f.read())
f.close()
return '\n\n'.join(code)
def _minify(code):
params = urllib.urlencode([
('js_code', code),
('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
('output_format', 'json'),
('output_info', 'compiled_code'),
('output_info', 'errors'),
('output_info', 'warnings'),
])
headers = {'Content-type': "application/x-www-form-urlencoded"}
conn = httplib.HTTPConnection('closure-compiler.appspot.com')
conn.request('POST', '/compile', params, headers)
response = conn.getresponse()
data = json.loads(response.read())
conn.close()
if not data.get('compiledCode'):
print >>sys.stderr, 'WARNING: Did not get any code from google js compiler.'
print >>sys.stderr, data
print >>sys.stderr
print >>sys.stderr, 'Using unminified code'
return None
return data.get('compiledCode')
# Minify code in chunks to avoid the size limit
chunks = []
code = ''
while len(files):
path = files[0]
f = open(path, 'r')
data = f.read()
f.close()
# Check if we reach the size limit
if len(code + data) >= 1000000:
res = _minify(code)
if res is None:
# Fallback to unminified code
return combine_js(js_files)
chunks.append(res)
code = ''
continue
code += data
del files[0]
if code:
res = _minify(code)
if res is None:
# Fallback to unminified code
return combine_js(js_files)
chunks.append(res)
return '\n\n'.join(chunks).strip()
if __name__ == '__main__':
print combine_js(sys.argv[1:], True)
Usage: python filename.py path/to/your/*.js > minified.js
用法: python filename.py path/to/your/*.js > minified.js
In case you are using an ancient python version and do not have simplejson installed on your system, here's how you can get the script to work with a local simplejson version (run those commands via SSH): 如果你使用古老的python版本并且没有在你的系统上安装simplejson,那么你可以在这里使用本地simplejson版本(通过SSH运行这些命令):
cd
wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.3.2.tar.gz
tar xzf simplejson-2.3.2.tar.gz
export PYTHONPATH=$HOME/simplejson-2.3.2/
Minify's default is not YUIC, but a native PHP port of JSMin plus bug fixes. Minify的默认值不是YUIC,而是JSMin的本机PHP端口以及错误修复。
Other native PHP Javascript compressors I've come across: 我遇到的其他本机PHP Javascript压缩器:
If you consider other javascript minifiers/compressors, take a look at a PHP port of dean edward's packer: http://joliclic.free.fr/php/javascript-packer/en/ 如果您考虑其他javascript minifiers / compressors,请查看dean edward的打包器的PHP端口: http : //joliclic.free.fr/php/javascript-packer/en/
There is an online demo available so you can test it online (highly recommended before you try to install it on your own server). 有一个在线演示可供您在线测试(强烈建议您在尝试将其安装在自己的服务器上之前)。 I quick online test gave me back a correct working minified javascript file. 我快速在线测试给了我一个正确的工作缩小的javascript文件。 It should get the job done. 它应该完成工作。
Find an online web based JS minifier, and query their <form>
through PHP with cURL or file_get_contents
or similar. 找到一个基于Web的在线JS minifier,并使用cURL或file_get_contents
或类似的方法通过PHP查询他们的<form>
。 I suppose you may have to ask the online service for permission before using their service in this way. 我想你可能需要在以这种方式使用他们的服务之前询问在线服务的许可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.