Skip to content

Commit 10e89f3

Browse files
sgulsethbinoy14
authored andcommitted
feat(typegen): add support for astro (#8098)
1 parent 3eea18c commit 10e89f3

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,18 @@ describe('findQueriesInPath', () => {
4040
assert(result.value.type === 'error') // workaround for TS
4141
expect(result.value.error.message).toMatch(/Duplicate query name found:/)
4242
})
43+
44+
test('can find and handle .astro files', async () => {
45+
const stream = findQueriesInPath({
46+
path: [path.join('**', 'typescript', '__tests__', 'fixtures', '*.astro')],
47+
})
48+
const res = []
49+
for await (const result of stream) {
50+
res.push(result)
51+
}
52+
expect(res.length).toBe(1)
53+
expect(res[0].type).toBe('queries')
54+
assert(res[0].type === 'queries') // workaround for TS
55+
expect(res[0].queries.length).toBe(1)
56+
})
4357
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
import groq from 'groq'
3+
import { type Client } from '@sanity/client'
4+
5+
export const query = groq`*[_type == "myType"]`
6+
---
7+
<MyComponent>
8+
<div>{query}</div>
9+
</MyComponent>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {describe, expect, test} from 'vitest'
2+
3+
import {parseSourceFile} from '../parseSource'
4+
5+
describe('parseSource', () => {
6+
test('should parse astro', () => {
7+
const source = `
8+
---
9+
import Layout from '../layouts/Layout.astro';
10+
import { project_dir } from '../libs/utils';
11+
12+
13+
const proj = "10_prerender"
14+
const render_time = new Date()
15+
export const prerender = true
16+
---
17+
18+
<Layout title="Prerendered">
19+
<main>
20+
<h1>Prerendered</h1>
21+
<p>This page was prerendered at {render_time.toISOString()}</p>
22+
</main>
23+
</Layout>
24+
`
25+
26+
const parsed = parseSourceFile(source, 'foo.astro', {})
27+
28+
expect(parsed.type).toBe('File')
29+
expect(parsed.program.body.length).toBe(5)
30+
})
31+
})

packages/@sanity/codegen/src/typescript/parseSource.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ import type * as babelTypes from '@babel/types'
33

44
// helper function to parse a source file
55
export function parseSourceFile(
6-
source: string,
7-
filename: string,
6+
_source: string,
7+
_filename: string,
88
babelOptions: TransformOptions,
99
): babelTypes.File {
10+
let source = _source
11+
let filename = _filename
12+
if (filename.endsWith('.astro')) {
13+
// append .ts to the filename so babel will parse it as typescript
14+
filename += '.ts'
15+
source = parseAstro(source)
16+
}
1017
const result = parse(source, {
1118
...babelOptions,
1219
filename,
@@ -18,3 +25,17 @@ export function parseSourceFile(
1825

1926
return result
2027
}
28+
29+
function parseAstro(source: string): string {
30+
// find all code fences, the js code is between --- and ---
31+
const codeFences = source.match(/---\n([\s\S]*?)\n---/g)
32+
if (!codeFences) {
33+
return ''
34+
}
35+
36+
return codeFences
37+
.map((codeFence) => {
38+
return codeFence.split('\n').slice(1, -1).join('\n')
39+
})
40+
.join('\n')
41+
}

0 commit comments

Comments
 (0)