From 9d3b4d6100fc2356c0bd421826b2f6b887d92d19 Mon Sep 17 00:00:00 2001 From: Dave Li Date: Fri, 21 Jun 2024 13:23:03 +0200 Subject: [PATCH] Improve loading environment variables based on https://github.com/sanity-io/sanity/releases/tag/v3.5.0. and add support for `sanity schema extract`. --- sanity.config.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sanity.config.ts b/sanity.config.ts index d4b3ec3..5e7e3c9 100644 --- a/sanity.config.ts +++ b/sanity.config.ts @@ -1,19 +1,18 @@ +const env = process.env ?? import.meta.env; + // Different environments use different variables const projectId = - import.meta.env.PUBLIC_SANITY_STUDIO_PROJECT_ID! || - import.meta.env.PUBLIC_SANITY_PROJECT_ID!; -const dataset = - import.meta.env.PUBLIC_SANITY_STUDIO_DATASET! || - import.meta.env.PUBLIC_SANITY_DATASET!; + env.PUBLIC_SANITY_STUDIO_PROJECT_ID! || env.PUBLIC_SANITY_PROJECT_ID!; +const dataset = env.PUBLIC_SANITY_STUDIO_DATASET! || env.PUBLIC_SANITY_DATASET!; const previewUrl = - import.meta.env.PUBLIC_SANITY_STUDIO_PREVIEW_URL || "http://localhost:4321"; + env.PUBLIC_SANITY_STUDIO_PREVIEW_URL! || "http://localhost:4321"; // Feel free to remove this check if you don't need it if (!projectId || !dataset || !previewUrl) { throw new Error( `Missing environment variable(s). Check if named correctly in .env file.\n\nShould be:\nPUBLIC_SANITY_STUDIO_PROJECT_ID=${projectId}\nPUBLIC_SANITY_STUDIO_DATASET=${dataset}\nPUBLIC_SANITY_STUDIO_PREVIEW_URL=${previewUrl}\n\nAvailable environment variables:\n${JSON.stringify( - import.meta.env, + env, null, 2 )}` @@ -24,7 +23,9 @@ import { defineConfig } from "sanity"; import { structureTool } from "sanity/structure"; import { presentationTool } from "sanity/presentation"; import { visionTool } from "@sanity/vision"; -import { schemaTypes } from "./schema"; + +// We have to import the full path since `sanity schema extract` produces `schema.json` and then that file will be loaded, breaking the application. +import { schemaTypes } from "./schema/index.ts"; export default defineConfig({ name: "project-name",