Skip to content

Commit a5653c3

Browse files
committed
feat(@angular/cli): support using multiple configurations
It's now possible to use multiple configurations by separating them with a comma: ``` ng build --configuration=one,two,three ``` They will be applied from left to right. If `--prod` is also present, it will be considered to be the first configuration and thus able to be overriden. You can also use it in target strings: ``` "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "latest-project:build:one,two" }, "configurations": { "production": { "browserTarget": "latest-project:build:production,one,two" } } ``` Fix angular#10612
1 parent b4fabc8 commit a5653c3

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

packages/angular/cli/models/architect-command.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,14 @@ export abstract class ArchitectCommand<
366366
} else {
367367
project = commandOptions.project;
368368
target = this.target;
369-
configuration = commandOptions.configuration;
370-
if (!configuration && commandOptions.prod) {
369+
if (commandOptions.prod) {
370+
// The --prod flag will always be the first configuration, available to be overwritten
371+
// by following configurations.
371372
configuration = 'production';
372373
}
374+
if (commandOptions.configuration) {
375+
configuration += `${configuration ? ',' : ''}${commandOptions.configuration}`;
376+
}
373377
}
374378

375379
if (!project) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expectFileToExist } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
3+
import { updateJsonFile } from '../../utils/project';
4+
import { expectToFail } from '../../utils/utils';
5+
6+
export default async function () {
7+
await updateJsonFile('angular.json', workspaceJson => {
8+
const appArchitect = workspaceJson.projects['test-project'].architect;
9+
// These are the default options, that we'll overwrite in subsequent configs.
10+
appArchitect['options'] = {
11+
outputPath: 'dist/latest-project',
12+
index: 'src/index.html',
13+
main: 'src/main.ts',
14+
polyfills: 'src/polyfills.ts',
15+
tsConfig: 'tsconfig.app.json',
16+
assets: [
17+
'src/favicon.ico',
18+
'src/assets',
19+
],
20+
'styles': [
21+
'src/styles.css',
22+
],
23+
'scripts': [],
24+
};
25+
const browserConfigs = appArchitect['build'].configurations;
26+
browserConfigs['production'] = {
27+
extractCss: true,
28+
};
29+
browserConfigs['one'] = {
30+
assets: [],
31+
};
32+
browserConfigs['two'] = {
33+
polyfills: undefined,
34+
};
35+
browserConfigs['three'] = {
36+
extractCss: false, // Defaults to false when not set.
37+
};
38+
});
39+
40+
// Test the base configuration.
41+
await ng('build');
42+
await expectFileToExist('dist/test-project/favicon.ico');
43+
await expectFileToExist('dist/test-project/polyfills-es2015.js');
44+
await expectFileToExist('dist/test-project/styles-es2015.js');
45+
await expectFileToExist('dist/test-project/vendor-es2015.js');
46+
// Test that --prod extracts css.
47+
await ng('build', '--prod');
48+
await expectFileToExist('dist/test-project/styles.css');
49+
// But using a config overrides prod.
50+
await ng('build', '--prod', '--configuration=three');
51+
await expectFileToExist('dist/test-project/styles-es2015.js');
52+
await expectToFail(() => expectFileToExist('dist/test-project/styles.css'));
53+
// Use two configurations.
54+
await ng('build', '--configuration=one,two', '--vendor-chunk=false');
55+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
56+
await expectToFail(() => expectFileToExist('dist/test-project/polyfills-es2015.js'));
57+
// Use two configurations and two overrides, one of which overrides a config.
58+
await ng('build', '--configuration=one,two', '--vendor-chunk=false', '--polyfills=src/polyfills.ts');
59+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
60+
await expectFileToExist('dist/test-project/polyfills-es2015.js');
61+
await expectToFail(() => expectFileToExist('dist/test-project/vendor-es2015.js'));
62+
// Use two configurations and a override, and prod at the end.
63+
await ng('build', '--configuration=one,two', '--vendor-chunk=false', '--prod');
64+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
65+
await expectToFail(() => expectFileToExist('dist/test-project/polyfills-es2015.js'));
66+
await expectToFail(() => expectFileToExist('dist/test-project/vendor-es2015.js'));
67+
await expectFileToExist('dist/test-project/styles-es2015.js');
68+
await expectToFail(() => expectFileToExist('dist/test-project/styles.css'));
69+
}

0 commit comments

Comments
 (0)