diff --git a/webpack_boilerplate/config.py b/webpack_boilerplate/config.py index 3f2c343..447250e 100644 --- a/webpack_boilerplate/config.py +++ b/webpack_boilerplate/config.py @@ -15,6 +15,7 @@ def load_from_django(): user_config = dict(DEFAULT_CONFIG, **getattr(settings, "WEBPACK_LOADER", {})) user_config["ignores"] = [re.compile(I) for I in user_config["IGNORE"]] + user_config["web_framework"] = "django" return user_config @@ -30,6 +31,7 @@ def load_from_flask(): user_config = dict(DEFAULT_CONFIG, **current_app.config["WEBPACK_LOADER"]) user_config["ignores"] = [re.compile(I) for I in user_config["IGNORE"]] + user_config["web_framework"] = "flask" return user_config diff --git a/webpack_boilerplate/loader.py b/webpack_boilerplate/loader.py index 938e38a..9558aee 100644 --- a/webpack_boilerplate/loader.py +++ b/webpack_boilerplate/loader.py @@ -46,7 +46,23 @@ def filter_chunks(self, chunks): yield chunk def get_chunk_url(self, chunk): - return chunk["url"] + url = chunk["url"] + + if self.config.get("web_framework", None) == "django": + from django.contrib.staticfiles.storage import staticfiles_storage + from django.conf import settings + + if url.startswith("http"): + # webpack dev server + return url + else: + prefix = settings.STATIC_URL + url_without_static_prefix = url[ + url.startswith(prefix) and len(prefix) : + ] + return staticfiles_storage.url(url_without_static_prefix) + else: + return url def get_bundle(self, bundle_name): assets = copy.copy(self.get_assets())