Skip to content

Commit 92fd484

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/build-angular): update vite client code update script
In the latest version of vite the script changed. (cherry picked from commit d1367a0)
1 parent d9a00e2 commit 92fd484

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/builder.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ export function execute(
7676
);
7777
}
7878

79-
if (options.allowedHosts?.length) {
80-
context.logger.warn(
81-
`The "allowedHosts" option will not be used because it is not supported by the "${builderName}" builder.`,
82-
);
83-
}
84-
8579
if (options.publicHost) {
8680
context.logger.warn(
8781
`The "publicHost" option will not be used because it is not supported by the "${builderName}" builder.`,

packages/angular_devkit/build_angular/src/builders/dev-server/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
},
8686
"disableHostCheck": {
8787
"type": "boolean",
88-
"description": "Don't verify connected clients are part of allowed hosts.",
88+
"description": "Don't verify connected clients are part of allowed hosts. This option has no effect when using the 'application' or other esbuild-based builders.",
8989
"default": false
9090
},
9191
"hmr": {

packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/allowed-hosts_spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
1313

1414
const FETCH_HEADERS = Object.freeze({ host: 'example.com' });
1515

16-
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
16+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget, isVite) => {
1717
describe('option: "allowedHosts"', () => {
1818
beforeEach(async () => {
1919
setupTarget(harness);
@@ -25,23 +25,31 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
2525
it('does not allow an invalid host when option is not present', async () => {
2626
harness.useTarget('serve', { ...BASE_OPTIONS });
2727

28-
const { result, response } = await executeOnceAndGet(harness, '/', {
28+
const { result, response, content } = await executeOnceAndGet(harness, '/', {
2929
request: { headers: FETCH_HEADERS },
3030
});
3131

3232
expect(result?.success).toBeTrue();
33-
expect(response?.statusCode).toBe(403);
33+
if (isVite) {
34+
expect(response?.statusCode).toBe(403);
35+
} else {
36+
expect(content).toBe('Invalid Host header');
37+
}
3438
});
3539

3640
it('does not allow an invalid host when option is an empty array', async () => {
3741
harness.useTarget('serve', { ...BASE_OPTIONS, allowedHosts: [] });
3842

39-
const { result, response } = await executeOnceAndGet(harness, '/', {
43+
const { result, response, content } = await executeOnceAndGet(harness, '/', {
4044
request: { headers: FETCH_HEADERS },
4145
});
4246

4347
expect(result?.success).toBeTrue();
44-
expect(response?.statusCode).toBe(403);
48+
if (isVite) {
49+
expect(response?.statusCode).toBe(403);
50+
} else {
51+
expect(content).toBe('Invalid Host header');
52+
}
4553
});
4654

4755
it('allows a host when specified in the option', async () => {

packages/angular_devkit/build_angular/src/tools/vite/angular-memory-plugin.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ export function createAngularMemoryPlugin(options: AngularMemoryPluginOptions):
7474
const codeContents = outputFiles.get(relativeFile)?.contents;
7575
if (codeContents === undefined) {
7676
if (relativeFile.endsWith('/node_modules/vite/dist/client/client.mjs')) {
77-
return {
78-
code: await loadViteClientCode(file),
79-
map: await readFile(file + '.map', 'utf-8'),
80-
};
77+
return await loadViteClientCode(file);
8178
}
8279

8380
return;
@@ -309,19 +306,21 @@ export function createAngularMemoryPlugin(options: AngularMemoryPluginOptions):
309306
* @param file The absolute path to the Vite client code.
310307
* @returns
311308
*/
312-
async function loadViteClientCode(file: string) {
309+
async function loadViteClientCode(file: string): Promise<string> {
313310
const originalContents = await readFile(file, 'utf-8');
314-
const firstUpdate = originalContents.replace('You can also disable this overlay by setting', '');
315-
assert(originalContents !== firstUpdate, 'Failed to update Vite client error overlay text. (1)');
316-
317-
const secondUpdate = firstUpdate.replace(
318-
// eslint-disable-next-line max-len
319-
'<code part="config-option-name">server.hmr.overlay</code> to <code part="config-option-value">false</code> in <code part="config-file-name">${hmrConfigName}.</code>',
311+
const updatedContents = originalContents.replace(
312+
`"You can also disable this overlay by setting ",
313+
h("code", { part: "config-option-name" }, "server.hmr.overlay"),
314+
" to ",
315+
h("code", { part: "config-option-value" }, "false"),
316+
" in ",
317+
h("code", { part: "config-file-name" }, hmrConfigName),
318+
"."`,
320319
'',
321320
);
322-
assert(firstUpdate !== secondUpdate, 'Failed to update Vite client error overlay text. (2)');
321+
assert(originalContents !== updatedContents, 'Failed to update Vite client error overlay text.');
323322

324-
return secondUpdate;
323+
return updatedContents;
325324
}
326325

327326
function pathnameWithoutBasePath(url: string, basePath: string): string {

0 commit comments

Comments
 (0)