@@ -4,23 +4,35 @@ import { join, relative, sep } from 'node:path'
4
4
import { sep as posixSep } from 'node:path/posix'
5
5
import { version , name } from '../../package.json'
6
6
7
- const SERVER_ID = 'virtual:netlify-server'
8
- const RESOLVED_SERVER_ID = `\0${ SERVER_ID } `
7
+ const NETLIFY_FUNCTIONS_DIR = '.netlify/functions-internal'
8
+
9
+ const FUNCTION_FILENAME = 'remix-server.mjs'
10
+ /**
11
+ * The chunk filename without an extension, i.e. in the Rollup config `input` format
12
+ */
13
+ const FUNCTION_HANDLER_CHUNK = 'server'
14
+
15
+ const FUNCTION_HANDLER_MODULE_ID = 'virtual:netlify-server'
16
+ const RESOLVED_FUNCTION_HANDLER_MODULE_ID = `\0${ FUNCTION_HANDLER_MODULE_ID } `
9
17
10
18
const toPosixPath = ( path : string ) => path . split ( sep ) . join ( posixSep )
11
19
12
- // The virtual module that is the compiled server entrypoint.
13
- const serverCode = /* js */ `
20
+ // The virtual module that is the compiled Vite SSR entrypoint (a Netlify Function handler)
21
+ const FUNCTION_HANDLER = /* js */ `
14
22
import { createRequestHandler } from "@netlify/remix-adapter";
15
23
import * as build from "virtual:remix/server-build";
16
- export default createRequestHandler({ build });
24
+ export default createRequestHandler({
25
+ build,
26
+ getLoadContext: async (_req, ctx) => ctx,
27
+ });
17
28
`
18
29
19
30
// This is written to the functions directory. It just re-exports
20
31
// the compiled entrypoint, along with Netlify function config.
21
- function generateNetlifyFunction ( server : string ) {
32
+ function generateNetlifyFunction ( handlerPath : string ) {
22
33
return /* js */ `
23
- export { default } from "${ server } ";
34
+ export { default } from "${ handlerPath } ";
35
+
24
36
export const config = {
25
37
name: "Remix server handler",
26
38
generator: "${ name } @${ version } ",
@@ -41,12 +53,18 @@ export function netlifyPlugin(): Plugin {
41
53
isSsr = isSsrBuild
42
54
if ( command === 'build' ) {
43
55
if ( isSsrBuild ) {
44
- // We need to add an extra entrypoint, as we need to compile
56
+ // We need to add an extra SSR entrypoint, as we need to compile
45
57
// the server entrypoint too. This is because it uses virtual
46
58
// modules.
59
+ // NOTE: the below is making various assumptions about the Remix Vite plugin's
60
+ // implementation details:
61
+ // https://github.com/remix-run/remix/blob/cc65962b1a96d1e134336aa9620ef1dad7c5efb1/packages/remix-dev/vite/plugin.ts#L1149-L1168
62
+ // TODO(serhalp) Stop making these assumptions or assert them explictly.
63
+ // TODO(serhalp) Unless I'm misunderstanding something, we should only need to *replace*
64
+ // the default Remix Vite SSR entrypoint, not add an additional one.
47
65
if ( typeof config . build ?. rollupOptions ?. input === 'string' ) {
48
66
config . build . rollupOptions . input = {
49
- server : SERVER_ID ,
67
+ [ FUNCTION_HANDLER_CHUNK ] : FUNCTION_HANDLER_MODULE_ID ,
50
68
index : config . build . rollupOptions . input ,
51
69
}
52
70
if ( config . build . rollupOptions . output && ! Array . isArray ( config . build . rollupOptions . output ) ) {
@@ -57,14 +75,14 @@ export function netlifyPlugin(): Plugin {
57
75
}
58
76
} ,
59
77
async resolveId ( source ) {
60
- if ( source === SERVER_ID ) {
61
- return RESOLVED_SERVER_ID
78
+ if ( source === FUNCTION_HANDLER_MODULE_ID ) {
79
+ return RESOLVED_FUNCTION_HANDLER_MODULE_ID
62
80
}
63
81
} ,
64
82
// See https://vitejs.dev/guide/api-plugin#virtual-modules-convention.
65
83
load ( id ) {
66
- if ( id === RESOLVED_SERVER_ID ) {
67
- return serverCode
84
+ if ( id === RESOLVED_FUNCTION_HANDLER_MODULE_ID ) {
85
+ return FUNCTION_HANDLER
68
86
}
69
87
} ,
70
88
async configResolved ( config ) {
@@ -74,14 +92,14 @@ export function netlifyPlugin(): Plugin {
74
92
async writeBundle ( ) {
75
93
// Write the server entrypoint to the Netlify functions directory
76
94
if ( currentCommand === 'build' && isSsr ) {
77
- const functionsDirectory = join ( resolvedConfig . root , '.netlify/functions-internal' )
95
+ const functionsDirectory = join ( resolvedConfig . root , NETLIFY_FUNCTIONS_DIR )
78
96
79
97
await mkdir ( functionsDirectory , { recursive : true } )
80
98
81
- const serverPath = join ( resolvedConfig . build . outDir , 'server .js' )
82
- const relativeServerPath = toPosixPath ( relative ( functionsDirectory , serverPath ) )
99
+ const handlerPath = join ( resolvedConfig . build . outDir , ` ${ FUNCTION_HANDLER_CHUNK } .js` )
100
+ const relativeHandlerPath = toPosixPath ( relative ( functionsDirectory , handlerPath ) )
83
101
84
- await writeFile ( join ( functionsDirectory , 'remix-server.mjs' ) , generateNetlifyFunction ( relativeServerPath ) )
102
+ await writeFile ( join ( functionsDirectory , FUNCTION_FILENAME ) , generateNetlifyFunction ( relativeHandlerPath ) )
85
103
}
86
104
} ,
87
105
}
0 commit comments