Skip to content

Commit

Permalink
Generate ts config before building (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis authored Sep 10, 2024
1 parent e150e20 commit fe0d759
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 89 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-dragons-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'houdini-react': patch
'houdini': patch
---

Fix bug when generating fresh project files with a local schema
2 changes: 1 addition & 1 deletion e2e/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:build": "pnpm build: && pnpm build",
"dev": "vite dev",
"build": "vite build",
"tests": "playwright test",
"tests": "npm run build && playwright test",
"test": "npm run tests",
"tw": "npx tailwindcss -i ./src/styles.css -o ./public/assets/output.css --watch",
"preview": "vite dev"
Expand Down
34 changes: 1 addition & 33 deletions e2e/react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"$houdini": ["./$houdini"],
"$houdini/*": ["./$houdini/*"],
"~": ["./src"],
"~/*": ["./src/*"]
},
"rootDirs": [".", "./$houdini/types"],
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"ambient.d.ts",
"./types/**/$types.d.ts",
"./vite.config.ts",
"./src/**/*.ts",
"./src/**/*.tsx"
],
"exclude": ["../node_modules/**", "./[!ambient.d.ts]**"]
"extends": "./$houdini/tsconfig.json"
}
47 changes: 0 additions & 47 deletions packages/houdini-react/src/plugin/codegen/typeRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,53 +121,6 @@ ${
])
}

export async function writeTsconfig(config: Config) {
await fs.writeFile(
path.join(config.rootDir, 'tsconfig.json'),
JSON.stringify(
{
compilerOptions: {
paths: {
$houdini: ['.'],
'$houdini/*': ['./*'],
'~': ['../src'],
'~/*': ['../src/*'],
},
rootDirs: ['..', './types'],
target: 'ESNext',
useDefineForClassFields: true,
lib: ['DOM', 'DOM.Iterable', 'ESNext'],
allowJs: false,
skipLibCheck: true,
esModuleInterop: false,
allowSyntheticDefaultImports: true,
strict: true,
forceConsistentCasingInFileNames: true,
module: 'ESNext',
moduleResolution: 'Node',
resolveJsonModule: true,
isolatedModules: true,
noEmit: true,
jsx: 'react-jsx',
},
include: [
'ambient.d.ts',
'./types/**/$types.d.ts',
'../vite.config.ts',
'../src/**/*.js',
'../src/**/*.ts',
'../src/**/*.jsx',
'../src/**/*.tsx',
'../src/+app.d.ts',
],
exclude: ['../node_modules/**', './[!ambient.d.ts]**'],
},
null,
4
)
)
}

function paramsType(config: Config, params?: PageManifest['params']): string {
return `{
${Object.entries(params ?? {})
Expand Down
7 changes: 0 additions & 7 deletions packages/houdini-react/src/plugin/vite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {
routerConventions,
} from 'houdini'
import React from 'react'
import ReactDOM from 'react-dom/server'
import { build, ConfigEnv, type BuildOptions, type Connect } from 'vite'

import { manifest, setManifest } from '.'
import { writeTsconfig } from './codegen/typeRoot'

let viteEnv: ConfigEnv
let devServer = false
Expand Down Expand Up @@ -115,10 +113,6 @@ export default {
return id.substring(id.indexOf('virtual:houdini'))
},

async buildStart({ houdiniConfig }) {
await writeTsconfig(houdiniConfig)
},

async closeBundle(config) {
// skip close bundles during dev mode
if (isSecondaryBuild() || viteEnv.mode !== 'production' || devServer) {
Expand Down Expand Up @@ -363,7 +357,6 @@ root.render(React.createElement(App, {
// capture the request before vite's dev server processes it.
async configureServer(server) {
devServer = true
await writeTsconfig(server.houdiniConfig)

server.middlewares.use(async (req, res, next) => {
if (!req.url) {
Expand Down
51 changes: 50 additions & 1 deletion packages/houdini/src/lib/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { StatementKind, TSTypeKind } from 'ast-types/lib/gen/kinds'
import * as graphql from 'graphql'
import * as recast from 'recast'

import { ensureImports, type Config, path } from '.'
import { ensureImports, type Config, path, fs } from '.'
import { unwrapType, TypeWrapper } from './graphql'

const AST = recast.types.builders
Expand Down Expand Up @@ -222,3 +222,52 @@ export function scalarPropertyValue(
}
}
}

export async function writeTsConfig(config: Config) {
await fs.mkdirp(config.rootDir)
await fs.writeFile(
path.join(config.rootDir, 'tsconfig.json'),
JSON.stringify(
{
compilerOptions: {
baseUrl: '.',
paths: {
$houdini: ['.'],
'$houdini/*': ['./*'],
'~': ['../src'],
'~/*': ['../src/*'],
},
rootDirs: ['..', './types'],
target: 'ESNext',
useDefineForClassFields: true,
lib: ['DOM', 'DOM.Iterable', 'ESNext'],
allowJs: false,
skipLibCheck: true,
esModuleInterop: false,
allowSyntheticDefaultImports: true,
strict: true,
forceConsistentCasingInFileNames: true,
module: 'ESNext',
moduleResolution: 'Node',
resolveJsonModule: true,
isolatedModules: true,
noEmit: true,
jsx: 'react-jsx',
},
include: [
'ambient.d.ts',
'./types/**/$types.d.ts',
'../vite.config.ts',
'../src/**/*.js',
'../src/**/*.ts',
'../src/**/*.jsx',
'../src/**/*.tsx',
'../src/+app.d.ts',
],
exclude: ['../node_modules/**', './[!ambient.d.ts]**'],
},
null,
4
)
)
}
5 changes: 5 additions & 0 deletions packages/houdini/src/vite/houdini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
load_manifest,
loadLocalSchema,
isSecondaryBuild,
writeTsConfig,
} from '../lib'

let config: Config
Expand Down Expand Up @@ -152,6 +153,8 @@ export default function Plugin(opts: PluginConfig = {}): VitePlugin {

// when the build starts, we need to make sure to generate
async buildStart(args) {
await writeTsConfig(config)

// check if the adapter has a pre hook
if (config.adapter?.pre && viteEnv.command === 'build' && !isSecondaryBuild()) {
await config.adapter.pre({
Expand Down Expand Up @@ -213,6 +216,8 @@ export default function Plugin(opts: PluginConfig = {}): VitePlugin {
async configureServer(server) {
devServer = true

await writeTsConfig(config)

// if there is a local schema we need to use that when generating
if (config.localSchema) {
config.schema = (await server.ssrLoadModule(config.localSchemaPath))
Expand Down

0 comments on commit fe0d759

Please sign in to comment.