Skip to content

Commit

Permalink
fix(codegen): will not try to read removed file
Browse files Browse the repository at this point in the history
  • Loading branch information
Teages committed Apr 4, 2024
1 parent 37f66bc commit bb2a478
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
14 changes: 3 additions & 11 deletions src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@ export interface UrqlModuleOptions {
*/
watch?: false | {
/**
* @description Regenerate code when the file changed.
* @experimental need more test
* @description Regenerate when the file changed. (The file must be watched by Nuxt)
*/
extra?: string[]

/**
* @description Regenerate code when Nitro server HMR.
* @description Regenerate when Nitro server HMR.
* @default false
* @experimental need more test
*/
nitro?: boolean | {
/**
* @description Reload schema when the Nitro server HMR.
* @experimental need more test
*/
reloadSchema?: string[]
}
nitro?: boolean
}
})
}
Expand Down
60 changes: 34 additions & 26 deletions src/setup/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,11 @@ export async function setupCodegen(

const extra = (watchConfig.extra ?? [])
.map(doc => rootResolver.resolve(doc))
const nitroWatch = !!watchConfig.nitro
const reloadSchema = typeof watchConfig.nitro === 'object'
? watchConfig.nitro.reloadSchema ?? []
: []
const nitroWatch = watchConfig.nitro ?? false

let lock: string | undefined
nuxt.hook('builder:watch', async (_event, path) => {
nuxt.hook('builder:watch', async (event, path) => {
const resolvedPath = normalize(rootResolver.resolve(path))
const isServer = resolvedPath.startsWith(normalize(rootResolver.resolve(nuxt.options.serverDir)))

const clients = await Promise.all(getClients.map(getClient => getClient()))
const graphqlTags = clients.map(client => client.pluckConfig.globalGqlIdentifierName).flat()
Expand All @@ -217,12 +213,15 @@ export async function setupCodegen(
.flatMap(doc => glob.sync(doc, { absolute: true }))
.map(file => normalize(file))

const changedFile = await fs.readFile(resolvedPath, 'utf-8')
if (
!(extraFiles.includes(resolvedPath) || graphqlTags.some(tag => changedFile.includes(tag)))
&& !(isServer && nitroWatch)
) {
logger.info('GraphQL codegen skipped')
const changedFile = ['add', 'change'].includes(event)
? await fs.readFile(resolvedPath, 'utf-8')
: ''

if (!(
// the file is in the extra list
extraFiles.includes(resolvedPath)
|| graphqlTags.some(tag => changedFile.includes(tag))
)) {
return
}

Expand All @@ -231,17 +230,6 @@ export async function setupCodegen(
return
}

if (isServer && nitroWatch) {
reloadSchema.forEach(async (clientId) => {
const client = options.clients[clientId]
if (!client) {
return
}
const url = client.url
schemaCache.delete(url)
})
}

logger.start(`GraphQL codegen: ${path}`)
lock = fileHash
await codegen()
Expand All @@ -250,14 +238,34 @@ export async function setupCodegen(
})
lock = undefined
})

if (nitroWatch) {
const nitroLock = 'Nitro HMR'
nuxt.hook('nitro:build:before', (nitro) => {
nitro.hooks.hook('dev:reload', async () => {
if (lock === nitroLock) {
return
}
logger.start(`GraphQL codegen: Nitro HMR`)
lock = nitroLock

await codegen()
await updateTemplates({
filter: ({ filename }) => filename.startsWith('urql-client/codegen/'),
})

lock = undefined
})
})
}
}

return autoImportList
}

function getDefaultDocuments() {
const exts = [
// js / ts files
// js / ts files
['js', 'ts', 'jsx', 'tsx', 'mjs', 'cjs', 'mts', 'cts'],

// vue files
Expand All @@ -278,7 +286,7 @@ function getDefaultDocuments() {
]

return [
`{${dirs.join(',')}}/**/*.{${exts.join(',')}}`,
`*.{${exts.join(',')}}`,
`{${dirs.join(',')}}/**/*.{${exts.join(',')}}`,
`*.{${exts.join(',')}}`,
]
}

0 comments on commit bb2a478

Please sign in to comment.