forked from yograterol/django-webpack-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,6 @@ docs/_build/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
# Editors and IDEs | ||
.ropeproject/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# will hook into collectstatic |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |