sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.24k stars 425 forks source link

Sanity graphql cli commands ignore vite config defined in sanity.cli.ts #4022

Open hyldmo opened 1 year ago

hyldmo commented 1 year ago

Describe the bug

Similar to how webpack config is ignored in v2 (see #3092), in v3 sanity graphql deploy now instead ignores the vite config, and as such I am not able to use import aliases (and probably other features too) if I want to deploy the graphQL API.

Here's the error message I'm currently getting:

Error: Failed to load configuration file "[...]/studio/sanity.config.ts":
Cannot find module '~/lib'

Here's my sanity.cli.ts:

import path from 'path'
import { defineCliConfig } from 'sanity/cli'

export default defineCliConfig({
    api: {
        projectId: '...',
        dataset: 'production'
    },
    vite(config: any) {
        config.resolve.alias = {
            ...config.resolve.alias,
            '~/lib': path.resolve(__dirname, './lib'),
            '~/types': path.resolve(__dirname, './types')
        }
        return config
    }
})

Everything works fine while running sanity dev, sanity build, and sanity deploy

To Reproduce

Steps to reproduce the behavior:

Create project and use alias imports.

Expected behavior

A successful deploy without import errors

Which versions of Sanity are you using?

@sanity/cli (global)   3.1.2 (up to date)
@sanity/cli           2.35.3 (latest: 3.1.2)
@sanity/dashboard      3.1.1 (up to date)
@sanity/table          1.0.1 (up to date)
@sanity/vision         3.0.0 (latest: 3.1.2)
sanity                 3.1.2 (up to date)

What operating system are you using?

MacOS 13.0.1

Which versions of Node.js / npm are you running?

Run npm -v && node -v in the terminal and copy-paste the result here.

8.15.0
v16.17.1
Rhym commented 1 year ago

sanity graphql deploy also can't get the environment variables from vite e.g: import.meta.env.SANITY_STUDIO_API_PROJECT_ID which is fine in sanity deploy but not in sanity graphql deploy .

rexxars commented 1 year ago

sanity graphql deploy also can't get the environment variables from vite e.g: import.meta.env.SANITY_STUDIO_API_PROJECT_ID which is fine in sanity deploy but not in sanity graphql deploy .

As of v3.2.4 you should be able to use environment variables through process.env for CLI configuration and such. Other custom vite configurations like aliases and similar are still not available, unfortunately.

Rhym commented 1 year ago

@rexxars excellent, thank you.

simenandre commented 1 year ago

It might be unrelated, but I'm seeing the same issues with running "type": "module" (pure ESM), where sanity graphql deploy is not recognizing ESM imports.

I'm using Sanity v3 here; I'm seeing this issue in both of these repositories:

echocrow commented 1 year ago

adding to this: the same issue seems to be present in sanity exec. the vite config is ignored; consequently, path aliases are undefined, resulting in aliased imports (like ~/lib in OP) not resolving.

fostimus commented 1 year ago

Is there any roadmap for this bugfix? We also have import aliases set up, and it's a huge pain to undo them only for sanity graphql deploy

Rhym commented 1 year ago

A bit off-topic, but I have recently swapped over all of my GraphQL queries instead to use https://github.com/FormidableLabs/groqd as I was just having too many issues with GraphQL and complex portable text types. Has been working well thus far.