From 3f98c2ddbe8071ecfd5af8f9c8488644e17a9861 Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Thu, 21 Nov 2024 14:22:00 +0100 Subject: [PATCH] feat(vite): add HTTPS support with configurable SSL (#8585) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Pull Request adds support for local HTTPS configuration and introduces a new environment variable for the app's base URL. - Added new environment variable REACT_APP_BASE_URL in .env.example. - Introduced logic to utilize SSL certificates for local HTTPS in vite.config.ts. - Added validation to ensure SSL key and certificate paths are defined for HTTPS. - Included support for dynamic base URL setting based on the environment configuration. - Enhanced server configuration in vite.config.ts to handle HTTPS and local development. --------- Co-authored-by: Félix Malfait --- packages/twenty-front/.env.example | 5 ++++- packages/twenty-front/vite.config.ts | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/twenty-front/.env.example b/packages/twenty-front/.env.example index 345d0fb92..1f9a51b33 100644 --- a/packages/twenty-front/.env.example +++ b/packages/twenty-front/.env.example @@ -5,4 +5,7 @@ GENERATE_SOURCEMAP=false # REACT_APP_PORT=3001 # CHROMATIC_PROJECT_TOKEN= # VITE_DISABLE_TYPESCRIPT_CHECKER=true -# VITE_DISABLE_ESLINT_CHECKER=true \ No newline at end of file +# VITE_DISABLE_ESLINT_CHECKER=true +# VITE_ENABLE_SSL=false +# SSL_KEY_PATH="./certs/your-cert.key" +# SSL_CERT_PATH="./certs/your-cert.crt" \ No newline at end of file diff --git a/packages/twenty-front/vite.config.ts b/packages/twenty-front/vite.config.ts index c631a60e5..eec22a826 100644 --- a/packages/twenty-front/vite.config.ts +++ b/packages/twenty-front/vite.config.ts @@ -3,6 +3,7 @@ import { isNonEmptyString } from '@sniptt/guards'; import react from '@vitejs/plugin-react-swc'; import wyw from '@wyw-in-js/vite'; import path from 'path'; +import fs from 'fs'; import { defineConfig, loadEnv, searchForWorkspaceRoot } from 'vite'; import checker from 'vite-plugin-checker'; import svgr from 'vite-plugin-svgr'; @@ -18,6 +19,7 @@ export default defineConfig(({ command, mode }) => { VITE_BUILD_SOURCEMAP, VITE_DISABLE_TYPESCRIPT_CHECKER, VITE_DISABLE_ESLINT_CHECKER, + VITE_ENABLE_SSL, REACT_APP_PORT, } = env; @@ -62,13 +64,27 @@ export default defineConfig(({ command, mode }) => { }; } + if (VITE_ENABLE_SSL && (!env.SSL_KEY_PATH || !env.SSL_CERT_PATH)) { + throw new Error( + 'to use https SSL_KEY_PATH and SSL_CERT_PATH must be both defined', + ); + } + return { root: __dirname, cacheDir: '../../node_modules/.vite/packages/twenty-front', server: { - port, - host: 'localhost', + port: port, + protocol: VITE_ENABLE_SSL ? 'https' : 'http', + ...(VITE_ENABLE_SSL + ? { + https: { + key: fs.readFileSync(env.SSL_KEY_PATH), + cert: fs.readFileSync(env.SSL_CERT_PATH), + }, + } + : {}), fs: { allow: [ searchForWorkspaceRoot(process.cwd()),