Commit b5dca5f6 by Timothée Peignier

update and improve documentation

parent 33b025d1
......@@ -14,12 +14,19 @@ Configuration and list of available settings for django-compress
Specifying files
================
You specify groups of files to be compressed in your settings.
You specify groups of files to be compressed in your settings. You can use glob
syntax to select multiples files.
The basic syntax for specifying CSS/JavaScript groups files is ::
COMPRESS_CSS = {
'group_one': {
'source_filenames': ('css/style.css', 'css/foo.css', 'css/bar.css'),
'source_filenames': (
'css/style.css',
'css/foo.css',
'css/button/*.css',
'css/bar.css'
),
'output_filename': 'css/one_compressed.css',
'extra_context': {
'media': 'screen,projection',
......@@ -30,8 +37,13 @@ The basic syntax for specifying CSS/JavaScript groups files is ::
COMPRESS_JS = {
'all': {
'source_filenames': ('js/jquery-1.2.3.js', 'js/jquery-preload.js', 'js/jquery.pngFix.js',
'js/my_script.js', 'js/my_other_script.js'),
'source_filenames': (
'js/jquery-1.2.3.js',
'js/jquery-preload.js',
'js/jquery.pngFix.js',
'js/my_script.js',
'js/my_other_script.js'
),
'output_filename': 'js/all_compressed.js',
}
}
......@@ -39,35 +51,88 @@ The basic syntax for specifying CSS/JavaScript groups files is ::
Group options
-------------
* ``source_filenames`` is a tuple with the source files to be compressed.
The files are concatenated in the order it is specified in the tuple. This option is required.
* ``output_filename`` is the filename of the (to be) compressed file. This option is required.
* ``extra_context`` is a dictionary of values to add to the template context,
``source_filenames``
....................
**Required**
Is a tuple with the source files to be compressed.
The files are concatenated in the order it is specified in the tuple.
``output_filename``
...................
**Required**
Is the filename of the (to be) compressed file.
``extra_context``
.................
**Optionnal**
Is a dictionary of values to add to the template context,
when generating the HTML for the HTML-tags with the templatetags.
This option is not required and can be left out.
For CSS, if you do not specify ``extra_context``/``media``, the default media in
the ``<link>`` output will be ``media="all"``.
Note that all filenames are specified relative to ``COMPRESS_ROOT``, and thus the source
files needs to be in your ``COMPRESS_ROOT``.
.. note::
Note that all filenames are specified relative to ``COMPRESS_ROOT``, and thus the source
files needs to be in your ``COMPRESS_ROOT``.
Other settings
--------------
* ``COMPRESS``: When ``COMPRESS`` is ``True``, CSS and JavaScripts will be concatenated and filtered.
``COMPRESS``
............
When ``COMPRESS`` is ``True``, CSS and JavaScripts will be concatenated and filtered.
When ``False``, the source-files will be used instead.
Defaults to ``not DEBUG`` (compressed files will only be used in non-DEBUG-mode (production))
* ``COMPRESS_AUTO``: Auto-generate CSS and JavaScript files whenever needed,
when the template tags are invoked.
Defaults to ``not DEBUG`` (compressed files will only be used when ``DEBUG`` is ``False``)
``COMPRESS_AUTO``
.................
Auto-generate CSS and JavaScript files whenever needed, when the template tags
are invoked.
This setting will make sure that the outputted files always are up to date
(assuming that you are using the provided templatetags to output the links to
your files).
If you disable this, you can use the management command to keep your files
manually updated. Defaults to ``True``.
* ``COMPRESS_VERSION``: Regulates whether or not to add a "version number" to the outputted files filename with for use with “far future Expires”. For more information, see [FarFutureExpires]. When you specify ``COMPRESS_VERSION`` you will also need to add a placeholder (which by default is '?') for the version number in the ``output_filename`` setting.
* ``COMPRESS_VERSION_REMOVE_OLD``: When ``True``, old compressed files will be removed when new versions are generated. All files with a matching name e.g. ``output_filename`` where ? can be replaced by digits will be removed. If you for some reason have files named in the same way, you should consider moving them or putting the compressed files in their own directory. Defaults to ``True``.
manually updated.
Defaults to ``True``.
``COMPRESS_VERSION``
....................
Regulates whether or not to add a "version number" to the outputted files
filename with for use with “far future Expires”.
For more information, see :doc:`farfutureexpires`.
When you specify ``COMPRESS_VERSION`` you will also need to add a placeholder
(which by default is ``?``) for the version number in the ``output_filename`` setting.
``COMPRESS_VERSION_REMOVE_OLD``
...............................
``COMPRESS_VERSION`` example::
When ``True``, old compressed files will be removed when new versions are generated.
All files with a matching name e.g. ``output_filename`` where ``?`` can be replaced
by digits will be removed.
If you for some reason have files named in the same way, you should consider moving
them or putting the compressed files in their own directory.
Defaults to ``True``.
Example::
COMPRESS = True
COMPRESS_VERSION = True
......@@ -82,48 +147,37 @@ Other settings
},
}
This will output a file like ``/media/c/screen.r1213947531.css``, which will be re-generated and updated when you change your source files.
This will output a file like ``/media/c/screen.r1213947531.css``,
which will be re-generated and updated when you change your source files.
* ``COMPRESS_CSS_COMPRESSORS``: A tuple of filters to be applied to CSS files.
Defaults to ``('compress.compressors.yui.YUICompressor', )``.
* ``COMPRESS_JS_COMPRESSORS``: A tuple of filters to be applied to JavaScript files.
Defaults to ``('compress.compressors.yui.YUICompressor',)``
``COMPRESS_CSS_COMPRESSORS``
............................
.. note::
A tuple of compressors to be applied to CSS files.
Please note that in order to use YUI Compressor, you need to install YUI Compressor (see :doc:`ìnstallation` for more details).
Defaults to ``('compress.compressors.yui.YUICompressor',)``.
``COMPRESS_*_COMPRESSORS`` can be set to an empty tuple or None to not use any filters.
The files will however still be concatenated to one file.
``COMPRESS_JS_COMPRESSORS``
...........................
Prefix - An Alternative to MEDIA_URL
------------------------------------
A tuple of compressors to be applied to JavaScript files.
In cases where you want to deploy your compiled script and styles to somewhere
other than your MEDIA_URL, say a Content Delivery Network,
you can use the optional ``prefix`` parameter::
Defaults to ``('compress.compressors.yui.YUICompressor',)``
COMPRESS_CSS = {
'group_one': {
'source_filenames': ('css/style.css', 'css/foo.css', 'css/bar.css'),
'output_filename': 'css/one_compressed.css',
'extra_context': {
'media': 'screen,projection',
'prefix': 'http://cdn.example.com/'
},
},
# other CSS groups goes here
}
Also ``COMPRESS_*_COMPRESSORS`` can be set to an empty tuple or ``None`` to not use any compressor.
The files will however still be concatenated to one file.
In this example, the template tags will render ``http://cdn.example.com/css/one_compressed.css`` in the link tag.
You will need to manually put there after you build as part of your deployment process.
.. note::
CSS URL not starting with slash
-------------------------------
Please note that in order to use YUI Compressor, you need to install YUI Compressor (see :doc:`installation` for more details).
If source CSS contain a relative URL not starting with slash (i.e. relative to current file),
Rewriting CSS url()
-------------------
If source CSS contain a relative URL (i.e. relative to current file),
those URL will be converted to full relative path using ``COMPRESS_URL``.
This conversion is performed before any filters applied ::
This conversion is performed before any compressors are applied ::
media/js/fancybox/
fancybox.png
......@@ -197,32 +251,65 @@ some horsepower to google which leads us as hinted in the example above to the n
reason to have external css files (Yes there are css frameworks as well on the net
but they are often very small or generated to fit your needs)
Google AJAX Libraries API
-------------------------
YUI Compressor settings
=======================
So the reason for adding external urls support to django-compress is google ajax
libraries api support (example above) but you may want to use it however you want.
``COMPRESS_YUI_BINARY``
-----------------------
The advantages for offloading huge javascript libraries to google cdn is of course
that your site will need a lot less bandwidth, even if you use far futures expires headers.
But the superior reason is of course that the more sites that uses it, the bigger
the chance is that your favorite js framework is already cached in your visitors browser.
Command line to execute for the YUI program.
You will most likely change this to the location of yui-compressor on your system.
Google also uses far future expires headers so don't worry about that.
Don't worry about latency outside the US either. Here in sweden I measured a latency of 39ms.
Defaults to ``'/usr/local/bin/yuicompressor'``.
To sum somethings up, it's up to you and your situation to decide if merging all js
files or offloading js libraries to google gives your site the best performance.
Both ways are great to achieve great performance.
``COMPRESS_YUI_CSS_ARGUMENTS``
------------------------------
For a complete list of javascript libraries supported go to http://code.google.com/apis/ajaxlibs/
Additional arguments to use when compressing CSS.
YUI Compressor settings
=======================
* ``COMPRESS_YUI_BINARY``: command line to execute for the YUI program.
Defaults to ``'java -jar yuicompressor.jar'``. You will most likely change this to the location of yuicompressor on your system.
* ``COMPRESS_YUI_CSS_ARGUMENTS``: Additional arguments to use when compressing CSS.
Defaults to ``''``.
* ``COMPRESS_YUI_JS_ARGUMENTS``: Additional arguments to use when compressing JavaScript.
``COMPRESS_YUI_JS_ARGUMENTS``
-----------------------------
Additional arguments to use when compressing JavaScript.
Defaults to ``''``.
Closure Compiler settings
=========================
``COMPRESS_CLOSURE_BINARY``
---------------------------
Command line to execute for the Closure Compiler program.
You will most likely change this to the location of closure on your system.
Default to ``'/usr/local/bin/closure'``
``COMPRESS_CLOSURE_ARGUMENTS``
------------------------------
Additional arguments to use when closure is called.
Default to ``''``
UglifyJS settings
=================
``COMPRESS_UGLIFYJS_BINARY``
----------------------------
Command line to execute for the Closure Compiler program.
You will most likely change this to the location of closure on your system.
Defaults to ``'/usr/local/bin/uglifyjs'``.
``COMPRESS_UGLIFYJS_ARGUMENTS``
-------------------------------
Additional arguments to use when uglifyjs is called.
Default to ``''``
......@@ -4,21 +4,31 @@
Customization
=============
.. note::
Describes how to customize and add your own functionality to django-compress
django-compress can be customized an a couple of ways.
``django-compress`` can be customized an a couple of ways.
If you need to change the output of the HTML-tags generated from the templatetags,
this can be done by overriding the templates ``compress/css.html`` and ``compress/js.html``.
You can also write your own compressor-class, if you for example want to implement
other types of compressors.
All you need to do is to define a compress_css and/or a compress_js functions in a
class that inherits from ``compress.compressors.CompressorBase``,
and specify it in the tuple of filters (``COMPRESS_CSS_COMPRESSORS``/``COMPRESS_JS_COMPRESSORS``)
(see :doc:`configuration` for more information) in the settings.
For now, see the files under `compressors/` for more details.
\ No newline at end of file
All you need to do is to create a class that inherits from ``compress.compressors.CompressorBase``
and implements ``compress_css`` and/or a ``compress_js`` when needed.
Finally, specify it in the tuple of compressors ``COMPRESS_CSS_COMPRESSORS`` or
``COMPRESS_JS_COMPRESSORS`` (see :doc:`configuration` for more information) in the settings.
Example
=======
A custom compressor for a imaginary compressor called jam ::
from compress.compressors import CompressorBase
class JamCompressor(CompressorBase):
def compress_js(self, js):
return jam.compress(js)
def compress(self, css):
return jam.compress(css)
.. _ref-farfuturexpires:
.. _ref-farfutureexpires:
==================
Far Future Expires
Far future expires
==================
Details about the "Far Future Expires" technique, and how to implement it with help from django-compress
Details about the "Far future expires" technique, and how to implement it with help from django-compress
General details and considerations about the "Far Future Expires" technique
General details and considerations about the "Far future expires" technique
===========================================================================
If you really want to make sure that your CSS and JavaScript files get cached by
......@@ -15,14 +15,14 @@ This means that you send the Expires HTTP-header with a date that is far in the
resulting in the file being cached at the client (almost) forever
If you do not specify an Expires header, the files will be requested again,
and the server will respond "304 Not Modified" - which means that the file that
and the server will respond "*304 Not Modified*" - which means that the file that
the client got in its cache is still up-to-date. *However, a HTTP request will still be made anyway.*
The actual data does not get sent, but there will still be unnecessary HTTP requests, which can be avoided.
Most modern browser implement "Heuristic expiration", so it will actually not be
"304 Not Modified" for your static files on every request, however, the
Most modern browser implement "*Heuristic expiration*", so it will actually not be
"*304 Not Modified*" for your static files on every request, however, the
`HTTP specification does not specify <http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html>`_ (see section 13.2.2)
how "Heuristic expiration" should be handled by the browsers.
how "*Heuristic expiration*" should be handled by the browsers.
Therefore it is recommended to explicitly set the Expires header, to make sure all
browsers cache the files in the preferred way.
......@@ -30,16 +30,18 @@ This technique obviously has a drawback, when you want to change your files,
you need to change their names, since their URLs will be cached... that's right: forever!
The solution to that is pretty obvious too: change the URLs.
django-compress with COMPRESS_VERSION
=====================================
COMPRESS_VERSION
================
If you specify ``COMPRESS_VERSION`` in your configuration, you can add a placeholder
to the filenames to output the version number of the different files.
See :doc:`configuration` for more information.
The filename itself will be changed when any of the source files are updated.
Configuration of web server (that serves MEDIA_URL)
===================================================
See :doc:`configuration` for more information.
Configuration of web server
===========================
Apache
------
......@@ -57,12 +59,11 @@ For example ::
ExpiresDefault "access plus 10 years"
</Directory>
Apache also allows the specify files by their mime-type:
<pre><code>
ExpiresActive On
ExpiresByType text/css "access plus 10 years"
ExpiresByType application/x-javascript "access plus 10 years"
</code></pre>
Apache also allows the specify files by their mime-type ::
ExpiresActive On
ExpiresByType text/css "access plus 10 years"
ExpiresByType application/x-javascript "access plus 10 years"
lighttpd
--------
......
......@@ -10,25 +10,27 @@ It adheres to the DRY principle, you set up your CSS-files and JavaScript-files
in your project settings, and you are done.
There is no need to enter the names in your template again (a couple of template tags takes care of that).
* Group/concatenate your CSS/JavaScript source files to single files,
and run the code through specified compressors
* Group and concatenate your stylesheets and jaavscripts source files to single files,
and run the code through specified compressors.
* Provides sane defaults for CSS and JavaScript minification (YUI compressor required).
* A compressor for YUI Compressor, Google Closure Compiler, UglifyJS is included.
* A compressor for **YUI Compressor**, **Google Closure Compiler**, **UglifyJS** is included.
* It’s easy and straightforward to write custom compressors to generate the output.
* A last-modifcation/version part can automatically be appended to the filename
* A last-modification and/or version part can automatically be appended to the filename
of the outputted files to make use of “far future Expires HTTP header),
and provide an automated way of updating the content without delays.
* You setup and group your CSS and JavaScript-files in the settings,
and set up whetter or not you want your files to be compressed.
* Templatetags to output CSS (<link...>) or JavaScript (<script...>) tags
* Templatetags to output CSS (``<link>``) or JavaScript (``<script>``) tags
with the compressed files if ``COMPRESS = True``, or otherwise the source files.
* The compressed files will be updated (if needed) when you are accessing them
from the templatetags. This only applies when ``COMPRESS_AUTO`` is enabled.
from the templatetags.
This only applies when ``COMPRESS_AUTO`` is enabled.
You can manually update them with ``./manage.py synccompress``.
......@@ -49,7 +49,7 @@ Table Of Contents
configuration
versioning
customization
farfuturexpires
farfutureexpires
backwardsincompatiblechanges
Indices and tables
......
......@@ -5,25 +5,27 @@ Installation
============
Installation is pretty straight-forward and follows the same installation
procedure as many other Django applications.
django-compress does not create any models, so you do not need to configure a database.
procedure as many other *Django* applications.
Recommendations
===============
By default django-compress uses YUI Compressor to compress CSS and JS.
YUI Compressor is an excellent stand-alone application for dealing with JS and CSS-files.
YUI Compressor can be downloaded from: http://developer.yahoo.com/yui/compressor/.
If you do not install YUI COMPRESSOR, make sure to disable the compressor in your settings.
``django-compress`` does not create any models, so you do not need to configure a database.
Install instructions
====================
1. Either check out django-compress from GitHub_ or to pull a release off PyPI_.
Doing ``pip install django-compress`` or ``easy_install django-compress`` is all that should be required.
2. Add 'compress' to your INSTALLED_APPS
2. Add 'compress' to your ``INSTALLED_APPS``
.. _GitHub: http://github.com/pelme/django-compress
.. _PyPI: http://pypi.python.org/
Recommendations
===============
By default django-compress uses YUI Compressor to compress CSS and JS.
YUI Compressor is an excellent stand-alone application for dealing with JS and CSS-files.
YUI Compressor can be downloaded from: http://developer.yahoo.com/yui/compressor/.
If you do not install YUI COMPRESSOR, make sure to disable the compressor in your settings.
......@@ -21,13 +21,13 @@ Management command
You can update and force updates of the compressed file(s) with the management command “synccompress”.
This makes it possible to keep the files updated manually.
The command is ::
The command is (assuming you are in you project-folder that contains ``manage.py``). ::
manage.py synccompress
./manage.py synccompress
To force all files to be re-generated, use the argument ``--force`` ::
(assuming you are in you project-folder that contains ``manage.py``).
To force all files to be re-generated, use the argument ``--force`` (e.g. ``./manage.py synccompress --force``)
./manage.py synccompress --force
Templatetags
============
......@@ -44,7 +44,7 @@ depending on the ``COMPRESS`` setting, if you do not specify the ``COMPRESS`` se
the source files will be used in DEBUG-mode, and compressed files in non-DEBUG-mode.
Example
=======
-------
If you have specified the CSS-groups “screen” and “print” and a JavaScript-group
with the name “scripts”, you would use the following code to output them all ::
......@@ -53,8 +53,3 @@ with the name “scripts”, you would use the following code to output them all
{% compressed_css 'screen' %}
{% compressed_css 'print' %}
{% compressed_js 'scripts' %}
Far future Expires
==================
Far future Expires can be used with the bump_filename-option, see the :doc:`farfuturexpires` page for more information.
......@@ -7,33 +7,33 @@ Versioning
There are several ways for generating version strings. Basically, two types are available.
These are: mtime version strings and hash version strings.
mtime version strings (default)
===============================
Modification time version
=========================
This is the default method for generating version strings. In short, when invoked, it checks whether any of the source files system timestamps (mtime) is newer than the version string of the corresponding compressed file. If that is the case, the compressed output file version string will be the mtime of the most recent source file.
hash version strings
====================
Hash version
============
Hash-based versioning works by generating a hash string based on the contents of the source files. Available hash-based versioning methods are MD5 and SHA-1.
MD5 version strings
-------------------
MD5 version
-----------
To generate MD5 version strings, put this in your `settings.py` ::
COMPRESS_VERSIONING = 'compress.versioning.hash.MD5Versioning'
SHA-1 version string
--------------------
SHA-1 version
-------------
To generate SHA-1 version strings, put this in your `settings.py`::
COMPRESS_VERSIONING = 'compress.versioning.hash.SHA1Versioning'
Git version strings
===================
Git version
===========
Versions formed on git revisions in codebase. Provides a fast way to check if any of your files changed that
will be consistent across multiple web servers so that they all generate the same version numbers for each
......@@ -43,15 +43,15 @@ Assumes deployment is git repositiory. Requires GitPython 0.2.0.
GitPython 0.3.0 uses an async library that does not currently play well with Django. To install using Git just do
pip install GitPython==0.2.0-beta1.
Git revision version strings
----------------------------
Git revision version
--------------------
To generate versions based on revision of every file in your source file list, put this in your `settings.py`::
COMPRESS_VERSIONING = 'compress.versioning.git.GitVersioningBase'
Git HEAD last revision version strings
--------------------------------------
Git HEAD last revision version
------------------------------
To generate versions based on the latest revision of HEAD in your git repository (which assumes all of your source files are in the
same repository), put this in your `settings.py`::
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment