diff --git a/apps/mobile/metro.config.js b/apps/mobile/metro.config.js index bcab80946..d9c99b688 100644 --- a/apps/mobile/metro.config.js +++ b/apps/mobile/metro.config.js @@ -18,6 +18,24 @@ config.resolver.nodeModulesPaths = [ path.resolve(__dirname, "../../node_modules"), ] +config.resolver.resolveRequest = (context, moduleName, platform) => { + const result = context.resolveRequest(context, moduleName, platform) + if (result.type === "sourceFile") { + const lastDotIndex = result.filePath.lastIndexOf(".") + const mobilePath = `${result.filePath.slice(0, lastDotIndex)}.mobile${result.filePath.slice(lastDotIndex)}` + const file = context.fileSystemLookup(mobilePath) + if (file.exists) { + return { + ...result, + filePath: mobilePath, + } + } else { + return result + } + } + return result +} + module.exports = wrapWithReanimatedMetroConfig( withNativeWind(config, { input: "./src/global.css" }), ) diff --git a/apps/mobile/src/lib/image.ts b/apps/mobile/src/lib/image.ts index 4b6edc4b5..8e8841c96 100644 --- a/apps/mobile/src/lib/image.ts +++ b/apps/mobile/src/lib/image.ts @@ -1,26 +1,4 @@ -const imageRefererMatches = [ - { - url: /^https:\/\/\w+\.sinaimg.cn/, - referer: "https://weibo.com", - }, - { - url: /^https:\/\/i\.pximg\.net/, - referer: "https://www.pixiv.net", - }, - { - url: /^https:\/\/cdnfile\.sspai\.com/, - referer: "https://sspai.com", - }, - { - url: /^https:\/\/(?:\w|-)+\.cdninstagram\.com/, - referer: "https://www.instagram.com", - }, - { - url: /^https:\/\/sp1\.piokok\.com/, - referer: "https://www.piokok.com", - force: true, - }, -] +import { imageRefererMatches } from "@follow/shared/src/image" const isValidUrl = (url: string) => { try { diff --git a/packages/shared/src/constants/env.mobile.ts b/packages/shared/src/constants/env.mobile.ts new file mode 100644 index 000000000..f4fff1ac4 --- /dev/null +++ b/packages/shared/src/constants/env.mobile.ts @@ -0,0 +1 @@ +export const env = {} diff --git a/packages/shared/src/constants/env.ts b/packages/shared/src/constants/env.ts new file mode 100644 index 000000000..9029ce303 --- /dev/null +++ b/packages/shared/src/constants/env.ts @@ -0,0 +1,67 @@ +import { createEnv } from "@t3-oss/env-core" +import { z } from "zod" + +export const isDev = + "process" in globalThis ? process.env.NODE_ENV === "development" : import.meta.env.DEV +export const env = createEnv({ + clientPrefix: "VITE_", + client: { + VITE_WEB_URL: z.string().url().default("https://app.follow.is"), + VITE_API_URL: z.string(), + VITE_DEV_PROXY: z.string().optional(), + VITE_SENTRY_DSN: z.string().optional(), + VITE_INBOXES_EMAIL: z.string().default("@follow.re"), + VITE_FIREBASE_CONFIG: z.string().optional(), + + VITE_OPENPANEL_CLIENT_ID: z.string().optional(), + VITE_OPENPANEL_API_URL: z.string().url().optional(), + + // For external, use api_url if you don't want to fill it in. + VITE_EXTERNAL_PROD_API_URL: z.string().optional(), + VITE_EXTERNAL_DEV_API_URL: z.string().optional(), + VITE_EXTERNAL_API_URL: z.string().optional(), + VITE_WEB_PROD_URL: z.string().optional(), + VITE_WEB_DEV_URL: z.string().optional(), + }, + + emptyStringAsUndefined: true, + runtimeEnv: getRuntimeEnv() as any, + + skipValidation: "process" in globalThis ? process.env.VITEST === "true" : false, +}) + +function metaEnvIsEmpty() { + try { + return Object.keys(import.meta.env || {}).length === 0 + } catch { + return true + } +} + +function getRuntimeEnv() { + try { + if (metaEnvIsEmpty()) { + return process.env + } + return injectExternalEnv(import.meta.env) + } catch { + return process.env + } +} + +declare const globalThis: any +function injectExternalEnv(originEnv: T): T { + if (!("document" in globalThis)) { + return originEnv + } + const prefix = "__followEnv" + const env = globalThis[prefix] + if (!env) { + return originEnv + } + + for (const key in env) { + originEnv[key as keyof T] = env[key] + } + return originEnv +} diff --git a/packages/shared/src/image.ts b/packages/shared/src/image.ts index a8635059d..60ab5862e 100644 --- a/packages/shared/src/image.ts +++ b/packages/shared/src/image.ts @@ -1,4 +1,4 @@ -import { env } from "@follow/shared/env" +import { env } from "./constants/env" export const IMAGE_PROXY_URL = "https://webp.follow.is" @@ -8,7 +8,7 @@ export const selfRefererMatches = [env.VITE_OPENPANEL_API_URL, IMAGE_PROXY_URL]. export const imageRefererMatches = [ { - url: /^https:\/\/\w+\.sinaimg.cn/, + url: /^https:\/\/\w+\.sinaimg\.cn/, referer: "https://weibo.com", }, {