Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ If the bundle generates a file called `main-cf4b5fab6e00a404e0c7.js` and your ST
<br>

#### STATS_FILE

`STATS_FILE` is the URL or filesystem path to the file generated by `webpack-bundle-tracker` plugin. If you initialize `webpack-bundle-tracker` plugin like this

```javascript
new BundleTracker({filename: './webpack-stats.json'})
```

If your webpack config is located at `/home/src/webpack.config.js`, then the value of `STATS_FILE` should be `/home/src/webpack-stats.json`.

```python
WEBPACK_LOADER = {
'DEFAULT': {
Expand All @@ -137,13 +146,23 @@ WEBPACK_LOADER = {
}
```

`STATS_FILE` is the filesystem path to the file generated by `webpack-bundle-tracker` plugin. If you initialize `webpack-bundle-tracker` plugin like this
- or -

```javascript
new BundleTracker({filename: './webpack-stats.json'})
```
If your webpack config is located at `/home/src/webpack.config.js`, and you have an http server serving the stats file as json at `127.0.0.1:3000/stats`, then the value of `STATS_FILE` should be `http://127.0.0.1:3000/stats`

`STATS_FILE_TIMEOUT` should be the maximum amount of time python should wait ebfore cancelling trying to read the stats file over TCP.

`STATS_FILE_SECRET_KEY` should be the value of the `SECRET_KEY` request header that is sent with the stats file read request. This is used by the server to validate the request.

and your webpack config is located at `/home/src/webpack.config.js`, then the value of `STATS_FILE` should be `/home/src/webpack-stats.json`
```python
WEBPACK_LOADER = {
'DEFAULT': {
'STATS_FILE': "http://127.0.0.1:3000/stats",
'STATS_FILE_TIMEOUT': 10.0,
'STATS_FILE_SECRET_KEY': 'A_VERY_SECRET_KEY'
}
}
```

<br>

Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def rel(*parts):
'''returns the relative path to a file wrt to the current directory'''
return os.path.abspath(os.path.join(os.path.dirname(__file__), *parts))

README = open('README.md', 'r').read()
with open("README.md", "r", encoding="utf-8") as f:
README = f.read()

with open(rel('webpack_loader', '__init__.py')) as handler:
INIT_PY = handler.read()
Expand All @@ -27,6 +28,9 @@ def rel(*parts):
author_email = 'hello@owaislone.org',
download_url = 'https://github.com/owais/django-webpack-loader/tarball/{0}'.format(VERSION),
url = 'https://github.com/owais/django-webpack-loader', # use the URL to the github repo
install_requires = [
"requests",
],
keywords = ['django', 'webpack', 'assets'], # arbitrary keywords
classifiers = [
'Programming Language :: Python :: 2.6',
Expand Down
1 change: 0 additions & 1 deletion tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
)
from webpack_loader.utils import get_loader


BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/')
DEFAULT_CONFIG = 'DEFAULT'

Expand Down
2 changes: 1 addition & 1 deletion webpack_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__author__ = 'Owais Lone'
__version__ = '0.6.0'
__version__ = '0.6.1'

default_app_config = 'webpack_loader.apps.WebpackLoaderConfig'
2 changes: 2 additions & 0 deletions webpack_loader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
'CACHE': not settings.DEBUG,
'BUNDLE_DIR_NAME': 'webpack_bundles/',
'STATS_FILE': 'webpack-stats.json',
'STATS_FILE_TIMEOUT': 3.0,
'STATS_FILE_SECRET_KEY': 'THIS_IS_A_REALLY_SECRET_KEY',
# FIXME: Explore usage of fsnotify
'POLL_INTERVAL': 0.1,
'TIMEOUT': None,
Expand Down
33 changes: 25 additions & 8 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import time
from io import open
import requests

from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
Expand All @@ -22,14 +23,30 @@ def __init__(self, name='DEFAULT'):
self.config = load_config(self.name)

def _load_assets(self):
try:
with open(self.config['STATS_FILE'], encoding="utf-8") as f:
return json.load(f)
except IOError:
raise IOError(
'Error reading {0}. Are you sure webpack has generated '
'the file and the path is correct?'.format(
self.config['STATS_FILE']))
stats_file = self.config["STATS_FILE"]
if stats_file.startswith(("http://", "https://")):
timeout = float(self.config["STATS_FILE_TIMEOUT"])
try:
response = requests.get(stats_file, timeout=timeout, headers={
"SECRET_KEY": self.config["STATS_FILE_SECRET_KEY"],
})
status_code = response.status_code
if status_code != 200:
raise requests.HTTPError("{} - {}: {}".format(
stats_file, status_code, response.reason))
return response.json()
except requests.Timeout:
raise requests.Timeout(
"Failed to connect in {} seconds. Are you sure that a "
"server is running at {}?".format(timeout, stats_file))
else:
try:
with open(stats_file, encoding="utf-8") as f:
return json.load(f)
except IOError:
raise IOError(
'Error reading {0}. Are you sure webpack has generated '
'the file and the path is correct?'.format(stats_file))

def get_assets(self):
if self.config['CACHE']:
Expand Down