|
1 | | -import { defineConfig, Schema } from '@julr/vite-plugin-validate-env'; |
| 1 | +import { Schema, defineConfig } from '@julr/vite-plugin-validate-env'; |
| 2 | + |
| 3 | +const webAppServeEnabled = process.env.WEB_APP_SERVE_ENABLED?.toLowerCase() === 'true'; |
| 4 | +if (webAppServeEnabled) { |
| 5 | + console.warn('Building application for WEB_APP_SERVE') |
| 6 | +} |
2 | 7 |
|
3 | 8 | export default defineConfig({ |
4 | | - APP_TITLE: Schema.string(), |
5 | | - APP_ENVIRONMENT: (key, value) => { |
6 | | - // NOTE: APP_ENVIRONMENT_PLACEHOLDER is meant to be used with image builds |
7 | | - // The value will be later replaced with the actual value |
8 | | - const regex = /^production|staging|testing|alpha-\d+|development|APP_ENVIRONMENT_PLACEHOLDER$/; |
9 | | - const valid = !!value && (value.match(regex) !== null); |
10 | | - if (!valid) { |
11 | | - throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`); |
| 9 | + validator: "builtin", |
| 10 | + schema: { |
| 11 | + APP_TITLE: Schema.string(), |
| 12 | + APP_ENVIRONMENT: (key, value) => { |
| 13 | + const regex = /^production|staging|testing|alpha-\d+|development$/; |
| 14 | + const valid = !!value && (value.match(regex) !== null); |
| 15 | + if (!valid) { |
| 16 | + throw new Error(`Value for environment variable "${key}" must match regex "${regex}", instead received "${value}"`); |
| 17 | + } |
| 18 | + return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development'); |
| 19 | + }, |
| 20 | + APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }), |
| 21 | + APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }), |
| 22 | + APP_MAPBOX_ACCESS_TOKEN: Schema.string(), |
| 23 | + APP_TINY_API_KEY: Schema.string(), |
| 24 | + APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }), |
| 25 | + APP_SDT_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }), |
| 26 | + APP_SENTRY_DSN: Schema.string.optional(), |
| 27 | + APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(), |
| 28 | + APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE: Schema.number.optional(), |
| 29 | + APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE: Schema.number.optional(), |
| 30 | + APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(), |
| 31 | + }, |
| 32 | + overrideDefine: (key, value) => { |
| 33 | + // Default: |
| 34 | + if (!webAppServeEnabled) { |
| 35 | + return JSON.stringify(value); |
12 | 36 | } |
13 | | - if (value === 'APP_ENVIRONMENT_PLACEHOLDER') { |
14 | | - console.warn(`Using ${value} for app environment. Make sure to not use this for builds without helm chart`) |
| 37 | + // Override: Skip defining env variables if web app serve is enabled |
| 38 | + if (value === null || value === undefined) { |
| 39 | + // NOTE: value should always be defined during build |
| 40 | + throw `Value for ${key} should not be null or undefined`; |
15 | 41 | } |
16 | | - return value as ('production' | 'staging' | 'testing' | `alpha-${number}` | 'development' | 'APP_ENVIRONMENT_PLACEHOLDER'); |
| 42 | + const replacement_str = `WEB_APP_SERVE_PLACEHOLDER__${key}`; |
| 43 | + // NOTE: For string values, we need to stringify 'replacement_str' |
| 44 | + // This adds double quotes around the replacement string |
| 45 | + return typeof value === 'string' |
| 46 | + ? JSON.stringify(replacement_str) |
| 47 | + : replacement_str; |
17 | 48 | }, |
18 | | - APP_API_ENDPOINT: Schema.string({ format: 'url', protocol: true, tld: false }), |
19 | | - APP_ADMIN_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }), |
20 | | - APP_MAPBOX_ACCESS_TOKEN: Schema.string(), |
21 | | - APP_TINY_API_KEY: Schema.string(), |
22 | | - APP_RISK_API_ENDPOINT: Schema.string({ format: 'url', protocol: true }), |
23 | | - APP_SDT_URL: Schema.string.optional({ format: 'url', protocol: true, tld: false }), |
24 | | - APP_SENTRY_DSN: Schema.string.optional(), |
25 | | - APP_SENTRY_TRACES_SAMPLE_RATE: Schema.number.optional(), |
26 | | - APP_SENTRY_REPLAYS_SESSION_SAMPLE_RATE: Schema.number.optional(), |
27 | | - APP_SENTRY_REPLAYS_ON_ERROR_SAMPLE_RATE: Schema.number.optional(), |
28 | | - APP_GOOGLE_ANALYTICS_ID: Schema.string.optional(), |
29 | 49 | }); |
0 commit comments