From 7fc06486c8be34ae010294bb4ccf5170311d5ae2 Mon Sep 17 00:00:00 2001 From: Dominic Saadi Date: Fri, 16 Feb 2024 13:04:09 -0800 Subject: [PATCH] fix(realtime): update logic for including sseLink (#10025) Since realtime went out of experimental, the logic for including the SSE Link in the bundle needs to be updated. We can't just use TOML config anymore as a tell since there's none. Right now all I can think of as a real source of truth is if the user has the realtime package on the api side. --- packages/vite/src/index.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/index.ts b/packages/vite/src/index.ts index 4de0a64da24e..00d9f6f0a891 100644 --- a/packages/vite/src/index.ts +++ b/packages/vite/src/index.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'fs' +import fs from 'fs' import path from 'path' import react from '@vitejs/plugin-react' @@ -30,6 +30,12 @@ export default function redwoodPluginVite(): PluginOption[] { const relativeEntryPath = path.relative(rwPaths.web.src, clientEntryPath) + // If realtime is enabled, we want to include the sseLink in the bundle. + // Right now the only way we have of telling is if the package is installed on the api side. + const realtimeEnabled = fs + .readFileSync(path.join(rwPaths.api.base, 'package.json'), 'utf-8') + .includes('@redwoodjs/realtime') + return [ { name: 'redwood-plugin-vite-html-env', @@ -83,7 +89,7 @@ export default function redwoodPluginVite(): PluginOption[] { // So we inject the entrypoint with the correct extension .tsx vs .jsx // And then inject the entry - if (existsSync(clientEntryPath)) { + if (fs.existsSync(clientEntryPath)) { return html.replace( '', // @NOTE the slash in front, for windows compatibility and for pages in subdirectories @@ -99,7 +105,7 @@ export default function redwoodPluginVite(): PluginOption[] { // but note index.html does not come through as an id during dev transform: (code: string, id: string) => { if ( - existsSync(clientEntryPath) && + fs.existsSync(clientEntryPath) && normalizePath(id) === normalizePath(rwPaths.web.html) ) { return { @@ -286,7 +292,7 @@ export default function redwoodPluginVite(): PluginOption[] { id: /@redwoodjs\/router\/dist\/splash-page/, }, ]), - !rwConfig.experimental.realtime.enabled && + !realtimeEnabled && removeFromBundle([ { id: /@redwoodjs\/web\/dist\/apollo\/sseLink/,