Merge pull request #1544 from umap-project/compress-static
feat: compress static in collectstatic post_process
This commit is contained in:
commit
969b02e7a0
7 changed files with 76 additions and 71 deletions
|
@ -24,21 +24,6 @@ The hosts that uMap expects.
|
|||
|
||||
Can be set through env var too: `ALLOWED_HOSTS=umap.mydomain.org,u.mydomain.org`
|
||||
|
||||
#### COMPRESS_ENABLED
|
||||
#### COMPRESS_STORAGE
|
||||
|
||||
To activate the compression of the static files, you can set this flag to `True`.
|
||||
|
||||
You can then run the following command to compress the assets:
|
||||
|
||||
```bash
|
||||
umap compress
|
||||
```
|
||||
|
||||
Optionally add `COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"`
|
||||
and add `gzip_static on` directive to Nginx `/static` location, so Nginx will
|
||||
serve pregenerated files instead of compressing them on the fly.
|
||||
|
||||
#### DEBUG
|
||||
|
||||
Set it to `True` for easier debugging in case of error.
|
||||
|
|
|
@ -143,9 +143,8 @@ Here are the commands you'll need to run to create the tables, collect the stati
|
|||
# Create the database tables
|
||||
umap migrate
|
||||
|
||||
# Collect and compress static files
|
||||
# Collect static files
|
||||
umap collectstatic
|
||||
umap compress
|
||||
|
||||
# Create a super user
|
||||
umap createsuperuser
|
||||
|
@ -167,5 +166,4 @@ Usually, for upgrading, you need those steps:
|
|||
pip install umap-project --upgrade
|
||||
umap migrate
|
||||
umap collectstatic
|
||||
umap compress
|
||||
```
|
||||
|
|
|
@ -37,6 +37,8 @@ dependencies = [
|
|||
"Pillow==10.0.1",
|
||||
"psycopg2==2.9.6",
|
||||
"requests==2.31.0",
|
||||
"rcssmin==1.1.2",
|
||||
"rjsmin==1.2.2",
|
||||
"social-auth-core==4.4.2",
|
||||
"social-auth-app-django==5.2.0",
|
||||
]
|
||||
|
|
|
@ -169,7 +169,7 @@ STORAGES = {
|
|||
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
||||
},
|
||||
"staticfiles": {
|
||||
"BACKEND": "umap.utils.UmapManifestStaticFilesStorage",
|
||||
"BACKEND": "umap.storage.UmapManifestStaticFilesStorage",
|
||||
},
|
||||
}
|
||||
|
||||
|
|
71
umap/storage.py
Normal file
71
umap/storage.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
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()
|
||||
minified = jsmin(initial)
|
||||
path.write_text(minified)
|
||||
if processed_path.endswith(".css"):
|
||||
path = Path(settings.STATIC_ROOT) / processed_path
|
||||
initial = path.read_text()
|
||||
minified = cssmin(initial)
|
||||
path.write_text(minified)
|
||||
yield original_path, processed_path, True
|
|
@ -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"
|
||||
|
|
|
@ -2,7 +2,6 @@ import gzip
|
|||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
|
||||
from django.urls import URLPattern, URLResolver, get_resolver
|
||||
|
||||
|
||||
|
@ -163,53 +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
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue