Skip to content

Commit a1f5d1d

Browse files
committed
refactor: simplify SSR entrypoint logic
1 parent dbd1a42 commit a1f5d1d

File tree

1 file changed

+19
-26
lines changed
  • packages/vite-plugin-react-router/src

1 file changed

+19
-26
lines changed

packages/vite-plugin-react-router/src/plugin.ts

+19-26
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,27 @@ function generateNetlifyFunction(handlerPath: string) {
4545

4646
export function netlifyPlugin(): Plugin {
4747
let resolvedConfig: ResolvedConfig
48-
let currentCommand: string
49-
let isSsr: boolean | undefined
48+
let isProductionSsrBuild = false
5049
return {
51-
name: 'vite-plugin-react-router-netlify-functions',
50+
name: 'vite-plugin-netlify-react-router',
5251
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,
7560
}
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'
7669
}
7770
},
7871
async resolveId(source) {
@@ -92,7 +85,7 @@ export function netlifyPlugin(): Plugin {
9285
// See https://rollupjs.org/plugin-development/#writebundle.
9386
async writeBundle() {
9487
// Write the server entrypoint to the Netlify functions directory
95-
if (currentCommand === 'build' && isSsr) {
88+
if (isProductionSsrBuild) {
9689
const functionsDirectory = join(resolvedConfig.root, NETLIFY_FUNCTIONS_DIR)
9790

9891
await mkdir(functionsDirectory, { recursive: true })

0 commit comments

Comments
 (0)