Skip to content

Instantly share code, notes, and snippets.

@jrief
Created November 28, 2015 22:19
Show Gist options
  • Save jrief/7c597aadead7051a2d4f to your computer and use it in GitHub Desktop.
Save jrief/7c597aadead7051a2d4f to your computer and use it in GitHub Desktop.
Replacement for django.contrib.staticfiles.finders.FileSystemFinder
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from django.conf import settings
from django.contrib.staticfiles.finders import FileSystemFinder
class ServeUnminimizedFinder(FileSystemFinder):
"""
In debug mode, serve /static/any/asset.min.ext as /static/any/asset.ext
"""
locations = []
serve_unminimized = getattr(settings, 'DEBUG', False)
def find_location(self, root, path, prefix=None):
if self.serve_unminimized:
# search for the unminimized version, and if it exists, return it
base, ext = os.path.splitext(path)
base, minext = os.path.splitext(base)
if minext == '.min':
unminimized_path = super(ServeUnminimizedFinder, self).find_location(root, base + ext, prefix)
if unminimized_path:
return unminimized_path
# otherwise proceed with the given one
path = super(ServeUnminimizedFinder, self).find_location(root, path, prefix)
return path
@jrief
Copy link
Author

jrief commented Nov 28, 2015

Upset to catch errors in third party Javascript code, which have been included as minimized versions?

By replacing

STATICFILES_FINDERS = (
   'django.contrib.staticfiles.finders.FileSystemFinder'
   ...
)

with

STATICFILES_FINDERS = (
    'finders.ServeUnminimizedFinder',
    ...
)

when in settings.py DEBUG=True and the unminimized files exists side by side with the included one, then serve the former one
instead of the minimized file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment