Skip to content

Commit e4e70ac

Browse files
committed
feat: add desktop specific resolver
1 parent fd81327 commit e4e70ac

File tree

4 files changed

+115
-114
lines changed

4 files changed

+115
-114
lines changed

packages/shared/src/env.desktop.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createEnv } from "@t3-oss/env-core"
2+
import { z } from "zod"
3+
4+
export const isDev =
5+
"process" in globalThis ? process.env.NODE_ENV === "development" : import.meta.env.DEV
6+
export const env = createEnv({
7+
clientPrefix: "VITE_",
8+
client: {
9+
VITE_WEB_URL: z.string().url().default("https://app.follow.is"),
10+
VITE_API_URL: z.string(),
11+
VITE_DEV_PROXY: z.string().optional(),
12+
VITE_SENTRY_DSN: z.string().optional(),
13+
VITE_INBOXES_EMAIL: z.string().default("@follow.re"),
14+
VITE_FIREBASE_CONFIG: z.string().optional(),
15+
16+
VITE_OPENPANEL_CLIENT_ID: z.string().optional(),
17+
VITE_OPENPANEL_API_URL: z.string().url().optional(),
18+
19+
// For external, use api_url if you don't want to fill it in.
20+
VITE_EXTERNAL_PROD_API_URL: z.string().optional(),
21+
VITE_EXTERNAL_DEV_API_URL: z.string().optional(),
22+
VITE_EXTERNAL_API_URL: z.string().optional(),
23+
VITE_WEB_PROD_URL: z.string().optional(),
24+
VITE_WEB_DEV_URL: z.string().optional(),
25+
},
26+
27+
emptyStringAsUndefined: true,
28+
runtimeEnv: getRuntimeEnv() as any,
29+
30+
skipValidation: "process" in globalThis ? process.env.VITEST === "true" : false,
31+
})
32+
33+
function metaEnvIsEmpty() {
34+
try {
35+
return Object.keys(import.meta.env || {}).length === 0
36+
} catch {
37+
return true
38+
}
39+
}
40+
41+
function getRuntimeEnv() {
42+
try {
43+
if (metaEnvIsEmpty()) {
44+
return process.env
45+
}
46+
return injectExternalEnv(import.meta.env)
47+
} catch {
48+
return process.env
49+
}
50+
}
51+
52+
declare const globalThis: any
53+
function injectExternalEnv<T>(originEnv: T): T {
54+
if (!("document" in globalThis)) {
55+
return originEnv
56+
}
57+
const prefix = "__followEnv"
58+
const env = globalThis[prefix]
59+
if (!env) {
60+
return originEnv
61+
}
62+
63+
for (const key in env) {
64+
originEnv[key as keyof T] = env[key]
65+
}
66+
return originEnv
67+
}

packages/shared/src/env.mobile.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ const profile = "prod"
22

33
const appEndpointMap = {
44
prod: {
5-
api: "https://api.follow.is",
6-
web: "https://app.follow.is",
5+
VITE_API_URL: "https://api.follow.is",
6+
VITE_WEB_URL: "https://app.follow.is",
7+
VITE_INBOXES_EMAIL: "@follow.re",
78
},
89
dev: {
9-
api: "https://api.dev.follow.is",
10-
web: "https://dev.follow.is",
10+
VITE_API_URL: "https://api.dev.follow.is",
11+
VITE_WEB_URL: "https://dev.follow.is",
12+
VITE_INBOXES_EMAIL: "__devdev@follow.re",
1113
},
1214
staging: {
13-
api: "https://api.follow.is",
14-
web: "https://staging.follow.is",
15+
VITE_API_URL: "https://api.follow.is",
16+
VITE_WEB_URL: "https://staging.follow.is",
17+
VITE_INBOXES_EMAIL: "@follow.re",
1518
},
1619
}
1720

1821
export const env = {
19-
VITE_WEB_URL: appEndpointMap[profile].web,
20-
VITE_API_URL: appEndpointMap[profile].api,
22+
VITE_WEB_URL: appEndpointMap[profile].VITE_WEB_URL,
23+
VITE_API_URL: appEndpointMap[profile].VITE_API_URL,
2124
}

packages/shared/src/env.ts

Lines changed: 19 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,23 @@
1-
import { createEnv } from "@t3-oss/env-core"
21
import { z } from "zod"
32

4-
export const isDev =
5-
"process" in globalThis ? process.env.NODE_ENV === "development" : import.meta.env.DEV
6-
export const env = createEnv({
7-
clientPrefix: "VITE_",
8-
client: {
9-
VITE_WEB_URL: z.string().url().default("https://app.follow.is"),
10-
VITE_API_URL: z.string(),
11-
VITE_DEV_PROXY: z.string().optional(),
12-
VITE_SENTRY_DSN: z.string().optional(),
13-
VITE_INBOXES_EMAIL: z.string().default("@follow.re"),
14-
VITE_FIREBASE_CONFIG: z.string().optional(),
15-
16-
VITE_OPENPANEL_CLIENT_ID: z.string().optional(),
17-
VITE_OPENPANEL_API_URL: z.string().url().optional(),
18-
19-
// For external, use api_url if you don't want to fill it in.
20-
VITE_EXTERNAL_PROD_API_URL: z.string().optional(),
21-
VITE_EXTERNAL_DEV_API_URL: z.string().optional(),
22-
VITE_EXTERNAL_API_URL: z.string().optional(),
23-
VITE_WEB_PROD_URL: z.string().optional(),
24-
VITE_WEB_DEV_URL: z.string().optional(),
25-
},
26-
27-
emptyStringAsUndefined: true,
28-
runtimeEnv: getRuntimeEnv() as any,
29-
30-
skipValidation: "process" in globalThis ? process.env.VITEST === "true" : false,
31-
})
32-
33-
function metaEnvIsEmpty() {
34-
try {
35-
return Object.keys(import.meta.env || {}).length === 0
36-
} catch {
37-
return true
38-
}
3+
export const envSchema = {
4+
VITE_WEB_URL: z.string().url().default("https://app.follow.is"),
5+
VITE_API_URL: z.string().default("https://api.follow.is"),
6+
VITE_DEV_PROXY: z.string().optional(),
7+
VITE_SENTRY_DSN: z.string().optional(),
8+
VITE_INBOXES_EMAIL: z.string().default("@follow.re"),
9+
VITE_FIREBASE_CONFIG: z.string().optional(),
10+
11+
VITE_OPENPANEL_CLIENT_ID: z.string().optional(),
12+
VITE_OPENPANEL_API_URL: z.string().url().optional(),
13+
14+
// For external, use api_url if you don't want to fill it in.
15+
VITE_EXTERNAL_PROD_API_URL: z.string().optional(),
16+
VITE_EXTERNAL_DEV_API_URL: z.string().optional(),
17+
VITE_EXTERNAL_API_URL: z.string().optional(),
18+
VITE_WEB_PROD_URL: z.string().optional(),
19+
VITE_WEB_DEV_URL: z.string().optional(),
3920
}
4021

41-
function getRuntimeEnv() {
42-
try {
43-
if (metaEnvIsEmpty()) {
44-
return process.env
45-
}
46-
return injectExternalEnv(import.meta.env)
47-
} catch {
48-
return process.env
49-
}
50-
}
51-
52-
declare const globalThis: any
53-
function injectExternalEnv<T>(originEnv: T): T {
54-
if (!("document" in globalThis)) {
55-
return originEnv
56-
}
57-
const prefix = "__followEnv"
58-
const env = globalThis[prefix]
59-
if (!env) {
60-
return originEnv
61-
}
62-
63-
for (const key in env) {
64-
originEnv[key as keyof T] = env[key]
65-
}
66-
return originEnv
67-
}
22+
export const isDev = false
23+
export const env = z.object(envSchema).parse({})

plugins/vite/specific-import.ts

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,24 @@ export function createPlatformSpecificImportPlugin(platform: Platform): Plugin {
66
name: "platform-specific-import",
77
enforce: "pre",
88
async resolveId(source, importer) {
9-
if (!importer) {
10-
return null
11-
}
12-
13-
const allowExts = [".js", ".jsx", ".ts", ".tsx"]
14-
15-
if (!allowExts.some((ext) => importer.endsWith(ext))) return null
16-
17-
if (importer.includes("node_modules")) return null
18-
const [path, query] = source.split("?")
19-
20-
if (path.startsWith(".") || path.startsWith("/")) {
21-
let priorities: string[] = []
22-
switch (platform) {
23-
case "electron": {
24-
priorities = [".electron.ts", ".electron.tsx", ".electron.js", ".electron.jsx"]
25-
26-
break
27-
}
28-
case "web": {
29-
priorities = [".web.ts", ".web.tsx", ".web.js", ".web.jsx"]
30-
31-
break
32-
}
33-
case "rn": {
34-
priorities = [".rn.ts", ".rn.tsx", ".rn.js", ".rn.jsx"]
35-
36-
break
37-
}
38-
// No default
39-
}
40-
41-
for (const ext of priorities) {
42-
const resolvedPath = await this.resolve(
43-
`${path}${ext}${query ? `?${query}` : ""}`,
44-
importer,
45-
{
46-
skipSelf: true,
47-
},
48-
)
49-
50-
if (resolvedPath) {
51-
return resolvedPath.id
9+
const resolvedPath = await this.resolve(source, importer, {
10+
skipSelf: true,
11+
})
12+
13+
if (resolvedPath && !resolvedPath.id.includes("node_modules")) {
14+
const lastDotIndex = resolvedPath.id.lastIndexOf(".")
15+
16+
const paths = [
17+
`${resolvedPath.id.slice(0, lastDotIndex)}.${platform}${resolvedPath.id.slice(lastDotIndex)}`,
18+
`${resolvedPath.id.slice(0, lastDotIndex)}.desktop${resolvedPath.id.slice(lastDotIndex)}`,
19+
]
20+
21+
for (const path of paths) {
22+
const resolvedPlatform = await this.resolve(path, importer, {
23+
skipSelf: true,
24+
})
25+
if (resolvedPlatform) {
26+
return resolvedPlatform.id
5227
}
5328
}
5429
}

0 commit comments

Comments
 (0)