From 624571d22879236a412d6b98a00d2b919b8ac103 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Tue, 16 Jan 2024 12:26:37 +0100 Subject: [PATCH] Load dotenv/config before reading the config (#3085) --- packages/toolpad-app/src/server/appServerWorker.ts | 1 + packages/toolpad-app/src/server/config.ts | 2 +- packages/toolpad-app/src/server/index.ts | 3 ++- packages/toolpad-app/src/server/localMode.ts | 1 + packages/toolpad-app/src/server/toolpadAppBuilder.ts | 11 +++++++++-- packages/toolpad-app/src/server/toolpadAppServer.ts | 1 - packages/toolpad-app/src/types.ts | 1 + packages/toolpad-app/tsup.config.ts | 6 ++++++ 8 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/toolpad-app/src/server/appServerWorker.ts b/packages/toolpad-app/src/server/appServerWorker.ts index e3f62778920..f28e481c93d 100644 --- a/packages/toolpad-app/src/server/appServerWorker.ts +++ b/packages/toolpad-app/src/server/appServerWorker.ts @@ -66,6 +66,7 @@ function devServerPlugin({ config }: ToolpadAppDevServerParams): Plugin { } export interface AppViteServerConfig extends ToolpadAppDevServerParams { + toolpadDevMode: boolean; port: number; mainThreadRpcPort: MessagePort; } diff --git a/packages/toolpad-app/src/server/config.ts b/packages/toolpad-app/src/server/config.ts index 9e7e169a2ac..e40ea6bb588 100644 --- a/packages/toolpad-app/src/server/config.ts +++ b/packages/toolpad-app/src/server/config.ts @@ -19,7 +19,7 @@ export type ServerConfig = { function readConfig(): ServerConfig { if (typeof window !== 'undefined') { - throw new Error(`Serverside config can't be loaded on the client side`); + throw new Error(`Server-side config can't be loaded on the client side`); } // Whitespace separated, do not use spaces in your keys diff --git a/packages/toolpad-app/src/server/index.ts b/packages/toolpad-app/src/server/index.ts index 82c057ee3c8..7b1f377facb 100644 --- a/packages/toolpad-app/src/server/index.ts +++ b/packages/toolpad-app/src/server/index.ts @@ -63,6 +63,7 @@ async function createDevHandler(project: ToolpadProject) { const worker = new Worker(appServerPath, { workerData: { + toolpadDevMode: project.options.toolpadDevMode, outDir: project.getAppOutputFolder(), base: project.options.base, config: runtimeConfig, @@ -292,7 +293,7 @@ async function createToolpadHandler({ }: ToolpadHandlerConfig): Promise { const editorBasename = '/_toolpad'; - const project = await initProject({ dev, dir, externalUrl, base }); + const project = await initProject({ toolpadDevMode, dev, dir, externalUrl, base }); await project.start(); const router = express.Router(); diff --git a/packages/toolpad-app/src/server/localMode.ts b/packages/toolpad-app/src/server/localMode.ts index 0f67461daf5..3e01bc3e62f 100644 --- a/packages/toolpad-app/src/server/localMode.ts +++ b/packages/toolpad-app/src/server/localMode.ts @@ -1326,6 +1326,7 @@ export async function initProject({ dir: dirInput, ...config }: InitProjectOptio const dir = resolveProjectDir(dirInput); const resolvedConfig: ToolpadProjectOptions = { + toolpadDevMode: false, dev: false, base: '/prod', customServer: false, diff --git a/packages/toolpad-app/src/server/toolpadAppBuilder.ts b/packages/toolpad-app/src/server/toolpadAppBuilder.ts index 046fa6fcc23..a52f4604f92 100644 --- a/packages/toolpad-app/src/server/toolpadAppBuilder.ts +++ b/packages/toolpad-app/src/server/toolpadAppBuilder.ts @@ -124,6 +124,7 @@ function toolpadVitePlugin(): Plugin { } export interface CreateViteConfigParams { + toolpadDevMode: boolean; outDir: string; root: string; dev: boolean; @@ -136,6 +137,7 @@ export interface CreateViteConfigParams { } export async function createViteConfig({ + toolpadDevMode, outDir, root, dev, @@ -308,8 +310,12 @@ if (import.meta.hot) { }, }, optimizeDeps: { - entries: [MAIN_ENTRY], - include: FALLBACK_MODULES.map((moduleName) => `@mui/toolpad > ${moduleName}`), + force: toolpadDevMode ? true : undefined, + include: [ + ...FALLBACK_MODULES.map((moduleName) => `@mui/toolpad > ${moduleName}`), + '@mui/toolpad/runtime', + '@mui/toolpad/canvas', + ], }, appType: 'custom', logLevel: 'info', @@ -343,6 +349,7 @@ export async function buildApp({ outDir, }: ToolpadBuilderParams) { const { viteConfig } = await createViteConfig({ + toolpadDevMode: false, dev: false, root, base, diff --git a/packages/toolpad-app/src/server/toolpadAppServer.ts b/packages/toolpad-app/src/server/toolpadAppServer.ts index 155099df7b6..7899f67aedc 100644 --- a/packages/toolpad-app/src/server/toolpadAppServer.ts +++ b/packages/toolpad-app/src/server/toolpadAppServer.ts @@ -1,4 +1,3 @@ -import 'dotenv/config'; import * as path from 'path'; import * as fs from 'fs/promises'; import { Server } from 'http'; diff --git a/packages/toolpad-app/src/types.ts b/packages/toolpad-app/src/types.ts index f62796bf1b9..dc584845c69 100644 --- a/packages/toolpad-app/src/types.ts +++ b/packages/toolpad-app/src/types.ts @@ -205,6 +205,7 @@ export type ProjectEvents = { }; export interface ToolpadProjectOptions { + toolpadDevMode: boolean; dev: boolean; externalUrl?: string; base: string; diff --git a/packages/toolpad-app/tsup.config.ts b/packages/toolpad-app/tsup.config.ts index 7f0723223fc..c2c20a177ab 100644 --- a/packages/toolpad-app/tsup.config.ts +++ b/packages/toolpad-app/tsup.config.ts @@ -30,6 +30,12 @@ export default defineConfig((options) => [ format: ['esm'], outDir: 'dist/cli', silent: true, + // Code splitting in esbuild is buggy. It doesn't preserve order of imports correctly. + // It's important that `dotenv/config` remains the first import in the program because it + // needs to set up environment variables before anything else is imported. + // To fix this, we disable code splitting. + // See: https://github.com/evanw/esbuild/issues/399 + splitting: false, clean: !options.watch, sourcemap: true, esbuildPlugins: [cleanFolderOnFailure(path.resolve(__dirname, './dist/cli'))],