-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Component query variable types (#610)
* fix sigint in init * add component gen * failing tests * group files * generate type files for components * remove test * changeset * let users overwrite prop type * update typescript guide
- Loading branch information
1 parent
c1363fe
commit 3168f7d
Showing
14 changed files
with
11,119 additions
and
7,895 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'houdini-svelte': patch | ||
--- | ||
|
||
Generate variable function definitions for non-route queries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/houdini-svelte/src/plugin/codegen/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { ArtifactKind, Config, fs, GenerateHookInput } from 'houdini' | ||
import path from 'path' | ||
import * as recast from 'recast' | ||
|
||
import { parseSvelte } from '../../extract' | ||
import { Framework } from '../../kit' | ||
|
||
type ExportNamedDeclaration = recast.types.namedTypes.ExportNamedDeclaration | ||
type VariableDeclaration = recast.types.namedTypes.VariableDeclaration | ||
|
||
export default async function componentTypesGenerator( | ||
framework: Framework, | ||
{ config, documents }: GenerateHookInput | ||
) { | ||
// if we treat the documents as the source of truth for files that match | ||
// we can just filter out the ones that don't apply:t | ||
// - in kit, exclude the route directory | ||
// - group the files by directory | ||
// - generate ./$houdini in the typeroot directory at the correct spot | ||
|
||
// there could be many queries in a given component so we can't just think about filepaths | ||
const queries: Record<string, { name: string; query: string }[]> = {} | ||
for (const document of documents) { | ||
if (document.kind !== ArtifactKind.Query) { | ||
continue | ||
} | ||
|
||
queries[document.filename] = (queries[document.filename] ?? []).concat({ | ||
name: document.name, | ||
query: document.originalString, | ||
}) | ||
} | ||
let matches = Object.keys(queries).filter((filepath) => filepath.endsWith('.svelte')) | ||
|
||
// if we are in kit, don't consider the source directory | ||
if (framework === 'kit') { | ||
matches = matches.filter((match) => !match.startsWith(config.routesDir)) | ||
} | ||
|
||
// group the files by directory | ||
const files: ProjectDirs = { | ||
dirs: {}, | ||
files: [], | ||
} | ||
|
||
// put every file we found in the right place | ||
for (let file of matches) { | ||
// only worry about things relative to the project root | ||
file = path.relative(config.projectRoot, file) | ||
|
||
// walk down the path | ||
let target = files | ||
const parts = file.split('/') | ||
for (const [i, part] of parts.entries()) { | ||
// if we are at the end of the path, we are looking at a file | ||
if (i === parts.length - 1) { | ||
target.files.push(part) | ||
continue | ||
} | ||
|
||
// we are on a file | ||
if (!target.dirs[part]) { | ||
target.dirs[part] = { | ||
dirs: {}, | ||
files: [], | ||
} | ||
} | ||
|
||
// there is guaranteed to be an entry for this particular filepath part | ||
// focus on it and move onto the next one | ||
target = target.dirs[part] | ||
} | ||
} | ||
|
||
// now that we've grouped together all of the files together, we can just walk down the | ||
// structure and generate the necessary types at the right place. | ||
await walk_project(config, files, queries, config.projectRoot) | ||
} | ||
|
||
async function walk_project( | ||
config: Config, | ||
dirs: ProjectDirs, | ||
queries: Record<string, { name: string; query: string }[]>, | ||
root: string | ||
) { | ||
// process every child directory | ||
await Promise.all( | ||
Object.entries(dirs.dirs).map(async ([path_part, child]) => { | ||
// keep going with the new root | ||
return walk_project(config, child, queries, path.join(root, path_part)) | ||
}) | ||
) | ||
|
||
// if we don't have any files at this spot we're done | ||
if (dirs.files.length === 0) { | ||
return | ||
} | ||
|
||
// every query in this directory needs an entry in the file | ||
let typeFile = "import type { ComponentProps } from 'svelte'" | ||
for (const file of dirs.files) { | ||
const no_ext = path.parse(file).name | ||
const prop_type = no_ext + 'Props' | ||
|
||
// figure out the full file path | ||
const filepath = path.join(root, file) | ||
|
||
// we need to figure out the props for this component | ||
const contents = await fs.readFile(filepath) | ||
// make typescript happy | ||
if (!contents) { | ||
continue | ||
} | ||
|
||
// define the prop types for the component | ||
typeFile = | ||
` | ||
import ${no_ext} from './${file}' | ||
` + | ||
typeFile + | ||
` | ||
type ${prop_type} = ComponentProps<${no_ext}> | ||
` | ||
|
||
// a file can contain multiple queries | ||
for (const query of queries[filepath]) { | ||
// we can't generate actual type defs for props so let's just export a | ||
// generic typedefinition | ||
typeFile = | ||
` | ||
import type { ${query.name}$input } from '${path | ||
.relative(filepath, path.join(config.artifactDirectory, query.name)) | ||
.replace('/$houdini', '')}' | ||
` + | ||
typeFile + | ||
` | ||
export type ${config.variableFunctionName( | ||
query.name | ||
)} = <_Props = ${prop_type}>(args: { props: _Props }) => FragmentQueryVars$input | ||
` | ||
} | ||
} | ||
|
||
// we need to write this file in the correct location in the type root dir | ||
const relative = path.join(config.typeRootDir, path.relative(config.projectRoot, root)) | ||
|
||
// write the file | ||
await fs.mkdirp(relative) | ||
await fs.writeFile(path.join(relative, '$houdini.d.ts'), typeFile) | ||
} | ||
|
||
type ProjectDirs = { | ||
dirs: Record<string, ProjectDirs> | ||
files: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
3168f7d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs-next – ./
docs-next-git-main-houdinigraphql.vercel.app
docs-next-kohl.vercel.app
docs-next-houdinigraphql.vercel.app