Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load dotenv/config before reading the config #3085

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/toolpad-app/src/server/appServerWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function devServerPlugin({ config }: ToolpadAppDevServerParams): Plugin {
}

export interface AppViteServerConfig extends ToolpadAppDevServerParams {
toolpadDevMode: boolean;
port: number;
mainThreadRpcPort: MessagePort;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/toolpad-app/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -290,7 +291,7 @@ async function createToolpadHandler({
}: ToolpadHandlerConfig): Promise<AppHandler> {
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();
Expand Down
1 change: 1 addition & 0 deletions packages/toolpad-app/src/server/localMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions packages/toolpad-app/src/server/toolpadAppBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function toolpadVitePlugin(): Plugin {
}

export interface CreateViteConfigParams {
toolpadDevMode: boolean;
outDir: string;
root: string;
dev: boolean;
Expand All @@ -136,6 +137,7 @@ export interface CreateViteConfigParams {
}

export async function createViteConfig({
toolpadDevMode,
outDir,
root,
dev,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -343,6 +349,7 @@ export async function buildApp({
outDir,
}: ToolpadBuilderParams) {
const { viteConfig } = await createViteConfig({
toolpadDevMode: false,
dev: false,
root,
base,
Expand Down
1 change: 0 additions & 1 deletion packages/toolpad-app/src/server/toolpadAppServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dotenv/config';
import * as path from 'path';
import * as fs from 'fs/promises';
import { Server } from 'http';
Expand Down
1 change: 1 addition & 0 deletions packages/toolpad-app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export type ProjectEvents = {
};

export interface ToolpadProjectOptions {
toolpadDevMode: boolean;
dev: boolean;
externalUrl?: string;
base: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/toolpad-app/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))],
Expand Down
Loading