File tree Expand file tree Collapse file tree 12 files changed +82
-10
lines changed Expand file tree Collapse file tree 12 files changed +82
-10
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ export const EXPRESS_SERVER = (args: {
4242} ) =>
4343 String . raw `
4444 import { createRequestHandler } from "@remix-run/express";
45- import { installGlobals } from "@remix-run/node";
45+ import { installGlobals, getServerBuild } from "@remix-run/node";
4646 import express from "express";
4747
4848 installGlobals();
@@ -71,9 +71,7 @@ export const EXPRESS_SERVER = (args: {
7171 app.all(
7272 "*",
7373 createRequestHandler({
74- build: viteDevServer
75- ? () => viteDevServer.ssrLoadModule("virtual:remix/server-build")
76- : await import("./build/index.js"),
74+ build: await getServerBuild("./build/server/index.js", viteDevServer),
7775 getLoadContext: () => (${ JSON . stringify ( args . loadContext ?? { } ) } ),
7876 })
7977 );
Original file line number Diff line number Diff line change @@ -95,7 +95,7 @@ const customServerFile = ({
9595
9696 return String . raw `
9797 import { createRequestHandler } from "@remix-run/express";
98- import { installGlobals } from "@remix-run/node";
98+ import { installGlobals, getServerBuild } from "@remix-run/node";
9999 import express from "express";
100100 installGlobals();
101101
@@ -115,9 +115,7 @@ const customServerFile = ({
115115 app.all(
116116 "${ basename } *",
117117 createRequestHandler({
118- build: viteDevServer
119- ? () => viteDevServer.ssrLoadModule("virtual:remix/server-build")
120- : await import("./build/server/index.js"),
118+ build: await getServerBuild("./build/server/index.js", viteDevServer),
121119 })
122120 );
123121 app.get("*", (_req, res) => {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ export {
1515 defer ,
1616 broadcastDevReady ,
1717 logDevReady ,
18+ getServerBuild ,
1819 isCookie ,
1920 isSession ,
2021 json ,
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ export {
1717 broadcastDevReady ,
1818 createSession ,
1919 defer ,
20+ getServerBuild ,
2021 isCookie ,
2122 isSession ,
2223 json ,
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ export {
2727 defer ,
2828 broadcastDevReady ,
2929 logDevReady ,
30+ getServerBuild ,
3031 isCookie ,
3132 isSession ,
3233 json ,
Original file line number Diff line number Diff line change 1+ import { getServerBuild } from "../build" ;
2+
3+ it ( "getServerBuild throws when path is relative" , async ( ) => {
4+ await expect ( ( ) => getServerBuild ( "./build/server/index.js" ) ) . rejects . toThrow (
5+ "Server build path must be absolute, but received relative path: ./build/server/index.js"
6+ ) ;
7+ } ) ;
8+
9+ it ( "getServerBuild throws when build does not exist" , async ( ) => {
10+ await expect ( ( ) =>
11+ getServerBuild ( "/this/path/doesnt/exist.js" )
12+ ) . rejects . toThrow (
13+ "Could not import server build from '/this/path/doesnt/exist.js'. Did you forget to run 'remix vite:build' first?"
14+ ) ;
15+ } ) ;
Original file line number Diff line number Diff line change 1+ import type { ViteDevServer } from "vite" ;
2+ import path from "pathe" ;
3+
14import type { ActionFunctionArgs , LoaderFunctionArgs } from "./routeModules" ;
25import type { AssetsManifest , EntryContext , FutureConfig } from "./entry" ;
36import type { ServerRouteManifest } from "./routes" ;
47import type { AppLoadContext } from "./data" ;
58
9+ export async function getServerBuild (
10+ buildPath : string ,
11+ {
12+ viteDevServer,
13+ } : {
14+ viteDevServer ?: ViteDevServer ;
15+ } = { }
16+ ) : Promise < ServerBuild | ( ( ) => Promise < ServerBuild > ) > {
17+ if ( viteDevServer ) {
18+ return ( ) =>
19+ viteDevServer . ssrLoadModule (
20+ "virtual:remix/server-build"
21+ ) as Promise < ServerBuild > ;
22+ }
23+
24+ if ( ! path . isAbsolute ( buildPath ) ) {
25+ throw new Error (
26+ `Server build path must be absolute, but received relative path: ${ buildPath } `
27+ ) ;
28+ }
29+
30+ // Convert file path meant for `import` to URL for Windows compatibility
31+ let buildURL = "file:///" + encodeURI ( buildPath ) ;
32+ return import ( buildURL ) . catch ( ( ) => {
33+ throw Error (
34+ `Could not import server build from '${ buildPath } '. Did you forget to run 'remix vite:build' first?`
35+ ) ;
36+ } ) ;
37+ }
38+
639// NOTE: IF you modify `ServerBuild`, be sure to modify the
740// `remix-dev/server-build.ts` file to reflect the new field as well
841
Original file line number Diff line number Diff line change @@ -80,3 +80,4 @@ export type {
8080 UploadHandler ,
8181 UploadHandlerPart ,
8282} from "./reexport" ;
83+ export { getServerBuild } from "./reexport" ;
Original file line number Diff line number Diff line change 2020 "@types/cookie" : " ^0.6.0" ,
2121 "@web3-storage/multipart-parser" : " ^1.0.0" ,
2222 "cookie" : " ^0.6.0" ,
23+ "pathe" : " ^1.1.2" ,
2324 "set-cookie-parser" : " ^2.4.8" ,
2425 "source-map" : " ^0.7.3"
2526 },
2627 "devDependencies" : {
2728 "@types/set-cookie-parser" : " ^2.4.1" ,
28- "typescript" : " ^5.1.6"
29+ "typescript" : " ^5.1.6" ,
30+ "vite" : " ^5.1.0"
2931 },
3032 "peerDependencies" : {
31- "typescript" : " ^5.1.0"
33+ "typescript" : " ^5.1.0" ,
34+ "vite" : " ^5.1.0"
3235 },
3336 "peerDependenciesMeta" : {
3437 "typescript" : {
3538 "optional" : true
39+ },
40+ "vite" : {
41+ "optional" : true
3642 }
3743 },
3844 "engines" : {
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ export type {
77 ServerBuild ,
88 ServerEntryModule ,
99} from "./build" ;
10+ export { getServerBuild } from "./build" ;
1011
1112export type { UploadHandlerPart , UploadHandler } from "./formData" ;
1213export type {
You can’t perform that action at this time.
0 commit comments