Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remix-dev/vite): partial support for custom basename during dev #8141

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/remix-dev/vite/node/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
IncomingHttpHeaders,
IncomingMessage,
ServerResponse,
} from "node:http";
import type { IncomingHttpHeaders, ServerResponse } from "node:http";
import { once } from "node:events";
import { Readable } from "node:stream";
import { splitCookiesString } from "set-cookie-parser";
Expand All @@ -11,6 +7,7 @@ import {
createReadableStreamFromReadable,
} from "@remix-run/node";
import { createRequestHandler as createBaseRequestHandler } from "@remix-run/server-runtime";
import type * as Vite from "vite";

import invariant from "../../invariant";

Expand All @@ -33,13 +30,16 @@ function createHeaders(requestHeaders: IncomingHttpHeaders) {
}

// Based on `createRemixRequest` in packages/remix-express/server.ts
function createRequest(req: IncomingMessage, res: ServerResponse): Request {
function createRequest(
req: Vite.Connect.IncomingMessage,
res: ServerResponse
): Request {
let origin =
req.headers.origin && "null" !== req.headers.origin
? req.headers.origin
: `http://${req.headers.host}`;
invariant(req.url, 'Expected "req.url" to be defined');
let url = new URL(req.url, origin);
invariant(req.originalUrl, 'Expected "req.originalUrl" to be defined');
let url = new URL(req.originalUrl, origin);

let init: RequestInit = {
method: req.method,
Expand Down Expand Up @@ -88,7 +88,7 @@ export let createRequestHandler = (
{ mode = "production" }: { mode?: string }
) => {
let handler = createBaseRequestHandler(build, mode);
return async (req: IncomingMessage, res: ServerResponse) => {
return async (req: Vite.Connect.IncomingMessage, res: ServerResponse) => {
let request = createRequest(req, res);
let response = await handler(request, {});
handleNodeResponse(response, res);
Expand Down
53 changes: 35 additions & 18 deletions packages/remix-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,15 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
path: route.path,
index: route.index,
caseSensitive: route.caseSensitive,
module: `${resolveFileUrl(
pluginConfig,
resolveRelativeRouteFilePath(route, pluginConfig)
)}${
isJsFile(route.file) ? "" : "?import" // Ensure the Vite dev server responds with a JS module
}`,
module: path.posix.join(
pluginConfig.publicPath,
`${resolveFileUrl(
pluginConfig,
resolveRelativeRouteFilePath(route, pluginConfig)
)}${
isJsFile(route.file) ? "" : "?import" // Ensure the Vite dev server responds with a JS module
}`
),
hasAction: sourceExports.includes("action"),
hasLoader: sourceExports.includes("loader"),
hasClientAction: sourceExports.includes("clientAction"),
Expand All @@ -512,12 +515,21 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {

return {
version: String(Math.random()),
url: VirtualModule.url(browserManifestId),
url: path.posix.join(
pluginConfig.publicPath,
VirtualModule.url(browserManifestId)
),
hmr: {
runtime: VirtualModule.url(injectHmrRuntimeId),
runtime: path.posix.join(
pluginConfig.publicPath,
VirtualModule.url(injectHmrRuntimeId)
),
},
entry: {
module: resolveFileUrl(pluginConfig, pluginConfig.entryClientFilePath),
module: path.posix.join(
pluginConfig.publicPath,
resolveFileUrl(pluginConfig, pluginConfig.entryClientFilePath)
),
imports: [],
},
routes,
Expand Down Expand Up @@ -601,8 +613,8 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => {
"@remix-run/react",
],
},
base: pluginConfig.publicPath,
...(viteCommand === "build" && {
base: pluginConfig.publicPath,
build: {
...viteUserConfig.build,
...(!isSsrBuild
Expand Down Expand Up @@ -1254,16 +1266,21 @@ async function getRouteMetadata(
path: route.path,
index: route.index,
caseSensitive: route.caseSensitive,
url:
url: path.posix.join(
pluginConfig.publicPath,
"/" +
path.relative(
pluginConfig.rootDirectory,
path.relative(
pluginConfig.rootDirectory,
resolveRelativeRouteFilePath(route, pluginConfig)
)
),
module: path.posix.join(
pluginConfig.publicPath,
`${resolveFileUrl(
pluginConfig,
resolveRelativeRouteFilePath(route, pluginConfig)
),
module: `${resolveFileUrl(
pluginConfig,
resolveRelativeRouteFilePath(route, pluginConfig)
)}?import`, // Ensure the Vite dev server responds with a JS module
)}?import`
), // Ensure the Vite dev server responds with a JS module
hasAction: sourceExports.includes("action"),
hasClientAction: sourceExports.includes("clientAction"),
hasLoader: sourceExports.includes("loader"),
Expand Down