Skip to content

Commit

Permalink
feat(@angular-devkit/architect): add implementation for defaultConfig…
Browse files Browse the repository at this point in the history
…uration

With this change, the architect can be configured to use a default configuration when it's not provided as part of the target.

Consider the below, where `defaultConfiguration` is configured to `production`. Running `ng build` will be invoked with "production" configuration.

```js
"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "defaultConfiguration": "production",
  "options": {
      ...
  },
  "configurations": {
    "production": {
        ...
    }
  }
}
```
  • Loading branch information
alan-agius4 authored and filipesilva committed Mar 8, 2021
1 parent f7e3e23 commit 1da359a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface WorkspaceHost {
getMetadata(project: string): Promise<json.JsonObject>;
getOptions(project: string, target: string, configuration?: string): Promise<json.JsonObject>;
hasTarget(project: string, target: string): Promise<boolean>;
getDefaultConfigurationName(project: string, target: string): Promise<string | undefined>;
}

function findProjectTarget(
Expand Down Expand Up @@ -99,6 +100,9 @@ export class WorkspaceNodeModulesArchitectHost implements ArchitectHost<NodeModu
async hasTarget(project, target) {
return !!workspaceOrHost.projects.get(project)?.targets.has(target);
},
async getDefaultConfigurationName(project, target) {
return workspaceOrHost.projects.get(project)?.targets.get(target)?.defaultConfiguration;
},
};
}
}
Expand Down Expand Up @@ -166,9 +170,11 @@ export class WorkspaceNodeModulesArchitectHost implements ArchitectHost<NodeModu
}

let options = await this.workspaceHost.getOptions(target.project, target.target);
const targetConfiguration =
target.configuration || await this.workspaceHost.getDefaultConfigurationName(target.project, target.target);

if (target.configuration) {
const configurations = target.configuration.split(',').map((c) => c.trim());
if (targetConfiguration) {
const configurations = targetConfiguration.split(',').map((c) => c.trim());
for (const configuration of configurations) {
options = {
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ export class BuilderHarness<T> {

return this.targetName === target || this.builderTargets.has(target);
},
getDefaultConfigurationName: async (_project, _target) => {
return undefined;
},
validate: async (options, builderName) => {
let schema;
if (builderName === this.builderInfo.builderName) {
Expand Down
35 changes: 35 additions & 0 deletions tests/legacy-cli/e2e/tests/misc/target-default-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expectFileToExist } from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { expectToFail } from '../../utils/utils';

export default async function () {
await updateJsonFile('angular.json', workspace => {
const build = workspace.projects['test-project'].architect.build;
build.defaultConfiguration = undefined;
build.options = {
...build.options,
optimization: false,
buildOptimizer: false,
outputHashing: 'none',
sourceMap: true,
};
});

await ng('build');
await expectFileToExist('dist/test-project/main.js');
await expectFileToExist('dist/test-project/main.js.map');

// Add new configuration and set "defaultConfiguration"
await updateJsonFile('angular.json', workspace => {
const build = workspace.projects['test-project'].architect.build;
build.defaultConfiguration = 'defaultConfiguration';
build.configurations.defaultConfiguration = {
sourceMap: false,
};
});

await ng('build');
await expectFileToExist('dist/test-project/main.js');
await expectToFail(() => expectFileToExist('dist/test-project/main.js.map'));
}

0 comments on commit 1da359a

Please sign in to comment.