From b86bb080e3a58a3320b2f68fb79edcdc98bfa7e9 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:30:34 -0400 Subject: [PATCH] fix(@angular/build): disable dev-server websocket when live reload is disabled When live reload is disabled (`"liveReload": false`/`no-live-reload`) within the development server, the Vite websocket server will no longer be initialized. Additionally, the client code will not be loaded by the browser. This allows the development server to be used in test scenarios that would prefer to more fully represent the production fielded configuration such as E2E testing or other forms of browser-based testing. (cherry picked from commit 46335625ebe6476887f3e8797177aa9431d98ee1) --- .../angular/build/src/builders/dev-server/vite-server.ts | 3 +++ .../src/tools/vite/plugins/angular-memory-plugin.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/angular/build/src/builders/dev-server/vite-server.ts b/packages/angular/build/src/builders/dev-server/vite-server.ts index 659d51bb2165..33a000a91f9b 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -651,6 +651,8 @@ export async function setupServer( host: serverOptions.host, open: serverOptions.open, headers: serverOptions.headers, + // Disable the websocket if live reload is disabled (false/undefined are the only valid values) + ws: serverOptions.liveReload === false && serverOptions.hmr === false ? false : undefined, proxy, cors: { // Allow preflight requests to be proxied. @@ -711,6 +713,7 @@ export async function setupServer( virtualProjectRoot, outputFiles, external: externalMetadata.explicitBrowser, + skipViteClient: serverOptions.liveReload === false && serverOptions.hmr === false, }), ], // Browser only optimizeDeps. (This does not run for SSR dependencies). diff --git a/packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts b/packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts index 1f5012b028f3..0af30710f2a9 100644 --- a/packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts +++ b/packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts @@ -17,6 +17,7 @@ interface AngularMemoryPluginOptions { virtualProjectRoot: string; outputFiles: AngularMemoryOutputFiles; external?: string[]; + skipViteClient?: boolean; } export async function createAngularMemoryPlugin( @@ -55,9 +56,11 @@ export async function createAngularMemoryPlugin( const relativeFile = '/' + normalizePath(relative(virtualProjectRoot, file)); const codeContents = outputFiles.get(relativeFile)?.contents; if (codeContents === undefined) { - return relativeFile.endsWith('/node_modules/vite/dist/client/client.mjs') - ? loadViteClientCode(file) - : undefined; + if (relativeFile.endsWith('/node_modules/vite/dist/client/client.mjs')) { + return options.skipViteClient ? '' : loadViteClientCode(file); + } + + return undefined; } const code = Buffer.from(codeContents).toString('utf-8');