From a7013db7129a1bad67f617c8d17cb5e43f4cf706 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Sun, 30 Oct 2022 17:00:19 -0700 Subject: [PATCH] add module helper --- module.mjs | 20 ++++++++++++++++++++ package.json | 6 +++++- src/client/index.ts | 9 +++------ 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 module.mjs diff --git a/module.mjs b/module.mjs new file mode 100644 index 0000000..32c2c06 --- /dev/null +++ b/module.mjs @@ -0,0 +1,20 @@ +import { defineNuxtModule, extendViteConfig } from '@nuxt/kit' + +export default defineNuxtModule({ + meta: { + name: 'trpc-nuxt', + configKey: 'trpc', + compatibility: { + nuxt: '^3.0.0', + }, + }, + async setup(_moduleOptions, nuxt) { + nuxt.options.build.transpile.push('trpc-nuxt') + + extendViteConfig((config) => { + config.optimizeDeps = config.optimizeDeps || {} + config.optimizeDeps.exclude = config.optimizeDeps.exclude || [] + config.optimizeDeps.include.push('trpc-nuxt/client') + }) + }, +}) diff --git a/package.json b/package.json index 64d75d6..047dd33 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,9 @@ "./client": { "require": "./dist/client/index.cjs", "import": "./dist/client/index.mjs" + }, + "./module": { + "import": "./module.mjs" } }, "main": "./dist/index.mjs", @@ -21,7 +24,8 @@ "types": "./dist/index.d.ts", "files": [ "dist", - "client.d.ts" + "client.d.ts", + "module.mjs" ], "scripts": { "prepublishOnly": "nr build", diff --git a/src/client/index.ts b/src/client/index.ts index 0c87ee3..be9f210 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -5,8 +5,9 @@ import type { } from '@trpc/server' import { createFlatProxy, createRecursiveProxy } from '@trpc/server/shared' import { hash } from 'ohash' -import { useAsyncData, useState } from 'nuxt/app' import type { DecoratedProcedureRecord } from './types' +// @ts-expect-error: Nuxt auto-imports +import { useAsyncData, useState } from '#imports' /** * Calculates the key used for `useAsyncData` call @@ -18,17 +19,13 @@ export function getQueryKey( return input === undefined ? path : `${path}-${hash(input || '')}` } -/** - * @internal - */ -export function createNuxtProxyDecoration(name: string, client: inferRouterProxyClient) { +function createNuxtProxyDecoration(name: string, client: inferRouterProxyClient) { return createRecursiveProxy((opts) => { const args = opts.args const pathCopy = [name, ...opts.path] // The last arg is for instance `.mutate` or `.query()` - const lastArg = pathCopy.pop()! const path = pathCopy.join('.')