Skip to content

Commit

Permalink
Organise code better
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Nov 29, 2024
1 parent 54f3602 commit 432d2f4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
14 changes: 14 additions & 0 deletions waspc/data/Generator/templates/react-app/plugins/validateEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type Plugin, loadEnv } from "vite";

import { ensureEnvSchema } from 'wasp/env'
import { clientEnvSchema } from 'wasp/client/env/schema'

export function validateEnv(): Plugin {
return {
name: 'wasp-validate-env',
config: (config) => {
const env = loadEnv(config.mode, process.cwd(), config.envPrefix)
ensureEnvSchema(env, clientEnvSchema)
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts", "./src/ext-src/vite.config.ts"]
"include": ["vite.config.ts", "./src/ext-src/vite.config.ts", "./plugins/"]
}
17 changes: 3 additions & 14 deletions waspc/data/Generator/templates/react-app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{{={= =}=}}
/// <reference types="vitest" />
import { mergeConfig, type Plugin, loadEnv } from "vite";
import { mergeConfig } from "vite";
import react from "@vitejs/plugin-react";
import { defaultExclude } from "vitest/config"

import { ensureEnvSchema } from 'wasp/dev'
import { clientEnvSchema } from 'wasp/client/env/schema'
import { validateEnv } from "./plugins/validateEnv.js";

{=# customViteConfig.isDefined =}
// Ignoring the TS error because we are importing a file outside of TS root dir.
Expand All @@ -20,8 +19,8 @@ const _waspUserProvidedConfig = {};
const defaultViteConfig = {
base: "{= baseDir =}",
plugins: [
react(),
validateEnv(),
react(),
],
optimizeDeps: {
exclude: ['wasp']
Expand Down Expand Up @@ -59,13 +58,3 @@ export default mergeConfig(
defaultViteConfig,
_waspUserProvidedConfig
);

function validateEnv(): Plugin {
return {
name: 'wasp-validate-env',
config: (config) => {
const env = loadEnv(config.mode, process.cwd(), config.envPrefix)
ensureEnvSchema(env, clientEnvSchema)
},
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{={= =}=}}
import * as z from 'zod'

// PRIVATE API (SDK, Vite config)
// PRIVATE API (sdk, Vite config)
export const clientEnvSchema = z.object({
REACT_APP_API_URL: z
.string()
Expand Down
2 changes: 0 additions & 2 deletions waspc/data/Generator/templates/sdk/wasp/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ import { join as joinPaths } from 'path'
export function resolveProjectPath(path: string): string {
return joinPaths('../../../', path)
}

export { ensureEnvSchema } from '../env/index.js'
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/wasp/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as z from 'zod'

const redColor = '\x1b[31m'

// PRIVATE API (SDK, Vite config)
export function ensureEnvSchema<Schema extends z.ZodTypeAny>(
data: unknown,
schema: Schema
Expand Down
1 change: 1 addition & 0 deletions waspc/data/Generator/templates/sdk/wasp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"./client/test": "./dist/client/test/index.js",
"./client": "./dist/client/index.js",
"./client/env/schema": "./dist/client/env/schema.js",
"./env": "./dist/env/index.js",
"./dev": "./dist/dev/index.js",

{=! todo(filip): Fixes below are for type errors in 0.13.1, remove ASAP =}
Expand Down
2 changes: 2 additions & 0 deletions waspc/src/Wasp/Generator/WebAppGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ genWebApp spec = do
genNpmrc,
genGitignore,
genIndexHtml spec,
-- Vite plugins
genFileCopy [relfile|plugins/validateEnv.ts|],
genViteConfig spec
]
<++> genSrcDir spec
Expand Down

0 comments on commit 432d2f4

Please sign in to comment.