from whitenoise.storage import CompressedManifestStaticFilesStorage


class LooseManifestStaticFilesStorage(CompressedManifestStaticFilesStorage):
    """WhiteNoise manifest storage that tolerates broken asset refs.

    Vendor JS bundles ship with `//# sourceMappingURL=...map` comments pointing
    at .map files we don't vendor. Two guards needed:

    - ``manifest_strict = False`` makes runtime ``{% static %}`` lookups for
      names missing from the manifest fall back to the original URL.
    - ``hashed_name()`` is overridden so the *post-processor* (which calls
      ``hashed_name`` to rewrite intra-file URL refs) also falls back instead
      of raising ``ValueError`` and aborting the whole collectstatic run on
      the first missing source map. ``ValueError`` is what Django's
      ``HashedFilesMixin.hashed_name`` raises when the referenced file isn't
      on disk; WhiteNoise only wraps it as ``MissingFileError`` later for the
      final error message, by which point post_process has already aborted.
    """

    manifest_strict = False

    def hashed_name(self, name, content=None, filename=None):
        try:
            return super().hashed_name(name, content, filename)
        except ValueError:
            return name
