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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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':
Expand Down
6 changes: 6 additions & 0 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions webpack_loader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script> & <link> tags for the assets in the
named bundle.
Expand All @@ -42,7 +42,7 @@ def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs=''):
:return: a list of formatted tags as strings
'''

bundle = _get_bundle(bundle_name, extension, config)
bundle = _get_bundle(bundle_name, extension, config, mode)
tags = []
for chunk in bundle:
if chunk['name'].endswith(('.js', '.js.gz')):
Expand Down