Skip to content

Best practice for widget media files? #95

@michaeljones

Description

@michaeljones

Hi,

Thank you so much for creating this set up. I've always found doing asset hashing with Django quite daunting but your work makes it super easy.

That said, I have custom widgets in my code base for which I have webpack built javascript assets. Previously I've used the media option on the widget to provide the direct path to the asset but now that path needs to be dynamic. I've ended up writing the code as:

from webpack_loader.utils import get_loader

class VenueByNameWidget(VenueWidget):

    url_name = 'venues:name-suggestions'
    target_id = 'venue-name-suggestions'
    js_app_name = 'venue-name-suggest-app'

    @property
    def media(self):
        loader = get_loader('DEFAULT')
        bundle = loader.get_bundle(self.js_app_name)
        paths = tuple(map(lambda entry: webpack_loader.get_chunk_url(entry), bundle))
        return widgets.Media(
            js=paths
        )

But I felt that maybe there should be a webpack_loader.utils function to help with this?

I am no Django expert so I might be approaching the whole thing incorrectly but I wanted to share my experience. I'm happy to attempt a pull request if this would be useful.

Kind regards and thanks again,
Michael

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions