Skip to content

Commit

Permalink
feat(plugin): add configureServer hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Nov 23, 2024
1 parent 59c213b commit 3aac455
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-cycles-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@umijs/tnf': patch
---

feat(plugin): add configureServer hook
4 changes: 1 addition & 3 deletions docs/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ Called when config resolved.

### configureServer

> Not implemented
- Type: `(server: DevServer) => void | Promise<void>`
- Type: `(server: { middlewares: express.Application }) => void | Promise<void>`

Can register pre or post middleware, or save server instance for other hooks.

Expand Down
9 changes: 9 additions & 0 deletions src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import { build } from './build';
import { createBundler } from './bundler/bundler';
import { BundlerType } from './bundler/bundler';
import { createServer } from './fishkit/server';
import { PluginHookType } from './plugin/plugin_manager';
import type { Context } from './types';

export async function dev({ context }: { context: Context }) {
const devServer = context.config?.devServer || {};
const { port, host, server, app } = await createServer({
devServer,
configureServer: () => {
context.pluginManager.apply({
hook: 'configureServer',
args: [],
type: PluginHookType.Series,
pluginContext: context.pluginContext,
});
},
});
const bundler = createBundler({ bundler: BundlerType.MAKO });
await bundler.configDevServer({
Expand Down
13 changes: 13 additions & 0 deletions src/fishkit/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { createHttpsServer } from './https';

export interface ServerOpts {
devServer: Config['devServer'];
configureServer?: (opts: {
middlewares: express.Application;
}) => void | (() => void);
}

export async function createServer(opts: ServerOpts): Promise<{
Expand All @@ -28,6 +31,14 @@ export async function createServer(opts: ServerOpts): Promise<{
const _port = await getPort(port);
const app = express();

const context = {
middlewares: app,
};
let configureServerFn;
if (opts.configureServer) {
configureServerFn = opts.configureServer(context);
}

// cors
app.use(
cors({
Expand All @@ -47,6 +58,8 @@ export async function createServer(opts: ServerOpts): Promise<{
}),
);

configureServerFn?.();

// create server
let server;
if (https) {
Expand Down
6 changes: 6 additions & 0 deletions src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export const PluginSchema = z.object({
z.union([z.any(), z.promise(z.any()), z.null()]),
)
.optional(),
configureServer: z
.function(
z.tuple([z.object({ middlewares: z.any() })]),
z.union([z.any(), z.promise(z.any()), z.null()]),
)
.optional(),
});

export type Plugin = z.infer<typeof PluginSchema>;
13 changes: 12 additions & 1 deletion src/preview.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { resolve } from 'pathe';
import sirv from 'sirv';
import { createServer } from './fishkit/server';
import { PluginHookType } from './plugin/plugin_manager';
import type { Context } from './types';

export async function preview({ context }: { context: Context }) {
const devServer = context.config?.devServer || {};
const { app } = await createServer({ devServer });
const { app } = await createServer({
devServer,
configureServer: () => {
context.pluginManager.apply({
hook: 'configureServer',
args: [],
type: PluginHookType.Series,
pluginContext: context.pluginContext,
});
},
});
const distDir = resolve(context.cwd, 'dist');

app.use(
Expand Down

0 comments on commit 3aac455

Please sign in to comment.