Skip to content

fix(@angular/build): allow a default application browser option #30240

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
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/build/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type ApplicationBuilderOptions = {
appShell?: boolean;
assets?: AssetPattern[];
baseHref?: string;
browser: string;
browser?: string;
budgets?: Budget[];
clearScreen?: boolean;
conditions?: string[];
Expand Down
20 changes: 12 additions & 8 deletions packages/angular/build/src/builders/application/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ export async function normalizeOptions(
i18nOptions.flatOutput = true;
}

const entryPoints = normalizeEntryPoints(workspaceRoot, options.browser, options.entryPoints);
const entryPoints = normalizeEntryPoints(
workspaceRoot,
projectSourceRoot,
options.browser,
options.entryPoints,
);
const tsconfig = path.join(workspaceRoot, options.tsConfig);
const optimizationOptions = normalizeOptimization(options.optimization);
const sourcemapOptions = normalizeSourceMaps(options.sourceMap ?? false);
Expand Down Expand Up @@ -545,26 +550,25 @@ async function getTailwindConfig(
*/
function normalizeEntryPoints(
workspaceRoot: string,
projectSourceRoot: string,
browser: string | undefined,
entryPoints: Set<string> | Map<string, string> = new Set(),
entryPoints: Set<string> | Map<string, string> | undefined,
): Record<string, string> {
if (browser === '') {
throw new Error('`browser` option cannot be an empty string.');
}

// `browser` and `entryPoints` are mutually exclusive.
if (browser && entryPoints.size > 0) {
if (browser && entryPoints) {
throw new Error('Only one of `browser` or `entryPoints` may be provided.');
}
if (!browser && entryPoints.size === 0) {
// Schema should normally reject this case, but programmatic usages of the builder might make this mistake.
throw new Error('Either `browser` or at least one `entryPoints` value must be provided.');
}

// Schema types force `browser` to always be provided, but it may be omitted when the builder is invoked programmatically.
if (browser) {
// Use `browser` alone.
return { 'main': path.join(workspaceRoot, browser) };
} else if (!entryPoints) {
// Default browser entry if no explicit entry points
return { 'main': path.join(projectSourceRoot, 'main.ts') };
} else if (entryPoints instanceof Map) {
return Object.fromEntries(
Array.from(entryPoints.entries(), ([name, entryPoint]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@
}
},
"additionalProperties": false,
"required": ["browser", "tsConfig"],
"required": ["tsConfig"],
"definitions": {
"assetPattern": {
"oneOf": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,49 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
harness.expectFile('dist/browser/main.js').content.toContain('console.log("main")');
});

it('defaults to use `src/main.ts` if not present', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
browser: undefined,
});

const { result } = await harness.executeOnce();

expect(result?.success).toBe(true);

harness.expectFile('dist/browser/main.js').toExist();
harness.expectFile('dist/browser/index.html').toExist();
});

it('uses project source root in default if `browser` not present', async () => {
harness.useProject('test', {
root: '.',
sourceRoot: 'source',
cli: {
cache: {
enabled: false,
},
},
});
harness.useTarget('build', {
...BASE_OPTIONS,
browser: undefined,
index: false,
});

// Update app for a `source/main.ts` file based on the above changed `sourceRoot`
await harness.writeFile('source/main.ts', `console.log('main');`);
await harness.modifyFile('src/tsconfig.app.json', (content) =>
content.replace('main.ts', '../source/main.ts'),
);

const { result } = await harness.executeOnce();

expect(result?.success).toBe(true);

harness.expectFile('dist/browser/main.js').content.toContain('console.log("main")');
});

it('fails and shows an error when file does not exist', async () => {
harness.useTarget('build', {
...BASE_OPTIONS,
Expand Down
Loading