""" Extra webasset filters. """
import os
import rcssmin # type:ignore[import-untyped]
from webassets.filter import Filter, register_filter # type:ignore
from webassets.filter.datauri import ( # type:ignore[import-untyped]
CSSDataUri, CSSUrlRewriter)
from dukpy.webassets import BabelJSX # type:ignore[import-untyped]
from dukpy import jsx_compile # type:ignore[import-untyped]
from typing import Any, IO
[docs]
class JsxFilter(BabelJSX):
"""
DukPy is a simple javascript interpreter for Python built on top of
duktape engine without any external dependency.
Note: let is not supported by the duktape engine,
see https://github.com/amol-/dukpy/issues/47.
"""
[docs]
babel_options: dict[str, Any] = {'minified': True}
[docs]
def setup(self) -> None:
self.transformer = jsx_compile
register_filter(JsxFilter)
[docs]
class DataUriFilter(CSSDataUri):
""" Overrides the default datauri filter to work around this issue:
https://github.com/miracle2k/webassets/issues/387
"""
@property
[docs]
def source_url(self) -> str:
return self.ctx.resolver.resolve_source_to_url(
self.ctx, self.keywords['source_path'], self.keywords['source'])
@property
[docs]
def output_url(self) -> str:
return self.ctx.resolver.resolve_output_to_url(
self.ctx, self.keywords['output'])
register_filter(DataUriFilter)
[docs]
class RCSSMinFilter(Filter):
""" Adds the rcssmin filter (not yet included in webassets) """
[docs]
name = 'custom-rcssmin'
[docs]
def setup(self) -> None:
self.rcssmin = rcssmin
[docs]
def output(self, _in: IO[str], out: IO[str], **kw: Any) -> None:
out.write(self.rcssmin.cssmin(_in.read()))
register_filter(RCSSMinFilter)