Skip to main content

A Jinja extension (compatible with Flask and other frameworks) to compile and/or compress your assets.

Project description

Build Status

jinja-assets-compressor

A Jinja2 extension to compile and/or compress your assets.

Installing

pip install jac

For LESS and CSS support, install less:

npm install -g less

For COFFEE support, install coffee-script:

npm install -g coffee-script

For Sass and SCSS support, install sass:

gem install sass

JavaScript minification is built-in using the Python rJsmin package.

When installing on Mac OS X set this shell variable, because jac dependencies contain C code:

export CFLAGS=-Qunused-arguments

Usage

To use it, you just have to put your css or js inside a compress tag.

{% compress 'css' %}
<style type="text/sass">
sass stuff
</style>
<link rel="stylesheet" type="text/sass" href="file.sass">
{% endcompress %}

{% compress 'js' %}
<script type="text/coffeescript">
coffee stuff
</script>
<script type="text/coffeescript" src="file.coffee"></script>
{% endcompress %}

Configuring Jinja

You just have to create an environment with jac on it and configure output dir, static prefix and say where it can find your sources.

import jinja2

from jac import CompressorExtension

env = jinja2.Environment(extensions=[CompressorExtension])
env.compressor_output_dir = './static/dist'
env.compressor_static_prefix = '/static'
env.compressor_source_dirs = './static_files'

After that just use template = env.from_string(html); template.render() to get it done.

Configuring Flask

Where you configure your app, just do this:

from jac.contrib.flask import JAC

app = Flask(__name__)
app.config['COMPRESSOR_DEBUG'] = app.config.get('DEBUG')
app.config['COMPRESSOR_OUTPUT_DIR'] = './static/dist'
app.config['COMPRESSOR_STATIC_PREFIX'] = '/static'
jac = JAC(app)

And you are done.

Offline Compression

JAC supports compressing static assets offline, then deploying to a production server. Here is a command to compress your static assets if using Flask:

python -m jac.contrib.flask my_flask_module:create_app

Replace my_flask_module with the correct import path to find your Flask app.

Custom Compressors

The compressor_classes template env variable tells jac which compressor to use for each mimetype. The default value for compressor_classes is:

{
    'text/css': LessCompressor,
    'text/coffeescript': CoffeeScriptCompressor,
    'text/less': LessCompressor,
    'text/javascript': JavaScriptCompressor,
    'text/sass': SassCompressor,
    'text/scss': SassCompressor,
}

To use an alternate compressor class, provide a class with a compile class method accepting arg text and kwargs mimetype, cwd, uri_cwd, and debug. For example, to use libsass-python for SASS files instead of the built-in SassCompressor, create your custom compressor class:

import sass

class CustomSassCompressor(object):
    """Custom compressor for text/sass mimetype.

    Uses libsass-python for compression.
    """

    @classmethod
    def compile(cls, text, cwd=None, **kwargs):

        include_paths = []
        if cwd:
            include_paths += [cwd]

        return sass.compile(string=text, include_paths=include_paths)

Then tell jac to use your custom compressor for text/sass mimetypes:

env.compressor_classes['text/sass'] = CustomSassCompressor

The equivalent for Flask is:

jac.set_compressor('text/sass', CustomSassCompressor)

To only customize the path of a compressor which forks a subprocess for the compile step (LessCompressor, CoffeeScriptCompressor, and SassCompressor), just extend the compressor class and overwrite the binary class attribute:

from jac.compressors import SassCompressor

class CustomSassCompressor(SassCompressor):
    """Custom SASS compressor using Compass binary instead of libsass for text/sass mimetype.

    Uses the faster libsass wrapper sassc for SASS compression.
    https://github.com/sass/sassc
    """

    binary = '/usr/bin/sassc'

# Tell Flask to use our custom SASS compressor
jac.set_compressor('text/sass', CustomSassCompressor)

Running Tests

virtualenv venv
. venv/bin/activate
pip install -r requirements_tests.txt
make coverage
make lint

Or use tox to run with multiple python versions:

pip install tox
tox

CHANGELOG

0.17.2

  • Make sure that flask contrib script always push an app context.

0.17.1

  • Fix error when the blueprint cannot be found (i.e. flask debug toolbar)

0.17.0

  • Fix app factory initialization

  • Change how tests and linters run

  • Add compress command to jac.contrib.flask

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jac-0.18.0.tar.gz (14.2 kB view details)

Uploaded Source

File details

Details for the file jac-0.18.0.tar.gz.

File metadata

  • Download URL: jac-0.18.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2

File hashes

Hashes for jac-0.18.0.tar.gz
Algorithm Hash digest
SHA256 0c13b7142a4d62a9a71f8a0885fb581ce4fadb85e4b52cc7c912c3e9e4712daa
MD5 a29a87b950d617c705138f537ae0bb52
BLAKE2b-256 e9cc965ea0d688e30c43c5f0a3f861a50175857ee2ced32a7711652bbcbb9616

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page