2024-01-19 06:47:46 -06:00
|
|
|
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")""",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def post_process(self, paths, **options):
|
|
|
|
collected = super().post_process(paths, **options)
|
|
|
|
for original_path, processed_path, processed in collected:
|
2024-02-19 06:47:57 -06:00
|
|
|
if isinstance(processed, Exception):
|
|
|
|
print("Error with file", original_path)
|
|
|
|
raise processed
|
2024-01-19 06:47:46 -06:00
|
|
|
if processed_path.endswith(".js"):
|
|
|
|
path = Path(settings.STATIC_ROOT) / processed_path
|
|
|
|
initial = path.read_text()
|
2024-01-21 13:02:16 -06:00
|
|
|
if "sourceMappingURL" not in initial: # Already minified.
|
|
|
|
minified = jsmin(initial)
|
|
|
|
path.write_text(minified)
|
2024-01-19 06:47:46 -06:00
|
|
|
if processed_path.endswith(".css"):
|
|
|
|
path = Path(settings.STATIC_ROOT) / processed_path
|
|
|
|
initial = path.read_text()
|
2024-01-21 13:02:16 -06:00
|
|
|
if "sourceMappingURL" not in initial: # Already minified.
|
|
|
|
minified = cssmin(initial)
|
|
|
|
path.write_text(minified)
|
2024-01-19 06:47:46 -06:00
|
|
|
yield original_path, processed_path, True
|