Skip to content

Instantly share code, notes, and snippets.

@statico
Created September 28, 2011 18:24
Show Gist options
  • Save statico/1248771 to your computer and use it in GitHub Desktop.
Save statico/1248771 to your computer and use it in GitHub Desktop.
Bundle generators for use with webassets
from django.conf import settings
from django_assets import Bundle, register
from webassets.filter import get_filter
def js(name, *args, **kwargs):
"""
Convenience helper for creating JavaScript bundles.
Given a `name`, this function registers a bundle named `name +
"_js"` with default filters. The output file will prefixed with
the `ASSETS_OUTPUT_PREFIX`.
"""
assert not name.endswith('.js'), 'first arg should be a bundle name'
assert not name.endswith('_js'), '"_js" is automatically added to the bundle name'
kwargs.setdefault('filters', 'jsmin')
kwargs.setdefault('output', settings.ASSETS_OUTPUT_PREFIX + name + '.js')
return register(name + '_js', Bundle(*args, **kwargs))
def css(name, *args, **kwargs):
"""
Convenience helper for creating CSS bundles.
Given a `name`, this function registers a bundle named `name +
"_css"` with default filters. The output file will prefixed with
the `ASSETS_OUTPUT_PREFIX`.
Files ending in `.sass` will be converted to SASS bundles
automatically, so you don't neeed to use the `sass()` function
defined above.
"""
assert not name.endswith('.css'), 'first arg should be a bundle name'
assert not name.endswith('_css'), '"_css" is automatically added to the bundle name'
kwargs.setdefault('filters', 'cssmin,cssrewrite')
kwargs.setdefault('output', settings.ASSETS_OUTPUT_PREFIX + name + '.css')
# Convert files ending in '.sass' to SASS bundles.
sass_filter = get_filter('sass', debug_info=False)
dest = settings.ASSETS_OUTPUT_PREFIX
files = []
for f in args:
if f.endswith('.sass'):
# We need a unique name for the bundle.
path = f.replace('/', '__')
files.append(Bundle(f, filters=(sass_filter, 'cssrewrite'),
debug=False,
output='%s%s__%s.src.css' % (dest, name, path)))
else:
files.append(f)
return register(name + '_css', Bundle(*files, **kwargs))
# --------------------------------------------------
jquery = ('jquery15', 'third_party/jquery-1.5.js')
js('base', 'globals.js', jquery)
css('base', 'global.sass', 'buttons.sass', 'tooltips.sass', 'legacy.css')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment