Skip to content

Commit

Permalink
Improve loading environment variables based on https://github.com/san…
Browse files Browse the repository at this point in the history
…ity-io/sanity/releases/tag/v3.5.0. and add support for `sanity schema extract`.
  • Loading branch information
daveli committed Jun 21, 2024
1 parent a368144 commit 6e6b9fc
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions sanity.config.ts
Original file line number Diff line number Diff line change
@@ -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
)}`
Expand All @@ -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",
Expand Down

0 comments on commit 6e6b9fc

Please sign in to comment.