Skip to content

Commit 249abf0

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 a30127f commit 249abf0

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,15 @@ 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 =
376+
`${configuration ? `${configuration},` : ''}${commandOptions.configuration}`;
377+
}
373378
}
374379

375380
if (!project) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// extractCss defaults to false
11+
// sourceMap defaults to true
12+
appArchitect['options'] = {
13+
outputPath: 'dist/latest-project',
14+
index: 'src/index.html',
15+
main: 'src/main.ts',
16+
polyfills: 'src/polyfills.ts',
17+
tsConfig: 'tsconfig.app.json',
18+
assets: [
19+
'src/favicon.ico',
20+
'src/assets',
21+
],
22+
'styles': [
23+
'src/styles.css',
24+
],
25+
'scripts': [],
26+
};
27+
const browserConfigs = appArchitect['build'].configurations;
28+
browserConfigs['production'] = {
29+
extractCss: true,
30+
};
31+
browserConfigs['one'] = {
32+
assets: [],
33+
};
34+
browserConfigs['two'] = {
35+
sourceMap: false,
36+
};
37+
browserConfigs['three'] = {
38+
extractCss: false, // Defaults to false when not set.
39+
};
40+
});
41+
42+
// Test the base configuration.
43+
await ng('build');
44+
await expectFileToExist('dist/test-project/favicon.ico');
45+
await expectFileToExist('dist/test-project/main-es2015.js.map');
46+
await expectFileToExist('dist/test-project/styles-es2015.js');
47+
await expectFileToExist('dist/test-project/vendor-es2015.js');
48+
// Test that --prod extracts css.
49+
await ng('build', '--prod');
50+
await expectFileToExist('dist/test-project/styles.css');
51+
// But using a config overrides prod.
52+
await ng('build', '--prod', '--configuration=three');
53+
await expectFileToExist('dist/test-project/styles-es2015.js');
54+
await expectToFail(() => expectFileToExist('dist/test-project/styles.css'));
55+
// Use two configurations.
56+
await ng('build', '--configuration=one,two', '--vendor-chunk=false');
57+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
58+
await expectToFail(() => expectFileToExist('dist/test-project/main-es2015.js.map'));
59+
// Use two configurations and two overrides, one of which overrides a config.
60+
await ng('build', '--configuration=one,two', '--vendor-chunk=false', '--sourceMap=true');
61+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
62+
await expectFileToExist('dist/test-project/main-es2015.js.map');
63+
await expectToFail(() => expectFileToExist('dist/test-project/vendor-es2015.js'));
64+
// Use three configurations and a override, and prod at the end.
65+
await ng('build', '--configuration=one,two,three', '--vendor-chunk=false', '--prod');
66+
await expectToFail(() => expectFileToExist('dist/test-project/favicon.ico'));
67+
await expectToFail(() => expectFileToExist('dist/test-project/main-es2015.js.map'));
68+
await expectToFail(() => expectFileToExist('dist/test-project/vendor-es2015.js'));
69+
await expectFileToExist('dist/test-project/styles-es2015.js');
70+
await expectToFail(() => expectFileToExist('dist/test-project/styles.css'));
71+
}

0 commit comments

Comments
 (0)