Skip to content

Support callable in DjangoViteConfig #144

@scobenes

Description

@scobenes

So in our sites we have a setting that is called LOCAL_ASSET_DEV and we use that so developers that don't want to have the vite server running all the time can just set that to false and use the latest prod assets. In the current vite config you have to set dev_mode to a boolean. because of the way we do imports, the local dev overrides don't happen until after all the other settings are imported.

In the previous non vite setup we just checked the settings when rendering so it wasn't a big deal, but now that this runs on the django app ready check, it makes this not work as well. To get around this I did a simple monkeypatch that I hope you can also implement into the main codebase.

def monkeypatched_apply_django_vite_settings(cls):
    """
    Takes DjangoViteConfigs from the DJANGO_VITE setting, and plugs them into
    DjangoViteAppClients.
    """

    django_vite_settings = getattr(settings, cls.DJANGO_VITE, None)

    if not django_vite_settings:
        return

    for app_name, config in django_vite_settings.items():
        if not isinstance(config, DjangoViteConfig):
            # THIS IS WHERE THE SHENANIGANS BEGIN
            for key, value in config.items():
                if callable(value):
                    config[key] = value()
            # END SHENANIGANS
            config = DjangoViteConfig(**config)
        cls._instance._apps[app_name] = DjangoViteAppClient(config, app_name)
        
DjangoViteAssetLoader._apply_django_vite_settings = classmethod(monkeypatched_apply_django_vite_settings)

and then it allows me to basically do this:

DJANGO_VITE = {
    "default": {
        "dev_mode": lambda: settings.LOCAL_ASSET_DEV,
    }
}

This way all the settings finish loading and then the django ready call happens with everything being overridden to the dev's desires. We'd prefer if they didn't have to redefine the entire DJANGO_VITE config in their local overrides

Appreciate the consideration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions