在烧瓶资产(网络资产)中编写自定义过滤器

Writing custom filter in flask-assets (webassets)

将 Flask 与 flask-assets(来自 webassets)一起使用 我为 css 压缩器 following the documentation 编写了自己的自定义过滤器。好像不行:

from csscompressor import compress
from flask_assets import Bundle
from webassets.filter import Filter, register_filter

class NoopFilter(Filter):
    name = 'csscompressor'
    print("noopfilter")

    def output(self, _in, out, **kwargs):
        print("output")

    def input(self, _in, out, **kwargs):
        print("input")

register_filter(NoopFilter)

bundles = {
    'home_css': Bundle(
        'css/main.css',
        'css/forms.css',
        output='gen/home.css',
        filters='csscompressor'
    )

class 在启动时被调用了两次,过滤器是通过它的名字找到的,但是 input()output() 似乎从来没有被调用过。 css 文件已创建,但显然没有进行任何压缩。有人对此有经验吗?

显然,这是一个缓存问题。更改过滤器名称使其工作。希望这可以帮助人们将他们的头发拉出几 kB。

使用 csscompressor 包压缩 CSS 的示例(pip install csscompressor)

from csscompressor import compress
from flask_assets import Bundle
from webassets.filter import Filter, register_filter

class NoopFilter(Filter):
    name = 'css_compress'

    def output(self, _in, out, **kwargs):
        css = compress(_in.read())
        out.write(css)

    def input(self, _in, out, **kwargs):
        css = compress(_in.read())
        out.write(css)


register_filter(NoopFilter)


bundles = {
    'app_css': Bundle(
        'css/responsive.css',
        'css/ads.css',
         filters='css_compress',
         output='all/app.css'
    ),