Skip to content

Commit cd7fed4

Browse files
committed
Merge branch 'master' of github.com:owais/django-webpack-loader
2 parents cb11fb8 + 7bea208 commit cd7fed4

File tree

7 files changed

+81
-9
lines changed

7 files changed

+81
-9
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module.exports = {
6363
context: __dirname,
6464
entry: './assets/js/index',
6565
output: {
66-
path: path.resolve('./assets/bundles/'),
66+
path: path.resolve('./assets/webpack_bundles/'),
6767
filename: "[name]-[hash].js"
6868
},
6969

@@ -80,7 +80,7 @@ module.exports = {
8080
WEBPACK_LOADER = {
8181
'DEFAULT': {
8282
'CACHE': not DEBUG,
83-
'BUNDLE_DIR_NAME': 'bundles/', # must end with slash
83+
'BUNDLE_DIR_NAME': 'webpack_bundles/', # must end with slash
8484
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
8585
'POLL_INTERVAL': 0.1,
8686
'TIMEOUT': None,
@@ -292,7 +292,8 @@ In the below example, `logo.png` can be any static asset shipped with any npm or
292292
293293
`./webpack_production.config.js`
294294
```javascript
295-
config = require('./webpack.config.js');
295+
var config = require('./webpack.config.js');
296+
var BundleTracker = require('webpack-bundle-tracker');
296297
297298
config.output.path = require('path').resolve('./assets/dist');
298299

requirements-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
twine==1.7.4
2+
Django==1.10.1
3+
django-jinja==2.2.1
4+
django-jinja2==0.1
5+
unittest2==1.1.0

tests/app/tests/test_webpack.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from webpack_loader.exceptions import (
1414
WebpackError,
1515
WebpackLoaderBadStatsError,
16-
WebpackLoaderTimeoutError
16+
WebpackLoaderTimeoutError,
17+
WebpackBundleLookupError
1718
)
1819
from webpack_loader.utils import get_loader
1920

@@ -66,6 +67,20 @@ def test_simple_and_css_extract(self):
6667
self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js'))
6768
self.assertEqual(main[1]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/styles.css'))
6869

70+
def test_js_gzip_extract(self):
71+
self.compile_bundles('webpack.config.gzipTest.js')
72+
assets = get_loader(DEFAULT_CONFIG).get_assets()
73+
self.assertEqual(assets['status'], 'done')
74+
self.assertIn('chunks', assets)
75+
76+
chunks = assets['chunks']
77+
self.assertIn('main', chunks)
78+
self.assertEqual(len(chunks), 1)
79+
80+
main = chunks['main']
81+
self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js.gz'))
82+
self.assertEqual(main[1]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/styles.css'))
83+
6984
def test_static_url(self):
7085
self.compile_bundles('webpack.config.publicPath.js')
7186
assets = get_loader(DEFAULT_CONFIG).get_assets()
@@ -153,6 +168,14 @@ def test_reporting_errors(self):
153168
except WebpackError as e:
154169
self.assertIn("Cannot resolve module 'the-library-that-did-not-exist'", str(e))
155170

171+
def test_missing_bundle(self):
172+
missing_bundle_name = 'missing_bundle'
173+
self.compile_bundles('webpack.config.simple.js')
174+
try:
175+
get_loader(DEFAULT_CONFIG).get_bundle(missing_bundle_name)
176+
except WebpackBundleLookupError as e:
177+
self.assertIn('Cannot resolve bundle {0}'.format(missing_bundle_name), str(e))
178+
156179
def test_missing_stats_file(self):
157180
stats_file = settings.WEBPACK_LOADER[DEFAULT_CONFIG]['STATS_FILE']
158181
if os.path.exists(stats_file):

tests/webpack.config.gzipTest.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var path = require("path");
2+
var webpack = require('webpack');
3+
var BundleTracker = require('webpack-bundle-tracker');
4+
var ExtractTextPlugin = require("extract-text-webpack-plugin");
5+
6+
7+
module.exports = {
8+
context: __dirname,
9+
entry: './assets/js/index',
10+
output: {
11+
path: path.resolve('./assets/bundles/'),
12+
filename: "[name].js.gz"
13+
},
14+
15+
plugins: [
16+
new ExtractTextPlugin("styles.css"),
17+
new BundleTracker({filename: './webpack-stats.json'}),
18+
],
19+
20+
module: {
21+
loaders: [
22+
// we pass the output from babel loader to react-hot loader
23+
{ test: /\.jsx?$/, exclude: /node_modules/, loaders: ['babel'], },
24+
{ test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }
25+
],
26+
},
27+
28+
resolve: {
29+
modulesDirectories: ['node_modules', 'bower_components'],
30+
extensions: ['', '.js', '.jsx']
31+
},
32+
}

webpack_loader/exceptions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
__all__ = ('WebpackError', 'WebpackLoaderBadStatsError')
1+
__all__ = (
2+
'WebpackError',
3+
'WebpackLoaderBadStatsError',
4+
'WebpackLoaderTimeoutError',
5+
'WebpackBundleLookupError'
6+
)
27

38

49
class WebpackError(Exception):
@@ -11,3 +16,7 @@ class WebpackLoaderBadStatsError(Exception):
1116

1217
class WebpackLoaderTimeoutError(Exception):
1318
pass
19+
20+
21+
class WebpackBundleLookupError(Exception):
22+
pass

webpack_loader/loader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from .exceptions import (
88
WebpackError,
99
WebpackLoaderBadStatsError,
10-
WebpackLoaderTimeoutError
10+
WebpackLoaderTimeoutError,
11+
WebpackBundleLookupError
1112
)
1213
from .config import load_config
1314

@@ -76,7 +77,9 @@ def get_bundle(self, bundle_name):
7677
)
7778

7879
if assets.get('status') == 'done':
79-
chunks = assets['chunks'][bundle_name]
80+
chunks = assets['chunks'].get(bundle_name, None)
81+
if chunks is None:
82+
raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name))
8083
return self.filter_chunks(chunks)
8184

8285
elif assets.get('status') == 'error':

webpack_loader/templatetags/webpack_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def filter_by_extension(bundle, extension):
1717
def render_as_tags(bundle, attrs):
1818
tags = []
1919
for chunk in bundle:
20-
if chunk['name'].endswith('.js'):
20+
if chunk['name'].endswith(('.js', '.js.gz')):
2121
tags.append((
2222
'<script type="text/javascript" src="{0}" {1}></script>'
2323
).format(chunk['url'], attrs))
24-
elif chunk['name'].endswith('.css'):
24+
elif chunk['name'].endswith(('.css', '.css.gz')):
2525
tags.append((
2626
'<link type="text/css" href="{0}" rel="stylesheet" {1}/>'
2727
).format(chunk['url'], attrs))

0 commit comments

Comments
 (0)