Skip to content

Commit

Permalink
fix(@angular/build): disable dev-server websocket when live reload is…
Browse files Browse the repository at this point in the history
… 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 4633562)
  • Loading branch information
clydin authored and alan-agius4 committed Nov 5, 2024
1 parent fed31e0 commit b86bb08
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface AngularMemoryPluginOptions {
virtualProjectRoot: string;
outputFiles: AngularMemoryOutputFiles;
external?: string[];
skipViteClient?: boolean;
}

export async function createAngularMemoryPlugin(
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit b86bb08

Please sign in to comment.