用于缩小css和javascript文件的命令行工具
shrink的Python项目详细描述
收缩
shrink是一个用于连接和压缩css样式表和 使它们更小的javascript文件。 缩小(或缩小)这些文件可以减少 在页面加载后生成,以及这些请求的大小。
此命令依赖于YUI Compressor进行压缩,并使用 Python2.5及以上版本,包括Python3。
安装
运行:
$ pip install shrink
安装后可以显示脚本信息和选项:
$ shrink -h
配置文件
INI样式文件用于知道将缩小哪些文件,请设置一些 全局选项,以及知道哪些文件将在 缩小。
熟悉SHRINK配置文件格式的一个好的起点是 阅读示例shrink配置文件。创建示例文件运行:
$ shrink --example-cfg
此命令在当前文件夹中创建名为example_shrink.cfg的文件。
配置文件格式
配置文件为每个可以生成的文件都有一个节, 最上面还有一个特殊的部分叫做DEFAULTwhere global 选项已定义。
全局DEFAULT选项:
- ^{tt5}$ defines a base directory used as prefix to find static files. This value can be referenced in any other section using the python variable notation ^{tt6}$.
- ^{tt7}$ defines a folder where ^{tt8}$ file is stored. See Shrink hash file for more info. By default, this file is stored in the same folder where shrink config file is located.
- ^{tt9}$ defines default values for some command line argumens. Supported arguments are ^{tt10}$ and ^{tt11}$. The values given here are overriden by the ones given during runtime as command line arguments.
每个文件节都有一些在连接、压缩和 文件的散列。这些文件节选项是:
- ^{tt12}$ value defines the folder where file(s) listed in ^{tt13}$ are located.
- ^{tt13}$ value can be a single file name, or a list of file names. When a list of names is given, each file in list is concatenated (from top to down) into a single file before compression.
- ^{tt15}$ value sets output directory for the minified file By default minified file is generated in source directory.
- ^{tt16}$ value is the name for the minified file.
- ^{tt17}$ is a boolean value. When it is true destination file is included during shrink hash generation. See Shrink hash file.
- ^{tt18}$ is a boolean value. Destination file is not compressed when this value is false. By default compression is done for destination files. This option is useful when is desirable to join many files without compressing them because they are already compressed.
例如,用于缩小名为sample-file.js的文件的节可以 写成:
[sample-single-file-js] source_directory = %(base_dir)s/js destination_file = sample-file.min.js source_files = sample-file.js
最终的缩小文件名将是sample-file.min.js。
许多文件也可以指定在 通过编写如下部分进行压缩:
[sample-multiple-file-css] source_directory = %(base_dir)s/css destination_file = sample-multiple-file.min.css source_files = sample-file1.css sample-file2.css sample-file3.css
生成的文件名由destination_filevalue给定。
最小化css和js文件
要缩小所有文件,请运行:
$ shrink -f example_shrink.cfg all
这将使用中的yuicompressor.jar和example_shrink.cfg文件 压缩所有文件的当前目录。
如果不是所有文件都需要缩小,也可以 通过以下方式缩小单个文件或一组文件(请参见Section groups) 使用每个节的名称而不是all作为参数。
要列出可用的节,请运行:
$ shrink -f example_shrink.cfg -l
截面组
而不是使用sample-single-file-js和 sample-multiple-file-css作为参数可以定义如下组:
[sample-group] group = sample-single-file-js sample-multiple-file-css
然后以sample-group作为唯一参数运行minifier脚本。
压缩哈希文件
缩小后,SHRINK可以创建包含SHA1哈希的文件。文件 当配置文件中至少有一个节具有hash = true时创建。搞砸 使用这些部分中所有目标文件的内容创建。
这有助于了解某些文件何时更改,以及重新加载静态css和 不使用时间戳或版本号的javascript文件。 有时需要重新加载修改过的静态文件而不增加 应用程序版本。在这些情况下,哈希可以用作请求参数 而不是版本号。
部署说明
当应用程序停止工作或出现未检查的结果时 它是用最小化的css和javascript文件部署的。 很多时候,有些问题是很容易避免的 在shrink.cfg设置期间,请注意以下事项:
- The order of the source files in each config section must be the same as the one in your HTML templates.
- CSS files normally contains URLs which are relative to the location of the file where they are declared. So for these cases the location for destination file must be the same as the one for source files. Some javascript files might define some path or URL that might also be relative to a file location.
- Check that all files wich are NOT minified are being included in your HTML template.
更改日志
1.1.1-2012年9月21日
- Setup argument use_2to3 is now enabled only for python 3 series
- Added ^{tt30}$ to README file
- Added read permissions to generated files for group and others
1.1.0-2012年7月31日
- Added python 3 support
- Updated documentation
- Added initial files for unit testing
1.0.1-2012年7月19日
- Added –hash-dir argument to allow changing hash file dir during runtime
- Added ^{tt18}$ INI file option to avoid compressing destination file
- Added –example-cfg argument to create an example_shrink.cfg file in current folder
1.0.0-2012年7月11日
- Added –version argument
- Added SHA1 hashing support (^{tt28}$ in any file section)
- Added –hash-all argument to generae SHA1 hash using all files contents