Skip to content

Commit

Permalink
fix: remove file extensions from added aliases (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
dulnan authored Nov 20, 2024
1 parent fa53530 commit 5028dba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export default defineNuxtConfig({
autoImport: false,
},

typescript: {
typeCheck: 'build',
},

future: {
compatibilityVersion: 4,
},
Expand Down
37 changes: 26 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from 'url'
import type { Types } from '@graphql-codegen/plugin-helpers'
import { type SchemaASTConfig } from '@graphql-codegen/schema-ast'
import { relative, resolve } from 'pathe'
import { relative, resolve, parse as pathParse, join as pathJoin } from 'pathe'
import { defu } from 'defu'
import { type BirpcGroup } from 'birpc'
import {
Expand Down Expand Up @@ -35,6 +35,11 @@ import { type ClientFunctions, type ServerFunctions } from './rpc-types'
import { type GraphqlMiddlewareDocument } from './types'
export type { GraphqlMiddlewareServerOptions } from './types'

function pathWithoutExtension(fullPath: string): string {
const parsed = pathParse(fullPath)
return pathJoin(parsed.dir, parsed.name)
}

export interface ModuleOptions {
/**
* File glob patterns for the auto import feature.
Expand Down Expand Up @@ -461,16 +466,20 @@ export default defineNuxtModule<ModuleOptions>({
})

if (result.dst.includes(GraphqlMiddlewareTemplate.Documents)) {
nuxt.options.alias['#graphql-documents'] = result.dst
nuxt.options.alias['#graphql-documents'] = pathWithoutExtension(
result.dst,
)
} else if (
result.dst.includes(GraphqlMiddlewareTemplate.OperationTypes)
) {
nuxt.options.alias['#graphql-operations'] = result.dst
nuxt.options.alias['#graphql-operations'] = pathWithoutExtension(
result.dst,
)
} else if (
result.dst.includes(GraphqlMiddlewareTemplate.ComposableContext)
) {
nuxt.options.alias['#nuxt-graphql-middleware/generated-types'] =
result.dst
pathWithoutExtension(result.dst)
}
})

Expand Down Expand Up @@ -563,7 +572,7 @@ export { serverOptions }
return `
import type { GraphqlMiddlewareServerOptions } from '${moduleTypesPath}'
${serverOptionsLineTypes}
import type { GraphqlServerResponse } from '#graphql-middleware/types'
import type { GraphqlServerResponse } from '${runtimeTypesPath}'
import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/generated-types'
type GraphqlResponseAdditions =
Expand Down Expand Up @@ -605,20 +614,25 @@ export { clientOptions }`
},
})

const runtimeTypesPath = relative(
nuxt.options.buildDir,
moduleResolver.resolve('./runtime/types.ts'),
)

addTemplate({
filename: 'graphqlMiddleware.clientOptions.d.ts',
write: true,
getContents: () => {
if (clientOptionsImport) {
return `import { GraphqlClientOptions } from '#graphql-middleware/types'
return `import type { GraphqlClientOptions } from '${runtimeTypesPath}'
${clientOptionsImport}
export type GraphqlClientContext = typeof clientOptions extends GraphqlClientOptions<infer R> ? R : {}
export { clientOptions }`
}

return `import { GraphqlClientOptions } from '#graphql-middleware/types'
return `import type { GraphqlClientOptions } from '${runtimeTypesPath}'
export const clientOptions: GraphqlClientOptions
export type GraphqlClientContext = {}
Expand All @@ -627,17 +641,18 @@ export type GraphqlClientContext = {}
})

nuxt.options.alias['#graphql-middleware-client-options'] =
clientOptionsTemplate.dst
pathWithoutExtension(clientOptionsTemplate.dst)

nuxt.options.nitro.externals = nuxt.options.nitro.externals || {}
nuxt.options.nitro.externals.inline =
nuxt.options.nitro.externals.inline || []
nuxt.options.nitro.externals.inline.push(template.dst)
nuxt.options.alias['#graphql-middleware-server-options-build'] =
template.dst
pathWithoutExtension(template.dst)

nuxt.options.alias['#graphql-middleware/types'] =
moduleResolver.resolve('./runtime/types')
nuxt.options.alias['#graphql-middleware/types'] = pathWithoutExtension(
moduleResolver.resolve('./runtime/types.ts'),
)

// Add the server API handler.
addServerHandler({
Expand Down

0 comments on commit 5028dba

Please sign in to comment.