Skip to content

Commit

Permalink
task/DES-2708: Build and serve React assets on deploy (#1173)
Browse files Browse the repository at this point in the history
* Build and serve React assets

* fix template string

* Add custom render block for react assets
  • Loading branch information
jarosenb authored Mar 27, 2024
1 parent a6a9fec commit 8abe49d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ coverage
.docs
.github
.pytest_cache
data
data

client/node_modules
client/.nx
client/dist
2 changes: 1 addition & 1 deletion client/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/client",
"base": "/static"
"base": "/static/react-assets"
},
"configurations": {
"development": {
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import path from 'path';

export default defineConfig({
root: __dirname,
Expand Down Expand Up @@ -30,6 +31,11 @@ export default defineConfig({
commonjsOptions: {
transformMixedEsModules: true,
},
rollupOptions: {
input: {
imports: path.resolve(__dirname, 'react-assets.html'),
},
},
},

test: {
Expand Down
15 changes: 15 additions & 0 deletions conf/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ RUN npm ci
COPY . /designsafe/
RUN npm run build

FROM node:20 as node_nx_build
COPY client/package.json client/package-lock.json /designsafe/
WORKDIR /designsafe/client
RUN npm ci

COPY client/ /designsafe/client/
RUN npm run build



FROM python:3.11-buster

Expand Down Expand Up @@ -46,9 +55,15 @@ RUN groupadd --gid 816877 G-816877 && \
useradd --uid 458981 --gid G-816877 -m --shell /bin/bash tg458981 -d /home/tg458981

COPY --from=node_build /designsafe/ /srv/www/designsafe

COPY --from=node_nx_build /designsafe/client/dist/client/ /srv/www/designsafe/designsafe/static/react-assets/
COPY --from=node_nx_build /designsafe/client/dist/client/react-assets.html /srv/www/designsafe/designsafe/templates/react-assets.html

RUN chown tg458981:G-816877 /srv/www/designsafe /srv/www/designsafe/*
RUN chown -R tg458981:G-816877 /srv/www/designsafe/designsafe



USER tg458981

WORKDIR /srv/www/designsafe
30 changes: 12 additions & 18 deletions designsafe/apps/data/templates/data/data_depot.j2
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,19 @@ Data Depot
</div>
{%endif%}

{% addtoblock "css" %}
{% endaddtoblock %}

{% addtoblock "js" %}
{% if debug and react_flag %}
<script type="module">
import RefreshRuntime from 'http://localhost:4200/static/@react-refresh';
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
</script>
<script type="module" src="http://localhost:4200/static/@vite/client"></script>
<script type="module" src="http://localhost:4200/static/src/main.tsx"></script>
{% addtoblock "react_assets" %}
{% if debug %}
<script type="module">
import RefreshRuntime from 'http://localhost:4200/static/@react-refresh';
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
</script>
<script type="module" src="http://localhost:4200/static/@vite/client"></script>
<script type="module" src="http://localhost:4200/static/src/main.tsx"></script>
{% else %}
<!--TODO: setup non-dev static import for React code-->
{% include "react-assets.html" %}
{% endif %}
{% if not react_flag %}
<script src="<%= htmlWebpackPlugin.files.js %>"></script>
{% endif %}
{% endaddtoblock %}
{% endblock %}
1 change: 1 addition & 0 deletions designsafe/templates/base.j2
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<link href="{% static 'styles/ng-designsafe.css' %}" rel="stylesheet">
{% block styles %}{% endblock %}
{% render_block "css" %}
{% render_block "react_assets" %}
<base href="/">
{% recaptcha_init 'en' %}

Expand Down

0 comments on commit 8abe49d

Please sign in to comment.