Skip to content

Commit

Permalink
chore: name remixDevServerMiddleware and make it a standalone plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey committed Feb 12, 2024
1 parent 4111fd1 commit 11611ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-badgers-pay.md
@@ -0,0 +1,6 @@
---
"remix": patch
"@remix-run/dev": patch
---

Name remixDevServerMiddleware and make it a standalone plugin giving subsequent Vite plugins a means to remove the dev server plugin, or the middleware it adds from the dev server.
64 changes: 37 additions & 27 deletions packages/remix-dev/vite/plugin.ts
Expand Up @@ -2,6 +2,7 @@
// context but want to use Vite's ESM build to avoid deprecation warnings
import type * as Vite from "vite";
import { type BinaryLike, createHash } from "node:crypto";
import type * as http from "node:http";
import * as path from "node:path";
import * as url from "node:url";
import * as fse from "fs-extra";
Expand Down Expand Up @@ -1183,33 +1184,6 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
}
}
});

return () => {
// Let user servers handle SSR requests in middleware mode,
// otherwise the Vite plugin will handle the request
if (!viteDevServer.config.server.middlewareMode) {
viteDevServer.middlewares.use(async (req, res, next) => {
try {
let build = (await viteDevServer.ssrLoadModule(
serverBuildId
)) as ServerBuild;

let handler = createRequestHandler(build, "development");
let nodeHandler: NodeRequestHandler = async (
nodeReq,
nodeRes
) => {
let req = fromNodeRequest(nodeReq);
let res = await handler(req, await remixDevLoadContext(req));
await toNodeRequest(res, nodeRes);
};
await nodeHandler(req, res);
} catch (error) {
next(error);
}
});
}
};
},
writeBundle: {
// After the SSR build is finished, we inspect the Vite manifest for
Expand Down Expand Up @@ -1298,6 +1272,42 @@ export const remixVitePlugin: RemixVitePlugin = (remixUserConfig = {}) => {
await viteChildCompiler?.close();
},
},
{
name: "remix-dev-server-middleware",
configureServer(viteDevServer) {
return () => {
// Let user servers handle SSR requests in middleware mode,
// otherwise the Vite plugin will handle the request
if (!viteDevServer.config.server.middlewareMode) {
async function remixDevServerMiddleware(
req: Vite.Connect.IncomingMessage,
res: http.ServerResponse<http.IncomingMessage>,
next: Vite.Connect.NextFunction
) {
try {
let build = (await viteDevServer.ssrLoadModule(
serverBuildId
)) as ServerBuild;

let handler = createRequestHandler(build, "development");
let nodeHandler: NodeRequestHandler = async (
nodeReq,
nodeRes
) => {
let req = fromNodeRequest(nodeReq);
let res = await handler(req, await remixDevLoadContext(req));
await toNodeRequest(res, nodeRes);
};
await nodeHandler(req, res);
} catch (error) {
next(error);
}
}
viteDevServer.middlewares.use(remixDevServerMiddleware);
}
};
},
},
{
name: "remix-virtual-modules",
enforce: "pre",
Expand Down

0 comments on commit 11611ac

Please sign in to comment.