Skip to content

fix(@angular-devkit/build-angular): don't override base-href in HTML when it's not set in builder #23477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion goldens/public-api/angular_devkit/build_angular/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type BrowserBuilderOutput = BuilderOutput & {
outputs: {
locale?: string;
path: string;
baseHref: string;
baseHref?: string;
}[];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async function _renderUniversal(
projectRoot,
root,
outputPath,
baseHref,
baseHref ?? '/',
browserOptions.ngswConfigPath,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export type BrowserBuilderOutput = BuilderOutput & {
outputs: {
locale?: string;
path: string;
baseHref: string;
baseHref?: string;
}[];
};

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -363,7 +361,7 @@ export function buildWebpackBrowser(
projectRoot,
context.workspaceRoot,
outputPath,
getLocaleBaseHref(i18n, locale) ?? defaultBaseHref,
getLocaleBaseHref(i18n, locale) ?? options.baseHref ?? '/',
options.ngswConfigPath,
);
} catch (error) {
Expand Down Expand Up @@ -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),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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': `
<html>
<head><base href="."></head>
<body></body>
</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(`<base href=".">`);

await run.stop();
});

it('should insert base href in the the correct position', async () => {
host.writeMultipleFiles({
'src/index.html': tags.oneLine`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down