Skip to content

Commit 73cef20

Browse files
committed
fix(@angular/build): always provide Vite client helpers with development server
In addition to the WebSocket code, the Vite client module contains helper functions which may be injected into modules at request time. These helpers are required for certain behavior to function. Previously, when `--no-live-reload` was used, these helpers may not have been available which led to runtime errors. These runtime errors will no longer occur. However, the browser console will now log that the Vite client cannot connect to the development server WebSocket. This is expected in this case since live reload functionality was disabled and the server side is intentionally not available.
1 parent 25dbe7c commit 73cef20

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

packages/angular/build/src/builders/dev-server/vite-server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ export async function setupServer(
854854
outputFiles,
855855
templateUpdates,
856856
external: externalMetadata.explicitBrowser,
857-
skipViteClient: serverOptions.liveReload === false && serverOptions.hmr === false,
857+
disableViteTransport: !serverOptions.liveReload,
858858
}),
859859
],
860860
// Browser only optimizeDeps. (This does not run for SSR dependencies).

packages/angular/build/src/tools/vite/plugins/angular-memory-plugin.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface AngularMemoryPluginOptions {
1919
outputFiles: AngularMemoryOutputFiles;
2020
templateUpdates?: ReadonlyMap<string, string>;
2121
external?: string[];
22-
skipViteClient?: boolean;
22+
disableViteTransport?: boolean;
2323
}
2424

2525
const ANGULAR_PREFIX = '/@ng/';
@@ -91,7 +91,7 @@ export async function createAngularMemoryPlugin(
9191
const codeContents = outputFiles.get(relativeFile)?.contents;
9292
if (codeContents === undefined) {
9393
if (relativeFile.endsWith('/node_modules/vite/dist/client/client.mjs')) {
94-
return options.skipViteClient ? '' : loadViteClientCode(file);
94+
return loadViteClientCode(file, options.disableViteTransport);
9595
}
9696

9797
return undefined;
@@ -118,9 +118,9 @@ export async function createAngularMemoryPlugin(
118118
* @param file The absolute path to the Vite client code.
119119
* @returns
120120
*/
121-
async function loadViteClientCode(file: string): Promise<string> {
121+
async function loadViteClientCode(file: string, disableViteTransport = false): Promise<string> {
122122
const originalContents = await readFile(file, 'utf-8');
123-
const updatedContents = originalContents.replace(
123+
let updatedContents = originalContents.replace(
124124
`"You can also disable this overlay by setting ",
125125
h("code", { part: "config-option-name" }, "server.hmr.overlay"),
126126
" to ",
@@ -133,5 +133,17 @@ async function loadViteClientCode(file: string): Promise<string> {
133133

134134
assert(originalContents !== updatedContents, 'Failed to update Vite client error overlay text.');
135135

136+
if (disableViteTransport) {
137+
const previousUpdatedContents = updatedContents;
138+
139+
updatedContents = updatedContents.replace('transport.connect(handleMessage)', '');
140+
assert(
141+
previousUpdatedContents !== updatedContents,
142+
'Failed to update Vite client WebSocket disable.',
143+
);
144+
145+
updatedContents = updatedContents.replace('console.debug("[vite] connecting...")', '');
146+
}
147+
136148
return updatedContents;
137149
}

0 commit comments

Comments
 (0)