Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
owais committed May 16, 2015
1 parent 8e3c78e commit e4f79e6
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ docs/_build/

# PyBuilder
target/

# Editors and IDEs
.ropeproject/
1 change: 1 addition & 0 deletions LICENSE.txt
7 changes: 7 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# file GENERATED by distutils, do NOT edit
setup.cfg
setup.py
webpack_loader/__init__.py
webpack_loader/utils.py
webpack_loader/templatetags/__init__.py
webpack_loader/templatetags/webpack_loader.py
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from distutils.core import setup
setup(
name = 'django-webpack-loader',
packages = ['webpack_loader', 'webpack_loader/templatetags'], # this must be the same as the name above
version = '0.0.1',
description = 'Load your webpack bundles and chunks in django',
author = 'Owais Lone',
author_email = '[email protected]',
url = 'https://github.com/owais/django-webpack-loader', # use the URL to the github repo
keywords = ['django', 'webpack', 'assets'], # arbitrary keywords
classifiers = [],
)
3 changes: 3 additions & 0 deletions webpack_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default_app_config = 'webpack_loader.apps.WebpackLoaderConfig'

from .utils import get_bundle
9 changes: 9 additions & 0 deletions webpack_loader/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.apps import AppConfig


class WebpackLoaderConfig(AppConfig):
name = 'webpack_loader'
verbose_name = "Webpack Loader"

def ready(self):
from .signals import *
1 change: 1 addition & 0 deletions webpack_loader/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# will hook into collectstatic
Empty file.
19 changes: 19 additions & 0 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import datetime
from django import template

from ..utils import get_bundle


register = template.Library()


@register.simple_tag
def render_bundle(bundle_name):
bundle = get_bundle(bundle_name)
tags = []
for chunk in bundle:
if chunk['name'].endswith('.js'):
tags.append('<script type="text/javascript" src="{}"/>'.format(chunk['publicPath']))
elif chunk['name'].endwith('.css'):
tags.append('<link type="text/css" href="{}" rel="stylesheet"/>'.format(chunk['publicPath']))
return '\n'.join(tags)
44 changes: 44 additions & 0 deletions webpack_loader/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json
import time

from django.conf import settings

static_path = getattr(settings, 'WEBPACK_BUNDLE_PATH', 'webpack_bundles/')
stats_file = getattr(settings, 'WEBPACK_STATS_FILE', 'webpack-stats.json')


__all__ = ('get_bundle',)


class WebpackException(BaseException):
pass


def get_assets():
try:
return json.loads(
open(stats_file).read())
except ValueError:
return {'status': 'compiling'}


def get_bundle(bundle_name):
assets = get_assets()

while assets['status'] == 'compiling':
time.sleep(0.5)
assets = get_assets()

if assets.get('status') == 'done':
bundle = assets['chunks'][bundle_name]
for F in bundle:
F['url'] = '{}{}'.format(static_path, F['name'])
print bundle
return bundle

elif assets.get('status') == 'error':
error = """
{error} in {file}
{message}
""".format(**assets)
raise WebpackException(error)

0 comments on commit e4f79e6

Please sign in to comment.