Skip to content

Commit

Permalink
Merge pull request #68 from serlo/build/transpile-bundle-backend-esbuild
Browse files Browse the repository at this point in the history
build(backend): transpile and bundle using esbuild
  • Loading branch information
LarsTheGlidingSquirrel authored Sep 30, 2024
2 parents 6273244 + 4e80143 commit 5c6ff65
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 11 deletions.
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:20.17.0 as build
WORKDIR /usr/src/app

COPY . .
RUN yarn install --immutable

RUN yarn build

FROM node:20.17.0 as production

WORKDIR /usr/src/app

COPY --from=build /usr/src/app/dist dist

EXPOSE 3000

ENTRYPOINT ["node", "dist/backend/index.cjs"]
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ RUN yarn build

EXPOSE 3000

ENTRYPOINT ["yarn", "start:dev"]
ENTRYPOINT ["yarn", "start"]
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"author": "Serlo Education e.V.",
"type": "module",
"scripts": {
"build": "tsc && vite build",
"build": "npm-run-all --parallel \"build:*\"",
"build:backend": "esbuild ./src/backend/index.ts --bundle --platform=node --outfile=dist/backend/index.cjs",
"build:frontend": "vite build",
"dev": "docker compose up --watch --build",
"format": "npm-run-all --sequential --continue-on-error \"format:*\"",
"format:eslint": "eslint --fix",
Expand All @@ -20,7 +22,8 @@
"push-image": "yarn tsx push-image.ts",
"push-image:dev": "yarn push-image dev",
"push-image:latest": "yarn push-image latest",
"start:dev": "tsx --watch src/backend/index.ts"
"start:dev": "tsx --watch src/backend/index.ts",
"start": "node dist/backend/index.cjs"
},
"dependencies": {
"@serlo/editor": "0.14.0",
Expand Down Expand Up @@ -48,6 +51,7 @@
"@types/react-dom": "^18.3.0",
"@types/uuid": "^10.0.0",
"@vitejs/plugin-react-swc": "^3.7.1",
"esbuild": "^0.24.0",
"eslint": "^9.10.0",
"eslint-plugin-react": "^7.36.1",
"eslint-plugin-react-hooks": "^4.6.2",
Expand Down
4 changes: 2 additions & 2 deletions scripts/setup_uberspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ echo 'Updated environment variables'
yarn
echo 'Installed dependencies using Yarn'

# Build frontend
# Build frontend and backend
yarn build
echo 'Built the frontend app using Yarn'
echo 'Built the frontend and the backend apps using Yarn'

# Run the backend as an Uberspace service
cp ./uberspace/app.ini ~/etc/services.d/
Expand Down
7 changes: 2 additions & 5 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { Pool, createPool } from 'mysql2/promise'
import { Database } from './database'
import { v4 as uuidv4 } from 'uuid'

// Requires Node.js 20.11 or higher
const __dirname = import.meta.dirname

const ltijsKey = readEnvVariable('LTIJS_KEY')
const mongodbConnectionUri = readEnvVariable('MONGODB_CONNECTION_URI')
const ltiPlatform = {
Expand Down Expand Up @@ -71,7 +68,7 @@ ltijs.setup(
loginUrl: '/lti/login',
keysetUrl: '/lti/keys',
dynRegRoute: '/lti/register',
staticPath: path.join(__dirname, './../../dist'), // Path to static files
staticPath: path.join(__dirname, './../../dist/frontend'), // Path to static files
cookies: {
secure: process.env['ENVIRONMENT'] === 'local' ? false : true, // Set secure to true if the testing platform is in a different domain and https is being used
sameSite: process.env['ENVIRONMENT'] === 'local' ? '' : 'None', // Set sameSite to 'None' if the testing platform is in a different domain and https is being used
Expand All @@ -87,7 +84,7 @@ ltijs.app.use((_, res, next) => {

// Opens Serlo editor
ltijs.app.get('/app', async (_, res) => {
return res.sendFile(path.join(__dirname, '../../dist/index.html'))
return res.sendFile(path.join(__dirname, '../../dist/frontend/index.html'))
})

// Endpoint to get content
Expand Down
2 changes: 1 addition & 1 deletion uberspace/app.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[program:serlo-app]
directory=/home/%(ENV_USER)s/serlo-editor-as-lti-tool
command=yarn start:dev
command=yarn start
autostart=true
autorestart=true
environment=NODE_ENV=production
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import react from '@vitejs/plugin-react-swc'

// https://vitejs.dev/config/
export default defineConfig({
build: {
outDir: './dist/frontend',
},
plugins: [react()],
})
Loading

0 comments on commit 5c6ff65

Please sign in to comment.