diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index b366dc3a..e965ccf8 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -46,6 +46,15 @@ def filter_chunks(self, chunks): chunk['url'] = self.get_chunk_url(chunk) yield chunk + def filter_chunks_auto(self, chunks): + for key, value in chunks.items(): + for chunk in chunks[key]: + ignore = any(regex.match(chunk['name']) + for regex in self.config['ignores']) + if not ignore: + chunk['url'] = self.get_chunk_url(chunk) + yield chunk + def get_chunk_url(self, chunk): public_path = chunk.get('publicPath') if public_path: @@ -56,7 +65,7 @@ def get_chunk_url(self, chunk): ) return staticfiles_storage.url(relpath) - def get_bundle(self, bundle_name): + def get_bundle(self, bundle_name, mode): assets = self.get_assets() # poll when debugging and block request until bundle is compiled @@ -78,9 +87,22 @@ def get_bundle(self, bundle_name): ) if assets.get('status') == 'done': - chunks = assets['chunks'].get(bundle_name, None) + if mode == 'auto': + chunks = {} + search_chunks = assets['chunks'] + for chunk in search_chunks.keys(): + split_chunk = chunk.split('~') + + # the last module name might have a hash appended. assume dash separator and strip it off + last_chunk = split_chunk.pop() + if bundle_name in split_chunk + [last_chunk.split("-")[0]]: + chunks.update({chunk: search_chunks[chunk]}) + else: + chunks = assets['chunks'].get(bundle_name, None) if chunks is None: raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name)) + if mode == 'auto': + return self.filter_chunks_auto(chunks) return self.filter_chunks(chunks) elif assets.get('status') == 'error': diff --git a/webpack_loader/templatetags/webpack_loader.py b/webpack_loader/templatetags/webpack_loader.py index d1e87ce7..54d87b49 100644 --- a/webpack_loader/templatetags/webpack_loader.py +++ b/webpack_loader/templatetags/webpack_loader.py @@ -13,6 +13,12 @@ def render_bundle(bundle_name, extension=None, config='DEFAULT', attrs=''): return mark_safe('\n'.join(tags)) +@register.simple_tag +def render_bundle_auto(bundle_name, extension=None, config='DEFAULT', attrs=''): + tags = utils.get_as_tags(bundle_name, extension=extension, config=config, attrs=attrs, mode='auto') + return mark_safe('\n'.join(tags)) + + @register.simple_tag def webpack_static(asset_name, config='DEFAULT'): return utils.get_static(asset_name, config=config) diff --git a/webpack_loader/utils.py b/webpack_loader/utils.py index e7b7b2f3..6638ab14 100644 --- a/webpack_loader/utils.py +++ b/webpack_loader/utils.py @@ -19,19 +19,19 @@ def _filter_by_extension(bundle, extension): yield chunk -def _get_bundle(bundle_name, extension, config): - bundle = get_loader(config).get_bundle(bundle_name) +def _get_bundle(bundle_name, extension, config, mode): + bundle = get_loader(config).get_bundle(bundle_name, mode) if extension: bundle = _filter_by_extension(bundle, extension) return bundle -def get_files(bundle_name, extension=None, config='DEFAULT'): +def get_files(bundle_name, extension=None, config='DEFAULT', mode="explicit"): '''Returns list of chunks from named bundle''' return list(_get_bundle(bundle_name, extension, config)) -def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs=''): +def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs='', mode='explicit'): ''' Get a list of formatted