From 43cb2f643092122fb6e3c51c917fbd9ad179e482 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 30 Jun 2022 12:13:22 +0000 Subject: [PATCH 1/2] fix(@angular-devkit/build-angular): don't override base-href in HTML when it's not set in builder With this change we fix a regression were we set the base-href to `/` when the browser builder `baseHref` option is not set. Closes #23475 --- .../src/builders/app-shell/index.ts | 2 +- .../src/builders/browser/index.ts | 12 +++++------ .../builders/browser/specs/base-href_spec.ts | 21 +++++++++++++++++++ .../build_angular/src/testing/test-utils.ts | 3 +-- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts index ec88c92cbcc4..37b1c7cb29e5 100644 --- a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts @@ -118,7 +118,7 @@ async function _renderUniversal( projectRoot, root, outputPath, - baseHref, + baseHref ?? '/', browserOptions.ngswConfigPath, ); } diff --git a/packages/angular_devkit/build_angular/src/builders/browser/index.ts b/packages/angular_devkit/build_angular/src/builders/browser/index.ts index 0651ae889f8b..34f21b81543c 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/index.ts @@ -79,7 +79,7 @@ export type BrowserBuilderOutput = BuilderOutput & { outputs: { locale?: string; path: string; - baseHref: string; + baseHref?: string; }[]; }; @@ -183,8 +183,6 @@ export function buildWebpackBrowser( ({ config, projectRoot, projectSourceRoot, i18n, target, cacheOptions }) => { const normalizedOptimization = normalizeOptimization(options.optimization); - const defaultBaseHref = options.baseHref ?? '/'; - return runWebpack(config, context, { webpackFactory: require('webpack') as typeof webpack, logging: @@ -319,7 +317,7 @@ export function buildWebpackBrowser( for (const [locale, outputPath] of outputPaths.entries()) { try { const { content, warnings, errors } = await indexHtmlGenerator.process({ - baseHref: getLocaleBaseHref(i18n, locale) || defaultBaseHref, + baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref, // i18nLocale is used when Ivy is disabled lang: locale || undefined, outputPath, @@ -363,7 +361,7 @@ export function buildWebpackBrowser( projectRoot, context.workspaceRoot, outputPath, - getLocaleBaseHref(i18n, locale) ?? defaultBaseHref, + getLocaleBaseHref(i18n, locale) ?? options.baseHref ?? '/', options.ngswConfigPath, ); } catch (error) { @@ -393,10 +391,10 @@ export function buildWebpackBrowser( [...outputPaths.entries()].map(([locale, path]) => ({ locale, path, - baseHref: getLocaleBaseHref(i18n, locale) ?? defaultBaseHref, + baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref, }))) || { path: baseOutputPath, - baseHref: defaultBaseHref, + baseHref: options.baseHref, }, } as BrowserBuilderOutput), ), diff --git a/packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts b/packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts index bb8b6e784171..536ea0c5ae16 100644 --- a/packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts @@ -39,6 +39,27 @@ describe('Browser Builder base href', () => { await run.stop(); }); + it('should not override base href in HTML when option is not set', async () => { + host.writeMultipleFiles({ + 'src/index.html': ` + + + + + `, + }); + + const run = await architect.scheduleTarget(targetSpec); + const output = (await run.result) as BrowserBuilderOutput; + + expect(output.success).toBeTrue(); + const fileName = join(normalize(output.outputs[0].path), 'index.html'); + const content = virtualFs.fileBufferToString(await host.read(fileName).toPromise()); + expect(content).toContain(``); + + await run.stop(); + }); + it('should insert base href in the the correct position', async () => { host.writeMultipleFiles({ 'src/index.html': tags.oneLine` diff --git a/packages/angular_devkit/build_angular/src/testing/test-utils.ts b/packages/angular_devkit/build_angular/src/testing/test-utils.ts index 3387b8cee8f9..c83d76f8d9c3 100644 --- a/packages/angular_devkit/build_angular/src/testing/test-utils.ts +++ b/packages/angular_devkit/build_angular/src/testing/test-utils.ts @@ -83,8 +83,7 @@ export async function browserBuild( }; } - const [{ path, baseHref }] = output.outputs; - expect(baseHref).toBeTruthy(); + const [{ path }] = output.outputs; expect(path).toBeTruthy(); const outputPath = normalize(path); From 422e1684901e51ab5ae582aebafd09d5a8aa59bc Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 30 Jun 2022 12:26:23 +0000 Subject: [PATCH 2/2] test(@angular-devkit/build-angular): update golden file --- goldens/public-api/angular_devkit/build_angular/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goldens/public-api/angular_devkit/build_angular/index.md b/goldens/public-api/angular_devkit/build_angular/index.md index 1450faeb3199..bfd2d1f2c2f4 100644 --- a/goldens/public-api/angular_devkit/build_angular/index.md +++ b/goldens/public-api/angular_devkit/build_angular/index.md @@ -79,7 +79,7 @@ export type BrowserBuilderOutput = BuilderOutput & { outputs: { locale?: string; path: string; - baseHref: string; + baseHref?: string; }[]; };