Skip to content

Commit 286d44e

Browse files
committed
Build CSS with postcss
1 parent a8a1f75 commit 286d44e

File tree

9 files changed

+34
-50
lines changed

9 files changed

+34
-50
lines changed

bin/frontend.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# Copyright: 2022, ECP, NLnet Labs and the Internet.nl contributors
22
# SPDX-License-Identifier: Apache-2.0
33
from argparse import ArgumentParser
4-
import io
54
import os
65
from uuid import uuid4 as uuid
76

87
import rjsmin
9-
import sass
108

119

1210
FRONTEND_FOLDER = "frontend"
@@ -15,7 +13,6 @@
1513

1614
DJANGO_STATIC_FOLDER = "static_frontend"
1715
DJANGO_STATIC_JS_FOLDER = DJANGO_STATIC_FOLDER + "/js"
18-
DJANGO_STATIC_CSS_FOLDER = DJANGO_STATIC_FOLDER + "/css"
1916

2017

2118
def build_js(args=None):
@@ -53,36 +50,6 @@ def build_js(args=None):
5350
print("Done!")
5451

5552

56-
def build_css(args=None):
57-
"""
58-
Build/minify CSS and copy into Django's static folder.
59-
60-
"""
61-
print("-"*20)
62-
print("Building CSS files.")
63-
try:
64-
os.mkdir(DJANGO_STATIC_CSS_FOLDER)
65-
except FileExistsError:
66-
pass
67-
for root, dirs, files in os.walk(FRONTEND_CSS_FOLDER):
68-
for filename in files:
69-
if filename.endswith(".css"):
70-
if filename == "style.css-notyet-scss":
71-
continue
72-
print(f"Found {filename}...")
73-
filepath = os.path.join(root, filename)
74-
content_min = sass.compile(
75-
filename=filepath, output_style="compressed")
76-
filename, _ = filename.rsplit(".", 1)
77-
filepath = os.path.join(
78-
DJANGO_STATIC_CSS_FOLDER, filename + "-min.css")
79-
print(f"... minifying to {filepath}")
80-
with open(filepath, 'w', encoding="utf-8") as f:
81-
f.write(content_min)
82-
break
83-
print("Done!")
84-
85-
8653
def parse():
8754
"""
8855
Parse the command line.
@@ -98,9 +65,6 @@ def parse():
9865

9966
parser.set_defaults(func=lambda x: parser.print_help())
10067

101-
to_django = subparsers.add_parser('css')
102-
to_django.set_defaults(func=build_css)
103-
10468
to_tar = subparsers.add_parser('js')
10569
to_tar.set_defaults(func=build_js)
10670

docker/Dockerfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ USER unbound
133133

134134
ENTRYPOINT ["/entrypoint.sh"]
135135

136+
FROM debian:bullseye-20241111-slim AS frontend
137+
138+
# install npm from official image
139+
COPY --from=node:20-slim /usr/local/bin/ /usr/local/bin/
140+
COPY --from=node:20-slim /usr/local/lib/node_modules /usr/local/lib/node_modules
141+
142+
ADD frontend/ /app/frontend
143+
WORKDIR /app/frontend
144+
RUN npm install
145+
RUN npm run css:build
146+
136147
# build main application image target
137148
FROM --platform=linux/amd64 debian:bullseye-20241111-slim AS build-app
138149
ARG PYTHON_VERSION
@@ -204,8 +215,10 @@ RUN install -d -m 0755 -o nobody -g nogroup /app/batch_results
204215
# some js/css files use a separate script to generate minimized versions
205216
# storage these in a place for django to find
206217
RUN mkdir -p /app/static_frontend
218+
# minimize JS files
207219
RUN python3 bin/frontend.py js
208-
RUN python3 bin/frontend.py css
220+
# copy build/minimized CSS files from frontand stage
221+
COPY --from=frontend --chown=nobody:nogroup /app/static_frontend/css/*.css /app/static_frontend/css/
209222

210223
# generate language files
211224
RUN python3 bin/pofiles.py to_django

docker/compose.development.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ services:
2222
develop:
2323
watch:
2424
# auto rebuild/reload when CSS/JS changes
25-
- path: ../frontend/
25+
- path: ../frontend/js
2626
action: sync+exec
27-
target: /app/frontend
27+
target: /app/frontend/js
2828
exec:
29-
command: /bin/sh -c "python3 bin/frontend.py js;python3 bin/frontend.py css;"
29+
command: python3 bin/frontend.py js
3030
# use Django runserver for better debug abilities during development
3131
entrypoint: ["/bin/bash"]
3232
# run CSS auto rebuild in the background and start devserver
3333
command:
3434
- "-c"
3535
- >
3636
if [ "$INTERNETNL_AUTORELOAD" = "True" ]; then
37+
# watch CSS files for rebuild
38+
cd /app/frontend;
39+
npm run css:build:watch &
40+
cd /app;
41+
# start development server with auto reloading
3742
./manage.py runserver 0.0.0.0:8080
3843
else
3944
./manage.py runserver 0.0.0.0:8080 --noreload
@@ -46,6 +51,8 @@ services:
4651
- ../checks:/app/checks
4752
- ../interface:/app/interface
4853
- ../internetnl:/app/internetnl
54+
# mount CSS files so `css:build:watch` can detect changes an rebuild
55+
- ../frontend/css:/app/frontend/css
4956

5057
worker:
5158
volumes:

frontend/js/imagecheck.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function ImageCheck() {
1515
* #unique is added to always get the file from the server if a newer
1616
* version exists.
1717
*/
18-
objCSS_noimages.href = '/static/css/alt-min.css'+'#'+unique;
18+
objCSS_noimages.href = '/static/css/alt.css'+'#'+unique;
1919
objCSS_noimages.type = 'text/css';
2020
}
2121
if (check_if_browser_in_high_contrast()) {
@@ -28,7 +28,7 @@ function ImageCheck() {
2828
* #unique is added to always get the file from the server if a newer
2929
* version exists.
3030
*/
31-
objCSS_highcontrast.href = '/static/css/high-contrast-min.css'+'#'+unique;
31+
objCSS_highcontrast.href = '/static/css/high-contrast.css'+'#'+unique;
3232
objCSS_highcontrast.type = 'text/css';
3333
}
3434
}

frontend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"scripts": {
33
"css:lint": "stylelint css/*.css",
4-
"css:build": "postcss css/*.css --dir build",
5-
"css:build:watch": "postcss css/*.css --dir build --watch --poll"
4+
"css:build": "postcss css/*.css --dir /app/static_frontend/css/",
5+
"css:build:watch": "postcss css/*.css --dir /app/static_frontend/css/ --watch --poll"
66
},
77
"devDependencies": {
88
"autoprefixer": "^10.4.20",
@@ -16,4 +16,4 @@
1616
"browserslist": [
1717
"defaults"
1818
]
19-
}
19+
}

integration_tests/common/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_static_files(page, app_url_subdomain):
9090

9191

9292
def test_generated_css_static_files(page, app_url_subdomain):
93-
response = page.request.get(app_url_subdomain + "/static/css/style-min.css")
93+
response = page.request.get(app_url_subdomain + "/static/css/style.css")
9494
expect(response).to_be_ok()
9595
assert "@font-face" in response.text()
9696
assert "expires" in response.headers

integration_tests/develop/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_static_files(page, app_url):
4141

4242

4343
def test_generated_css_static_files(page, app_url):
44-
response = requests.get(app_url + "/static/css/style-min.css", verify=False)
44+
response = requests.get(app_url + "/static/css/style.css", verify=False)
4545
response.raise_for_status()
4646
assert "@font-face" in response.text
4747
assert "expires" in response.headers

integration_tests/live/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_static_files(page, app_url):
4646

4747

4848
def test_generated_css_static_files(page, app_url):
49-
response = requests.get(app_url + "/static/css/style-min.css")
49+
response = requests.get(app_url + "/static/css/style.css")
5050
response.raise_for_status()
5151
assert "@font-face" in response.text
5252
assert "expires" in response.headers

interface/templates/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<link rel="icon" href="{% static "favicon.png" %}" sizes="192x192" />
1616
<link rel="apple-touch-icon-precomposed" href="{% static 'favicon.png' %}" />
1717
<meta name="msapplication-TileImage" content="{% static 'favicon.png' %}" />
18-
<link rel="stylesheet" type="text/css" href="{% static 'css/style-min.css' %}" />
19-
<link rel="stylesheet" type="text/css" href="{% static 'css/print-min.css' %}" media="print" />
18+
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
19+
<link rel="stylesheet" type="text/css" href="{% static 'css/print.css' %}" media="print" />
2020

2121
<script src="{% static "js/jquery-3.5.1.min.js" %}"></script>
2222

0 commit comments

Comments
 (0)