sanity-io / sanity

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

Type generation incompatible with Astro #6965

Open ivyrze opened 3 months ago

ivyrze commented 3 months ago

Describe the bug

I'm trying to use GROQ typegen in my Astro project, but it doesn't recognize the JSX-in-frontmatter syntax and mistakenly thinks its all errors, for example:

src/pages/about.astro: `import` can only be used in `import()` or `import.meta`. (2:0)

  1 | ---
> 2 | import Layout from '../layouts/BlogPost.astro';

Using the same code in a standard JSX format produces expected results.

To Reproduce

Steps to reproduce the behavior:

  1. Follow Astro + Sanity getting started guide.
  2. Create sanity-typegen.json to include .astro files in the path search glob.
  3. Run sanity schema extract && sanity typegen generate.

Expected behavior

The types should generate without error.

Which versions of Sanity are you using?

Run sanity versions in the terminal and copy-paste the result here.

@sanity/cli (global)  3.46.1 (up to date)
@sanity/astro          3.1.3 (up to date)
@sanity/image-url      1.0.2 (up to date)
@sanity/types         3.46.1 (up to date)
@sanity/vision        3.46.1 (up to date)
sanity                3.46.1 (up to date)

What operating system are you using?

macOS 14.5

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

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

10.2.4
v18.19.1
AdamGEmerson commented 1 month ago

Any ideas where to start on this one? I'd like to see this working and am happy to help where I can.

sgulseth commented 1 month ago

Hi! Sorry for this taking some time. The challenge here is that we are using babel to parse source files, but astro isn't supported by babel or any plugin afaik. I think, without having looked very much into it yet, we would have to intercept .astro files and parse them with the astro-compiler. Then we should be able to have a typescript version which we could use to pick up the queries.