Skip to main content

Auto-detects your static files for minification, combination, and versioning. Like Django-Compressor for Flask.

Project description

Tests Coverage

Auto-detects your static files for minification, combination, and versioning. Like Django-Compressor for Flask.

Installation

pip install flask-static-compress

Usage

Just wrap your existing css/js with a compress block and Flask-Static-Compress handles the rest:

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

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

Also, initialize the extension inside your Flask app:

from flask_static_compress import FlaskStaticCompress
app = Flask(__name__)
compress = FlaskStaticCompress(app)

All static assets inside a compress block are compressed into a single file, and your html is updated to the new path when rendering the template.

For example:

{% compress 'js' %}
  <script type="text/javascript" src="{{ STATIC_URL }}js/config.js"></script>
  <script type="text/javascript" src="{{ STATIC_URL }}js/app.js"></script>
{% endcompress %}

Is turned into:

<script type="text/javascript" src="/static/sdist/a041936b125a3ec4ce9bf7a83130157d.js"></script>

The compressed a041936b125a3ec4ce9bf7a83130157d.js contains both app.js and config.js combined for faster page loading. The file name is calculated based on the contents of app.js and config.js. This means any change to your static code is automatically reloaded, or cache-busted, in browsers.

With debug mode turned on, file names and line numbers are preserved while still running the compression flow:

<script type="text/javascript" src="/static/sdist/93a97ef5491b84db5155916be8f8fd7f-config.js"></script>
<script type="text/javascript" src="/static/sdist/af77fa42b92bb5a1ef85d9eb773d608e-app.js"></script>

The type attribute is used to decide which compressor to use for the asset.

Use offline compression for improved performance.

Create custom compressors to support more types of static files.

For example, to remove trailing commas with Prettier then compress with jsmin:

import errno
import subprocess
from jac.compat import file, u, utf8_encode
from jac.exceptions import InvalidCompressorError
from rjsmin import jsmin


class CustomJavaScriptCompressor(object):
    binary = 'prettier'

    @classmethod
    def compile(cls, content, mimetype='text/less', cwd=None, uri_cwd=None,
                debug=None):
        if debug:
            return content

        args = ['--no-config', '--trailing-comma', 'none']

        args.insert(0, cls.binary)

        try:
            handler = subprocess.Popen(args,
                                       stdout=subprocess.PIPE,
                                       stdin=subprocess.PIPE,
                                       stderr=subprocess.PIPE, cwd=None)
        except OSError as e:
            msg = '{0} encountered an error when executing {1}: {2}'.format(
                cls.__name__,
                cls.binary,
                u(e),
            )
            if e.errno == errno.ENOENT:
                msg += ' Make sure {0} is in your PATH.'.format(cls.binary)
            raise InvalidCompressorError(msg)

        if isinstance(content, file):
            content = content.read()
        (stdout, stderr) = handler.communicate(input=utf8_encode(content))
        stdout = u(stdout)

        if handler.returncode == 0:
            return jsmin(stdout)
        else:
            raise RuntimeError('Error compressing: %s' % stderr)


COMPRESSOR_CLASSES = {
    'text/javascript': CustomJavaScriptCompressor,
}

Configuration

COMPRESSOR_ENABLED Default: True

COMPRESSOR_OFFLINE_COMPRESS Default: False

COMPRESSOR_FOLLOW_SYMLINKS Default: False

COMPRESSOR_DEBUG Default: False

COMPRESSOR_OUTPUT_DIR Default: app.static_folder + ‘/sdist’

COMPRESSOR_CACHE_DIR Default: app.static_folder + ‘/sdist’

COMPRESSOR_STATIC_PREFIX Default: app.static_url_path + ‘/sdist’

COMPRESSOR_CLASSES Default:

[
    'text/css': LessCompressor,
    'text/coffeescript': CoffeeScriptCompressor,
    'text/less': LessCompressor,
    'text/javascript': JavaScriptCompressor,
    'text/sass': SassCompressor,
    'text/scss': SassCompressor,
]

Thanks to Jay Santos, creator of jac. Flask-Static-Compress is just a wrapper around jac!

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

Flask-Static-Compress-1.0.3.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

Flask_Static_Compress-1.0.3-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Static-Compress-1.0.3.tar.gz.

File metadata

  • Download URL: Flask-Static-Compress-1.0.3.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for Flask-Static-Compress-1.0.3.tar.gz
Algorithm Hash digest
SHA256 0cc3e97c6789626d8f468b35c16ab37267527309b3b3d3a46e911d0c28e003c7
MD5 8d84ab6d293e368612f43a1e10ff06a4
BLAKE2b-256 0e577a53c39b42ab56932f01e2ee7c2312d39117ef534b56cae74a03f8ada02f

See more details on using hashes here.

File details

Details for the file Flask_Static_Compress-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: Flask_Static_Compress-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for Flask_Static_Compress-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f5a3567ac262ce85fa50438a3f2ffd1509cebe9a0adc8393570763fde8c97a1c
MD5 382189a320af7f28fa1a0df2bb9afe6a
BLAKE2b-256 311cf4a855d2f9a013e488ba01e348612ba44c0c24195f3302c8ce464f19bad2

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