跳转到主要内容

自动检测您的静态文件以进行压缩、合并和版本管理。类似于Django-Compressor for Flask。

项目描述

Tests Coverage

自动检测您的静态文件以进行压缩、合并和版本管理。类似于Django-Compressor for Flask。

安装

pip install flask-static-compress

使用

只需将现有的css/js用压缩块包裹,Flask-Static-Compress就会处理其余部分

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

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

此外,请在您的Flask应用中初始化扩展

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

压缩块内的所有静态资产都压缩成单个文件,并在渲染模板时更新您的html到新路径。

例如

{% 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 %}

变成

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

压缩的 a041936b125a3ec4ce9bf7a83130157d.js 包含了 app.jsconfig.js 的合并,以加快页面加载速度。文件名是根据 app.jsconfig.js 的内容计算的。这意味着您静态代码的任何更改都会自动重新加载或在浏览器中缓存失效。

开启调试模式时,文件名和行号会被保留,同时仍然运行压缩流程

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

使用 type 属性来决定用于资产的压缩器。

使用 离线压缩 以提高性能。

创建 自定义压缩器 以支持更多类型的静态文件。

例如,要使用 Prettier 移除尾部逗号,然后使用 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,
}

配置

COMPRESSOR_ENABLED 默认:True

COMPRESSOR_OFFLINE_COMPRESS 默认:False

COMPRESSOR_FOLLOW_SYMLINKS 默认:False

COMPRESSOR_DEBUG 默认:False

COMPRESSOR_OUTPUT_DIR 默认:app.static_folder + ‘/sdist’

COMPRESSOR_CACHE_DIR 默认:app.static_folder + ‘/sdist’

COMPRESSOR_STATIC_PREFIX 默认:app.static_url_path + ‘/sdist’

COMPRESSOR_CLASSES 默认

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

感谢 Jay Santos,jac 的创造者。Flask-Static-Compress 只是 jac 的包装器!

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分布

Flask-Static-Compress-1.0.3.tar.gz (5.2 kB 查看哈希值)

上传时间

构建分布

Flask_Static_Compress-1.0.3-py3-none-any.whl (5.4 kB 查看哈希值)

上传时间 Python 3

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面