mirror of
https://github.com/ArthurDanjou/trpc-nuxt.git
synced 2026-01-27 10:20:39 +01:00
fix: build error
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
"prepublishOnly": "nr build",
|
"prepublishOnly": "nr build",
|
||||||
"build": "nuxt-module-build",
|
"build": "nuxt-module-build",
|
||||||
"play": "nr build && nuxi dev playground",
|
"play": "nr build && nuxi dev playground",
|
||||||
|
"build:playground": "nuxi build playground",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
"lint:fix": "eslint . --fix",
|
"lint:fix": "eslint . --fix",
|
||||||
"release": "bumpp --commit --push --tag && pnpm publish",
|
"release": "bumpp --commit --push --tag && pnpm publish",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { defineNuxtConfig } from 'nuxt'
|
import { defineNuxtConfig } from 'nuxt'
|
||||||
import Module from '..'
|
import Module from '../src/module'
|
||||||
|
|
||||||
// https://v3.nuxtjs.org/api/configuration/nuxt.config
|
// https://v3.nuxtjs.org/api/configuration/nuxt.config
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import { dirname, join } from 'pathe'
|
import { join } from 'pathe'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
|
|
||||||
import { addServerHandler, defineNuxtModule } from '@nuxt/kit'
|
import { addServerHandler, addTemplate, defineNuxtModule } from '@nuxt/kit'
|
||||||
import fs from 'fs-extra'
|
|
||||||
|
|
||||||
export interface ModuleOptions {
|
export interface ModuleOptions {
|
||||||
baseURL: string
|
baseURL: string
|
||||||
@@ -21,10 +20,10 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
},
|
},
|
||||||
async setup(options, nuxt) {
|
async setup(options, nuxt) {
|
||||||
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
|
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
|
||||||
nuxt.options.build.transpile.push(runtimeDir)
|
nuxt.options.build.transpile.push(runtimeDir, '#build/trpc-client', '#build/trpc-handler')
|
||||||
|
|
||||||
const clientPath = join(nuxt.options.buildDir, 'trpc-client.ts')
|
|
||||||
const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
|
const handlerPath = join(nuxt.options.buildDir, 'trpc-handler.ts')
|
||||||
|
nuxt.options.build.transpile.push(handlerPath)
|
||||||
|
|
||||||
// Final resolved configuration
|
// Final resolved configuration
|
||||||
const finalConfig = nuxt.options.runtimeConfig.public.trpc = defu(nuxt.options.runtimeConfig.public.trpc, {
|
const finalConfig = nuxt.options.runtimeConfig.public.trpc = defu(nuxt.options.runtimeConfig.public.trpc, {
|
||||||
@@ -32,43 +31,53 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
trpcURL: options.trpcURL,
|
trpcURL: options.trpcURL,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
nuxt.hook('autoImports:extend', (imports) => {
|
||||||
|
imports.push(
|
||||||
|
{ name: 'useClient', from: '#build/trpc-client' },
|
||||||
|
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
addServerHandler({
|
addServerHandler({
|
||||||
route: `${finalConfig.trpcURL}/*`,
|
route: `${finalConfig.trpcURL}/*`,
|
||||||
handler: handlerPath,
|
handler: handlerPath,
|
||||||
})
|
})
|
||||||
|
|
||||||
nuxt.hook('autoImports:extend', (imports) => {
|
addTemplate({
|
||||||
imports.push(
|
filename: 'trpc-client.ts',
|
||||||
{ name: 'useClient', from: clientPath },
|
write: true,
|
||||||
{ name: 'useAsyncQuery', from: join(runtimeDir, 'client') },
|
getContents() {
|
||||||
)
|
return `
|
||||||
|
import * as trpc from '@trpc/client'
|
||||||
|
import type { router } from '~/server/trpc'
|
||||||
|
|
||||||
|
const client = trpc.createTRPCClient<typeof router>({
|
||||||
|
url: '${finalConfig.baseURL}${finalConfig.trpcURL}',
|
||||||
|
})
|
||||||
|
|
||||||
|
export const useClient = () => client
|
||||||
|
`
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await fs.ensureDir(dirname(clientPath))
|
addTemplate({
|
||||||
|
filename: 'trpc-handler.ts',
|
||||||
await fs.writeFile(clientPath, `
|
write: true,
|
||||||
import * as trpc from '@trpc/client'
|
getContents() {
|
||||||
import type { router } from '~/server/trpc'
|
return `
|
||||||
|
import { createTRPCHandler } from 'trpc-nuxt/api'
|
||||||
const client = trpc.createTRPCClient<typeof router>({
|
import { useRuntimeConfig } from '#imports'
|
||||||
url: '${finalConfig.baseURL}${finalConfig.trpcURL}',
|
import * as functions from '~/server/trpc'
|
||||||
})
|
|
||||||
|
|
||||||
export const useClient = () => client
|
const { trpc: { trpcURL } } = useRuntimeConfig().public
|
||||||
`)
|
|
||||||
|
export default createTRPCHandler({
|
||||||
await fs.writeFile(handlerPath, `
|
...functions,
|
||||||
import { createTRPCHandler } from 'trpc-nuxt/api'
|
trpcURL
|
||||||
import { useRuntimeConfig } from '#imports'
|
})
|
||||||
import * as functions from '~/server/trpc'
|
`
|
||||||
|
},
|
||||||
const { trpc: { trpcURL } } = useRuntimeConfig().public
|
})
|
||||||
|
|
||||||
export default createTRPCHandler({
|
|
||||||
...functions,
|
|
||||||
trpcURL
|
|
||||||
})
|
|
||||||
`)
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import type { ProcedureRecord, inferHandlerInput, inferProcedureInput, inferProc
|
|||||||
import type { TRPCClientErrorLike } from '@trpc/client'
|
import type { TRPCClientErrorLike } from '@trpc/client'
|
||||||
import { objectHash } from 'ohash'
|
import { objectHash } from 'ohash'
|
||||||
// @ts-expect-error: Resolved by Nuxt
|
// @ts-expect-error: Resolved by Nuxt
|
||||||
import { useAsyncData, useState } from '#imports'
|
import { useAsyncData, useState } from '#app'
|
||||||
// @ts-expect-error: Resolved by Nuxt
|
// @ts-expect-error: Resolved by Nuxt
|
||||||
import { useClient } from '#build/trpc-client'
|
import { useClient } from '#build/trpc-client'
|
||||||
// @ts-expect-error: Resolved by Nuxt
|
// @ts-expect-error: Resolved by Nuxt
|
||||||
|
|||||||
Reference in New Issue
Block a user