@@ -45,34 +45,27 @@ function generateNetlifyFunction(handlerPath: string) {
45
45
46
46
export function netlifyPlugin ( ) : Plugin {
47
47
let resolvedConfig : ResolvedConfig
48
- let currentCommand : string
49
- let isSsr : boolean | undefined
48
+ let isProductionSsrBuild = false
50
49
return {
51
- name : 'vite-plugin-react-router-netlify-functions ' ,
50
+ name : 'vite-plugin-netlify- react-router' ,
52
51
config ( config , { command, isSsrBuild } ) {
53
- currentCommand = command
54
- isSsr = isSsrBuild
55
- if ( command === 'build' ) {
56
- if ( isSsrBuild ) {
57
- // We need to add an extra SSR entrypoint, as we need to compile
58
- // the server entrypoint too. This is because it uses virtual
59
- // modules.
60
- // NOTE: the below is making various assumptions about the React Router Vite plugin's
61
- // implementation details:
62
- // https://github.com/remix-run/remix/blob/cc65962b1a96d1e134336aa9620ef1dad7c5efb1/packages/remix-dev/vite/plugin.ts#L1149-L1168
63
- // TODO(serhalp) Stop making these assumptions or assert them explictly.
64
- // TODO(serhalp) Unless I'm misunderstanding something, we should only need to *replace*
65
- // the default React Router Vite SSR entrypoint, not add an additional one.
66
- if ( typeof config . build ?. rollupOptions ?. input === 'string' ) {
67
- config . build . rollupOptions . input = {
68
- [ FUNCTION_HANDLER_CHUNK ] : FUNCTION_HANDLER_MODULE_ID ,
69
- index : config . build . rollupOptions . input ,
70
- }
71
- if ( config . build . rollupOptions . output && ! Array . isArray ( config . build . rollupOptions . output ) ) {
72
- config . build . rollupOptions . output . entryFileNames = '[name].js'
73
- }
74
- }
52
+ isProductionSsrBuild = isSsrBuild === true && command === 'build'
53
+ if ( isProductionSsrBuild ) {
54
+ // Replace the default SSR entrypoint with our own entrypoint (which is imported by our
55
+ // Netlify function handler via a virtual module)
56
+ config . build ??= { }
57
+ config . build . rollupOptions ??= { }
58
+ config . build . rollupOptions . input = {
59
+ [ FUNCTION_HANDLER_CHUNK ] : FUNCTION_HANDLER_MODULE_ID ,
75
60
}
61
+ config . build . rollupOptions . output ??= { }
62
+ if ( Array . isArray ( config . build . rollupOptions . output ) ) {
63
+ console . warn (
64
+ 'Expected Vite config `build.rollupOptions.output` to be an object, but it is an array - overwriting it, but this may cause issues with your custom configuration' ,
65
+ )
66
+ config . build . rollupOptions . output = { }
67
+ }
68
+ config . build . rollupOptions . output . entryFileNames = '[name].js'
76
69
}
77
70
} ,
78
71
async resolveId ( source ) {
@@ -92,7 +85,7 @@ export function netlifyPlugin(): Plugin {
92
85
// See https://rollupjs.org/plugin-development/#writebundle.
93
86
async writeBundle ( ) {
94
87
// Write the server entrypoint to the Netlify functions directory
95
- if ( currentCommand === 'build' && isSsr ) {
88
+ if ( isProductionSsrBuild ) {
96
89
const functionsDirectory = join ( resolvedConfig . root , NETLIFY_FUNCTIONS_DIR )
97
90
98
91
await mkdir ( functionsDirectory , { recursive : true } )
0 commit comments