Skip to content

Commit f740a81

Browse files
committed
[PPHA-182] Build assets using rollup and sass
In production build assets as a Docker build stage In development add a new docker compose service for watching assets and building on the fly. Compiled assets are then synced back to the host machine in order to present to the running django server
1 parent 93f3fbc commit f740a81

File tree

12 files changed

+2439
-0
lines changed

12 files changed

+2439
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414

1515
.env
1616
__pycache__
17+
lung_cancer_screening/assets/compiled/*
18+
!lung_cancer_screening/assets/compiled/.gitkeep

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
FROM node:24.4.1-alpine3.21 AS asset_builder
2+
3+
WORKDIR /app
4+
5+
COPY package.json package-lock.json rollup.config.js ./
6+
COPY lung_cancer_screening ./lung_cancer_screening
7+
RUN npm ci
8+
RUN npm run compile
9+
110
FROM python:3.12-alpine3.19 AS builder
211

312
ENV PYTHONDONTWRITEBYTECODE=1 \
@@ -40,6 +49,8 @@ WORKDIR /app
4049

4150
COPY --chown=app:app . .
4251

52+
COPY --from=asset_builder --chown=app:app /app/lung_cancer_screening/assets/compiled /app/lung_cancer_screening/assets/compiled
53+
4354
USER app
4455

4556
EXPOSE 8000

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ services:
1313
- ./lung_cancer_screening:/app/lung_cancer_screening
1414
restart: unless-stopped
1515

16+
asset_builder:
17+
build:
18+
context: .
19+
dockerfile: Dockerfile
20+
target: asset_builder
21+
command: npm run watch
22+
volumes:
23+
- ./lung_cancer_screening/assets/compiled:/app/lung_cancer_screening/assets/compiled
24+
1625
db:
1726
image: postgres:15-alpine
1827
env_file:

lung_cancer_screening/assets/compiled/.gitkeep

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "nhsuk-frontend/packages/nhsuk.js";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Import NHS.UK frontend library
2+
@import "nhsuk-frontend/packages/nhsuk";

lung_cancer_screening/core/jinja2/layout.jinja

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
{% set assetPath = STATIC_URL ~ "/assets" %}
2+
13
{% extends 'template.jinja' %}
24

5+
{% block head %}
6+
<link rel="stylesheet" href="{{ static('css/main.css' )}}">
7+
<script type="module" src="{{ static('js/bundle.js' )}}"></script>
8+
{% endblock %}
9+
310
{% block pageTitle %}Lung Cancer Screening - NHS{% endblock %}
411

512
{% block content %}

lung_cancer_screening/jinja2_env.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from django.conf import settings
2+
from django.templatetags.static import static
3+
from django.urls import reverse
14
from jinja2 import ChoiceLoader, Environment, PackageLoader
25

6+
37
def environment(**options):
48

59
env = Environment(**options, extensions=["jinja2.ext.do"])
@@ -19,4 +23,8 @@ def environment(**options):
1923
]
2024
)
2125

26+
env.globals.update(
27+
{"static": static, "url": reverse, "STATIC_URL": settings.STATIC_URL}
28+
)
29+
2230
return env

lung_cancer_screening/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ def boolean_env(key, default=None):
139139

140140
STATIC_URL = 'static/'
141141

142+
STATIC_ROOT = BASE_DIR / "staticfiles"
143+
144+
STATICFILES_DIRS = [
145+
BASE_DIR / "lung_cancer_screening" / "assets" / "compiled"
146+
]
147+
142148
# Default primary key field type
143149
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
144150

0 commit comments

Comments
 (0)