From 164d2e6b658a7b91af02a445781035b2daf862d6 Mon Sep 17 00:00:00 2001 From: Shrikant Sharat Kandula Date: Tue, 3 Oct 2023 07:00:40 +0530 Subject: [PATCH] ci: Use esbuild to build RTS (#27310) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes RTS build to use `esbuild`. 1. This means the whole `node_modules` won't need to be copied over to the Docker image. There's unused insignifant _test_ files in there, that don't add any value, but are causing irrelevant CVEs to be reported on our Docker image. See example at https://github.com/appsmithorg/appsmith-ee/pull/2349. 2. Much faster. Not that RTS build is our slow point, but still. Perhaps we can move client to `esbuild` too. 🙂 ## Why are we doing this? The current method of loading RTS into the Docker image means that _all_ contents of _all_ dependencies are copied over. The whole `node_modules`. But several of these packages include _test_ files too, that aren't needed at runtime at all. One of such test files is creating a false alert for a CVE on our Docker image. Has absolutely no relevance and impact, but it's there. To fix that, I [had to `rm -rf /opt/appsmith/rts/node_modules/*/test` in the Docker image](https://github.com/appsmithorg/appsmith-ee/pull/2349/files). This felt very hacky, and very dirty. It felt like we're introducing more debt and more duct tape around the current build process. So, `esbuild`. ## Where is `esbuild` coming from? We're using `esbuild` v0.18.20 only, while the latest is v0.19.3. We need to update `design-system`'s storybook dependency, I think, to get a more recent version of `esbuild`. I'm yet to figure this out and can use some help. 🙂 --- Dockerfile | 2 +- app/client/packages/rts/build.sh | 26 +++++++++---------- app/client/packages/rts/src/server.ts | 7 ----- .../supervisord/application_process/rts.conf | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index e885af058d..e72caca16b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -78,7 +78,7 @@ COPY ${PLUGIN_JARS} backend/plugins/ COPY ./app/client/build editor/ # Add RTS - Application Layer -COPY ./app/client/packages/rts/package.json ./app/client/packages/rts/dist rts/ +COPY ./app/client/packages/rts/dist rts/ RUN cd ./utils && npm install --only=prod && npm install --only=prod -g . && cd - \ && chmod 0644 /etc/cron.d/* \ diff --git a/app/client/packages/rts/build.sh b/app/client/packages/rts/build.sh index d75897f57b..12dcf98b59 100755 --- a/app/client/packages/rts/build.sh +++ b/app/client/packages/rts/build.sh @@ -3,18 +3,18 @@ set -o errexit cd "$(dirname "$0")" -rm -rf dist/ -# This is required for the first time build as node_modules is not present in the image -yarn install --immutable -yarn tsc && yarn tsc-alias -# Install only production dependencies -YARN_NM_HOISTING_LIMITS=workspaces yarn workspaces focus --production appsmith-rts -# Copying node_modules directory into dist as rts server requires production dependencies to run server build properly. -# This was previously being done in dockerfile which was copying the symlinks to image rather than the whole directory of shared modules (e.g. AST) -# Also, we copy node_modules with -L flag in order to follow the symlinks for @shared folder and copy the contents instead of just the symlink -cp -RL node_modules ./dist -# Delete production dependencies -rm -rf node_modules -# Restore all dependencies +root="$(git rev-parse --show-toplevel)" + yarn install --immutable +yarn run tsc --noEmit + +rm -rf dist +exec "$root/app/client/node_modules/.bin/esbuild" src/server.ts \ + --bundle \ + --minify \ + --sourcemap \ + --platform=node \ + --target="$(node --version | sed s/v/node/)" \ + --outdir=dist/bundle \ + --external:dtrace-provider diff --git a/app/client/packages/rts/src/server.ts b/app/client/packages/rts/src/server.ts index cd2802c9ca..0674d9b81f 100644 --- a/app/client/packages/rts/src/server.ts +++ b/app/client/packages/rts/src/server.ts @@ -1,5 +1,4 @@ import http from "http"; -import path from "path"; import express from "express"; import { Server } from "socket.io"; import type { LogLevelDesc } from "loglevel"; @@ -35,16 +34,10 @@ const io = new Server(server, { path: RTS_BASE_PATH, }); -// Initializing Sockets initializeSockets(io); // parse incoming json requests app.use(express.json({ limit: "5mb" })); -// Initializing Routes -app.use(express.static(path.join(__dirname, "static"))); -app.get("/", (_, res) => { - res.redirect("/index.html"); -}); app.use(`${RTS_BASE_API_PATH}/ast`, ast_routes); app.use(`${RTS_BASE_API_PATH}`, health_check_routes); diff --git a/deploy/docker/fs/opt/appsmith/templates/supervisord/application_process/rts.conf b/deploy/docker/fs/opt/appsmith/templates/supervisord/application_process/rts.conf index c25b0b353f..9f94487204 100644 --- a/deploy/docker/fs/opt/appsmith/templates/supervisord/application_process/rts.conf +++ b/deploy/docker/fs/opt/appsmith/templates/supervisord/application_process/rts.conf @@ -1,5 +1,5 @@ [program:rts] -directory=/opt/appsmith/rts +directory=/opt/appsmith/rts/bundle command=/opt/appsmith/run-with-env.sh node server.js priority=15 autostart=true