miracle2k / webassets

Asset management for Python web development.
BSD 2-Clause "Simplified" License
921 stars 258 forks source link

How to configure filters using the YAML config option? Specifically cssrewrite. #540

Open gg2 opened 2 years ago

gg2 commented 2 years ago

I'm using webassets, as part of a platform on Flask (CKAN) (so as built-in of Flask? I don't know).

I have a plugin that uses a js library, that uses some css themes, that are initially located relatively to each other: js.js js-themes/ default/ icons.png style.css

style.css references url(icons.png).

Using webassets to bundle style.css, just adding cssrewrite as a filter without config fails with a ValueError in query_url_mapping: "'Cannot determine url for %s' % filepath".

I see in the docs, get_filter('cssrewrite', replace={'old_directory':'/custom/path/'}) replace looks like what I need.

I see there is the documentation for adding a config section to a YAML (if I interpret correctly):

config:
    another_custom_config_value: foo
bundles:
    # ...

So, what should cssrewrite config look like via YAML? Variations on the following stabs-in-the-dark don't work or cause crashes parsing the YAML:

config:
  cssrewrite:
    - replace={'': 'assets/js-themes/default/'}
gg2 commented 2 years ago

So, I assume, now that I'm looking at it again a 2nd time, cssrewrite simply accepts no config from the environment. The "manual mode" is the expectation for use.

If so, then I'd like to lobby for enabling "replace" as a config for cssrewrite, especially via YAML (presumably an option can be set via YAML by default, if environment.config can be set). For the platform I'm working with, I'm not sure they provide a means to hook into webassets before/after it runs, aside from simply setting up the YAML config. And I always dislike setting up custom hacks to make things work outside the regular flow.

Anyway, thank you for the consideration.

Also, now that I've had the presence of mind to find the environment config docs, I suppose the YAML might look more like this?

config:
    cssrewrite_replace:
        - ''
          'assets/js-themes/default/'
        - 'etc'
          ...

If I can't specify to simply add a prefix where there is none, and must use the regex replace, that would be helpful to elaborate in the docs. Thank you, again.