From c83367bd480368ec7f8a205c4063ef0d74126475 Mon Sep 17 00:00:00 2001 From: Naily Date: Thu, 7 Nov 2024 19:55:48 +0800 Subject: [PATCH] docs(changeset): feat(unplugin-rpc): add rpc factory function to load module --- .changeset/flat-seals-hide.md | 5 ++++ packages/unplugin-rpc/src/core/build.ts | 26 +++++++++++++++++++ packages/unplugin-rpc/src/core/factory.ts | 7 +++++ .../src/core/vite-server-adapter.ts | 18 ++++++++++--- packages/unplugin-rpc/src/index.ts | 26 +++---------------- 5 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 .changeset/flat-seals-hide.md create mode 100644 packages/unplugin-rpc/src/core/build.ts create mode 100644 packages/unplugin-rpc/src/core/factory.ts diff --git a/.changeset/flat-seals-hide.md b/.changeset/flat-seals-hide.md new file mode 100644 index 0000000..1ec965e --- /dev/null +++ b/.changeset/flat-seals-hide.md @@ -0,0 +1,5 @@ +--- +"unplugin-rpc": patch +--- + +feat(unplugin-rpc): add rpc factory function to load module diff --git a/packages/unplugin-rpc/src/core/build.ts b/packages/unplugin-rpc/src/core/build.ts new file mode 100644 index 0000000..1cb7766 --- /dev/null +++ b/packages/unplugin-rpc/src/core/build.ts @@ -0,0 +1,26 @@ +import path from 'node:path' +import { cwd } from 'node:process' +import { build, mergeConfig, UserConfig } from 'vite' +import { Options } from '../types' +import { swc } from './swc' + +export async function buildServer(options?: Options | undefined): Promise { + const serverEntry = options?.serverEntry || path.join(cwd(), './backend/main.ts') + const viteOptions = options?.viteOptions || {} + + await build(mergeConfig({ + build: { + ssr: serverEntry, + ssrManifest: true, + outDir: 'dist/backend', + }, + + ssr: { + noExternal: true, + }, + + plugins: [ + swc(), + ], + } as UserConfig, viteOptions)) +} diff --git a/packages/unplugin-rpc/src/core/factory.ts b/packages/unplugin-rpc/src/core/factory.ts new file mode 100644 index 0000000..feafce9 --- /dev/null +++ b/packages/unplugin-rpc/src/core/factory.ts @@ -0,0 +1,7 @@ +import { RpcBootstrap } from '@nailyjs/rpc' + +export type ViteRpcContextFn = (ctx: RpcBootstrap) => void | Promise + +export function ViteRpc(ctx?: ViteRpcContextFn): ViteRpcContextFn { + return ctx || (() => {}) +} diff --git a/packages/unplugin-rpc/src/core/vite-server-adapter.ts b/packages/unplugin-rpc/src/core/vite-server-adapter.ts index 812f738..e2c5d7e 100644 --- a/packages/unplugin-rpc/src/core/vite-server-adapter.ts +++ b/packages/unplugin-rpc/src/core/vite-server-adapter.ts @@ -6,6 +6,7 @@ import { Container } from '@nailyjs/ioc' import { RpcBootstrap, RpcControllerScanner, RpcHandlerContext } from '@nailyjs/rpc' import { ViteDevServer } from 'vite' import { Options } from '../types' +import { ViteRpcContextFn } from './factory' class ViteDevHttpAdapter implements IBackendAdapter { constructor( @@ -20,9 +21,20 @@ class ViteDevHttpAdapter implements IBackendAdapter { private async loadEntryModule(): Promise { const mod = await this.server.ssrLoadModule(this.serverEntry, { fixStacktrace: true }) - if (!(this.entryExport in mod) || typeof mod[this.entryExport] !== 'object') - throw new Error(`Cannot find export "${this.entryExport}" in ${this.serverEntry}`) - return mod[this.entryExport] as RpcBootstrap + const entryExport: ViteRpcContextFn = mod[this.entryExport] + if (!entryExport) + throw new Error(`[unplugin-rpc] Entry export '${this.entryExport}' not found in file '${this.serverEntry}'.`) + if (typeof entryExport !== 'function') + throw new Error(`[unplugin-rpc] Update v2.0.9, please rewrite your entry file export point to 'ViteRpc' function. For example: +export const app = ViteRpc((ctx) => { + // ctx.use(...) + + if (import.meta.env.PROD) + ctx.run(3000).then(() => console.log('Server is running on http://localhost:3000')) +})`) + const bootstrap = new RpcBootstrap() + await entryExport(bootstrap) + return bootstrap } private container = new Container() diff --git a/packages/unplugin-rpc/src/index.ts b/packages/unplugin-rpc/src/index.ts index 14912ca..ace2ff8 100644 --- a/packages/unplugin-rpc/src/index.ts +++ b/packages/unplugin-rpc/src/index.ts @@ -3,32 +3,12 @@ import path from 'node:path' import { cwd, exit } from 'node:process' import { createFilter } from '@rollup/pluginutils' import { createUnplugin, UnpluginFactory } from 'unplugin' -import { build, mergeConfig, UserConfig } from 'vite' -import { swc } from './core/swc' +import { buildServer } from './core/build' import { useViteDevServer } from './core/vite-server-adapter' import { Options } from './types' -export async function buildServer(options?: Options | undefined): Promise { - const serverEntry = options?.serverEntry || path.join(cwd(), './backend/main.ts') - const viteOptions = options?.viteOptions || {} - - await build(mergeConfig({ - build: { - ssr: serverEntry, - ssrManifest: true, - outDir: 'dist/backend', - }, - - ssr: { - noExternal: true, - }, - - plugins: [ - swc(), - ], - } as UserConfig, viteOptions)) -} - +export * from './core/build' +export * from './core/factory' export const unpluginFactory: UnpluginFactory = (options, meta) => { if (meta.framework !== 'vite') throw new Error(`[unplugin-rpc] Unsupported framework: ${meta.framework}, current only support vite.`) const watchDirs = options?.watchDirs || ['./backend/**/*']