Skip to content

Commit

Permalink
fix: post configure server hook return undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ErKeLost committed Sep 25, 2024
1 parent 5a232d6 commit a379051
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
27 changes: 12 additions & 15 deletions examples/vue3/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@ import vue from "@vitejs/plugin-vue";
// import VueRouter from "unplugin-vue-router/vite";
// import AutoImport from 'unplugin-auto-import/vite';
import compression from "compression";
import { createHtmlPlugin } from 'vite-plugin-html'
import viteCompression from 'vite-plugin-compression';
import mkcert from 'vite-plugin-mkcert'
import Inspect from 'vite-plugin-inspect'
import { aaa } from "./test.js"
import { createHtmlPlugin } from "vite-plugin-html";
import viteCompression from "vite-plugin-compression";
import mkcert from "vite-plugin-mkcert";
import Inspect from "vite-plugin-inspect";
import { aaa } from "./test.js";
const compressionMiddleware = () => {
return {
name: "compression",
configureServer(server) {
// console.log("server", server.middlewares);
server.middlewares.use(compression());
}
},
};
};

function myCustomPlugin() {
return {
name: 'vite-plugin-custom',
name: "vite-plugin-custom",
configureServer(server) {
server.middlewares.use((req, res, next) => {
console.log(`收到请求 之前的: ${req.url}`);
next();
});

return () => {
server.middlewares.use((req, res, next) => {
console.log(`收到请求 posthook的: ${req.url}`);
Expand All @@ -37,15 +38,13 @@ function myCustomPlugin() {
configResolved(resolvedConfig) {
// console.log(resolvedConfig.env);
},
}
};
}


const logger = new Logger({
prefix: "我是曼"
prefix: "我是曼",
});


export default defineConfig({
vitePlugins: [
// mkcert(),
Expand Down Expand Up @@ -98,11 +97,9 @@ export default defineConfig({
},
server: {
// https: true,
port: 5384,
port: 5380,
},
plugins: [
// myCustomPlugin(),
]
// plugins: [myCustomPlugin()],
// plugins: [compressionMiddleware()],
// server: {
// port: 5232,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/plugin/js/vite-plugin-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ export class VitePluginAdapter implements JsPlugin {
'configureServer',
this._rawPlugin.configureServer
);

this._viteDevServer = createViteDevServerAdapter(
this.name,
this._viteConfig,
devServer
);

if (hook) {
await hook(this._viteDevServer);
return await hook(this._viteDevServer);
}
}

Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,6 @@ export class Server extends httpServer {
jsPlugins,
'configureServer'
)) {
console.log(hook);

this.postConfigureServerHooks.push(await hook(reflexServer));
}
}
Expand Down Expand Up @@ -565,8 +563,9 @@ export class Server extends httpServer {
this.middlewares.use(adaptorViteMiddleware(this));
}

// TODO server post hooks not work bug!
this.postConfigureServerHooks.forEach((fn) => fn && fn());
this.postConfigureServerHooks.reduce((_, fn) => {
if (typeof fn === 'function') fn();
}, null);

// TODO todo add appType 这块要判断 单页面还是 多页面 多 html 处理不一样
this.middlewares.use(htmlFallbackMiddleware(this));
Expand Down

0 comments on commit a379051

Please sign in to comment.