chore: move UmapManifestStaticFilesStorage to a dedicated file

This commit is contained in:
Yohan Boniface 2024-01-19 13:47:46 +01:00
parent df3ed76f3e
commit 23af4c60cd
4 changed files with 79 additions and 77 deletions

View file

@ -169,7 +169,7 @@ STORAGES = {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "umap.utils.UmapManifestStaticFilesStorage",
"BACKEND": "umap.storage.UmapManifestStaticFilesStorage",
},
}

77
umap/storage.py Normal file
View file

@ -0,0 +1,77 @@
from pathlib import Path
from django.conf import settings
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from rcssmin import cssmin
from rjsmin import jsmin
class UmapManifestStaticFilesStorage(ManifestStaticFilesStorage):
support_js_module_import_aggregation = True
# We remove `;` at the end of all regexps to match our prettier config.
_js_module_import_aggregation_patterns = (
"*.js",
(
(
(
r"""(?P<matched>import(?s:(?P<import>[\s\{].*?))"""
r"""\s*from\s*['"](?P<url>[\.\/].*?)["']\s*)"""
),
'import%(import)s from "%(url)s"\n',
),
(
(
r"""(?P<matched>export(?s:(?P<exports>[\s\{].*?))"""
r"""\s*from\s*["'](?P<url>[\.\/].*?)["']\s*)"""
),
'export%(exports)s from "%(url)s"\n',
),
(
r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*)""",
'import"%(url)s"\n',
),
(
r"""(?P<matched>import\(["'](?P<url>.*?)["']\))""",
"""import("%(url)s")""",
),
),
)
# https://github.com/django/django/blob/0fcee1676c7f14bb08e2cc662898dee56d9cf207↩
# /django/contrib/staticfiles/storage.py#L78C5-L105C6
patterns = (
(
"*.css",
(
r"""(?P<matched>url\(['"]{0,1}\s*(?P<url>.*?)["']{0,1}\))""",
(
r"""(?P<matched>@import\s*["']\s*(?P<url>.*?)["'])""",
"""@import url("%(url)s")""",
),
# Remove CSS source map rewriting
),
),
# Remove JS source map rewriting
)
def post_process(self, paths, **options):
collected = super().post_process(paths, **options)
for original_path, processed_path, processed in collected:
if processed_path.endswith(".js"):
path = Path(settings.STATIC_ROOT) / processed_path
initial = path.read_text()
path_map = path.with_suffix(f"{path.suffix}.map")
minified = jsmin(initial)
minified += f"\n//# sourceMappingURL={path_map.name}"
path.write_text(minified)
path_map.write_text(initial)
if processed_path.endswith(".css"):
path = Path(settings.STATIC_ROOT) / processed_path
initial = path.read_text()
path_map = path.with_suffix(f"{path.suffix}.map")
minified = cssmin(initial)
minified += f"\n//# sourceMappingURL={path_map.name}"
path.write_text(minified)
path_map.write_text(initial)
yield original_path, processed_path, True

View file

@ -24,7 +24,7 @@ def test_javascript_have_been_loaded(
):
settings.STORAGES["staticfiles"][
"BACKEND"
] = "umap.utils.UmapManifestStaticFilesStorage"
] = "umap.storage.UmapManifestStaticFilesStorage"
datalayer.settings["displayOnLoad"] = False
datalayer.save()
map.settings["properties"]["defaultView"] = "latest"

View file

@ -1,12 +1,8 @@
import gzip
import os
from pathlib import Path
from django.conf import settings
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
from django.urls import URLPattern, URLResolver, get_resolver
from rcssmin import cssmin
from rjsmin import jsmin
def _urls_for_js(urls=None):
@ -166,74 +162,3 @@ def merge_features(reference: list, latest: list, incoming: list):
merged.append(item)
return merged
class UmapManifestStaticFilesStorage(ManifestStaticFilesStorage):
support_js_module_import_aggregation = True
# We remove `;` at the end of all regexps to match our prettier config.
_js_module_import_aggregation_patterns = (
"*.js",
(
(
(
r"""(?P<matched>import(?s:(?P<import>[\s\{].*?))"""
r"""\s*from\s*['"](?P<url>[\.\/].*?)["']\s*)"""
),
'import%(import)s from "%(url)s"\n',
),
(
(
r"""(?P<matched>export(?s:(?P<exports>[\s\{].*?))"""
r"""\s*from\s*["'](?P<url>[\.\/].*?)["']\s*)"""
),
'export%(exports)s from "%(url)s"\n',
),
(
r"""(?P<matched>import\s*['"](?P<url>[\.\/].*?)["']\s*)""",
'import"%(url)s"\n',
),
(
r"""(?P<matched>import\(["'](?P<url>.*?)["']\))""",
"""import("%(url)s")""",
),
),
)
# https://github.com/django/django/blob/0fcee1676c7f14bb08e2cc662898dee56d9cf207↩
# /django/contrib/staticfiles/storage.py#L78C5-L105C6
patterns = (
(
"*.css",
(
r"""(?P<matched>url\(['"]{0,1}\s*(?P<url>.*?)["']{0,1}\))""",
(
r"""(?P<matched>@import\s*["']\s*(?P<url>.*?)["'])""",
"""@import url("%(url)s")""",
),
# Remove CSS source map rewriting
),
),
# Remove JS source map rewriting
)
def post_process(self, paths, **options):
collected = super().post_process(paths, **options)
for original_path, processed_path, processed in collected:
if processed_path.endswith(".js"):
path = Path(settings.STATIC_ROOT) / processed_path
initial = path.read_text()
path_map = path.with_suffix(f"{path.suffix}.map")
minified = jsmin(initial)
minified += f"\n//# sourceMappingURL={path_map.name}"
path.write_text(minified)
path_map.write_text(initial)
if processed_path.endswith(".css"):
path = Path(settings.STATIC_ROOT) / processed_path
initial = path.read_text()
path_map = path.with_suffix(f"{path.suffix}.map")
minified = cssmin(initial)
minified += f"\n//# sourceMappingURL={path_map.name}"
path.write_text(minified)
path_map.write_text(initial)
yield original_path, processed_path, True