Skip to content

Commit 4106883

Browse files
committed
vite: typesafe server build
1 parent dc5cdb7 commit 4106883

File tree

12 files changed

+82
-10
lines changed

12 files changed

+82
-10
lines changed

integration/helpers/vite.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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
);

integration/vite-basename-test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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) => {

packages/remix-cloudflare/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export {
1515
defer,
1616
broadcastDevReady,
1717
logDevReady,
18+
getServerBuild,
1819
isCookie,
1920
isSession,
2021
json,

packages/remix-deno/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export {
1717
broadcastDevReady,
1818
createSession,
1919
defer,
20+
getServerBuild,
2021
isCookie,
2122
isSession,
2223
json,

packages/remix-node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
defer,
2828
broadcastDevReady,
2929
logDevReady,
30+
getServerBuild,
3031
isCookie,
3132
isSession,
3233
json,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
});

packages/remix-server-runtime/build.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1+
import type { ViteDevServer } from "vite";
2+
import path from "pathe";
3+
14
import type { ActionFunctionArgs, LoaderFunctionArgs } from "./routeModules";
25
import type { AssetsManifest, EntryContext, FutureConfig } from "./entry";
36
import type { ServerRouteManifest } from "./routes";
47
import 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

packages/remix-server-runtime/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ export type {
8080
UploadHandler,
8181
UploadHandlerPart,
8282
} from "./reexport";
83+
export { getServerBuild } from "./reexport";

packages/remix-server-runtime/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,25 @@
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": {

packages/remix-server-runtime/reexport.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type {
77
ServerBuild,
88
ServerEntryModule,
99
} from "./build";
10+
export { getServerBuild } from "./build";
1011

1112
export type { UploadHandlerPart, UploadHandler } from "./formData";
1213
export type {

packages/remix-server-runtime/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"lib": ["ES2022"],
66
"target": "ES2022",
77
"composite": true,
8+
"module": "ESNext",
89

910
"moduleResolution": "Bundler",
1011
"allowSyntheticDefaultImports": true,

yarn.lock

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10747,6 +10747,11 @@ pathe@^1.0.0, pathe@^1.1.0:
1074710747
resolved "https://registry.npmjs.org/pathe/-/pathe-1.1.0.tgz#e2e13f6c62b31a3289af4ba19886c230f295ec03"
1074810748
integrity sha512-ODbEPR0KKHqECXW1GoxdDb+AZvULmXjVPy4rt+pGo2+TnjJTIPJQSVS6N63n8T2Ip+syHhbn52OewKicV0373w==
1074910749

10750+
pathe@^1.1.2:
10751+
version "1.1.2"
10752+
resolved "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz#6c4cb47a945692e48a1ddd6e4094d170516437ec"
10753+
integrity sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==
10754+
1075010755
peek-stream@^1.1.0:
1075110756
version "1.1.3"
1075210757
resolved "https://registry.npmjs.org/peek-stream/-/peek-stream-1.1.3.tgz"
@@ -13494,6 +13499,17 @@ vite@5.1.3:
1349413499
optionalDependencies:
1349513500
fsevents "~2.3.2"
1349613501

13502+
vite@^5.1.0:
13503+
version "5.1.4"
13504+
resolved "https://registry.npmjs.org/vite/-/vite-5.1.4.tgz#14e9d3e7a6e488f36284ef13cebe149f060bcfb6"
13505+
integrity sha512-n+MPqzq+d9nMVTKyewqw6kSt+R3CkvF9QAKY8obiQn8g1fwTscKxyfaYnC632HtBXAQGc1Yjomphwn1dtwGAHg==
13506+
dependencies:
13507+
esbuild "^0.19.3"
13508+
postcss "^8.4.35"
13509+
rollup "^4.2.0"
13510+
optionalDependencies:
13511+
fsevents "~2.3.3"
13512+
1349713513
w3c-xmlserializer@^4.0.0:
1349813514
version "4.0.0"
1349913515
resolved "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-4.0.0.tgz#aebdc84920d806222936e3cdce408e32488a3073"

0 commit comments

Comments
 (0)