Skip to content

Commit

Permalink
docs(changeset): feat(unplugin-rpc): add rpc factory function to load…
Browse files Browse the repository at this point in the history
… module
  • Loading branch information
nailiable committed Nov 7, 2024
1 parent 0747865 commit c83367b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-seals-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unplugin-rpc": patch
---

feat(unplugin-rpc): add rpc factory function to load module
26 changes: 26 additions & 0 deletions packages/unplugin-rpc/src/core/build.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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))
}
7 changes: 7 additions & 0 deletions packages/unplugin-rpc/src/core/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { RpcBootstrap } from '@nailyjs/rpc'

export type ViteRpcContextFn = (ctx: RpcBootstrap) => void | Promise<void>

export function ViteRpc(ctx?: ViteRpcContextFn): ViteRpcContextFn {
return ctx || (() => {})
}
18 changes: 15 additions & 3 deletions packages/unplugin-rpc/src/core/vite-server-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -20,9 +21,20 @@ class ViteDevHttpAdapter implements IBackendAdapter {

private async loadEntryModule(): Promise<RpcBootstrap> {
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()
Expand Down
26 changes: 3 additions & 23 deletions packages/unplugin-rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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> = (options, meta) => {
if (meta.framework !== 'vite') throw new Error(`[unplugin-rpc] Unsupported framework: ${meta.framework}, current only support vite.`)
const watchDirs = options?.watchDirs || ['./backend/**/*']
Expand Down

0 comments on commit c83367b

Please sign in to comment.