Skip to content

Commit 7f28049

Browse files
filipesilvahansl
authored andcommitted
feat(@angular/cli): add build defaults to config
Adds following defaults to `.angular-cli.json` under `defaults`: `sourcemaps`, `baseHref`, `progress`, `poll`, `deleteOutputPath`, `preserveSymlinks`, `showCircularDependencies`. They can be set via `ng set defaults.build.KEY = VALUE`. Also removes `apps.0.showCircularDependencies`. This is not a breaking chance since it was only added in 1.3.0-beta.0. Followup to #6884 (comment).
1 parent 0c3b4f6 commit 7f28049

File tree

7 files changed

+100
-75
lines changed

7 files changed

+100
-75
lines changed

packages/@angular/cli/commands/build.ts

+25-18
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import { oneLine } from 'common-tags';
55

66
const Command = require('../ember-cli/lib/models/command');
77

8+
89
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
9-
const pollDefault = config.config.defaults && config.config.defaults.poll;
10+
const buildConfigDefaults = config.getPaths('defaults.build', [
11+
'sourcemaps', 'baseHref', 'progress', 'poll', 'deleteOutputPath', 'preserveSymlinks',
12+
'showCircularDependencies'
13+
]);
1014

1115
// defaults for BuildOptions
1216
export const baseBuildCommandOptions: any = [
@@ -20,7 +24,7 @@ export const baseBuildCommandOptions: any = [
2024
{
2125
name: 'environment',
2226
type: String,
23-
aliases: ['e'] ,
27+
aliases: ['e'],
2428
description: 'Defines the build environment.'
2529
},
2630
{
@@ -38,7 +42,8 @@ export const baseBuildCommandOptions: any = [
3842
name: 'sourcemaps',
3943
type: Boolean,
4044
aliases: ['sm', 'sourcemap'],
41-
description: 'Output sourcemaps.'
45+
description: 'Output sourcemaps.',
46+
default: buildConfigDefaults['sourcemaps']
4247
},
4348
{
4449
name: 'vendor-chunk',
@@ -51,7 +56,8 @@ export const baseBuildCommandOptions: any = [
5156
name: 'base-href',
5257
type: String,
5358
aliases: ['bh'],
54-
description: 'Base url for the application being built.'
59+
description: 'Base url for the application being built.',
60+
default: buildConfigDefaults['base-href']
5561
},
5662
{
5763
name: 'deploy-url',
@@ -69,9 +75,9 @@ export const baseBuildCommandOptions: any = [
6975
{
7076
name: 'progress',
7177
type: Boolean,
72-
default: true,
7378
aliases: ['pr'],
74-
description: 'Log progress to the console while building.'
79+
description: 'Log progress to the console while building.',
80+
default: buildConfigDefaults['progress']
7581
},
7682
{
7783
name: 'i18n-file',
@@ -111,8 +117,8 @@ export const baseBuildCommandOptions: any = [
111117
{
112118
name: 'poll',
113119
type: Number,
114-
default: pollDefault,
115-
description: 'Enable and define the file watching poll time period (milliseconds).'
120+
description: 'Enable and define the file watching poll time period (milliseconds).',
121+
default: buildConfigDefaults['poll']
116122
},
117123
{
118124
name: 'app',
@@ -123,15 +129,15 @@ export const baseBuildCommandOptions: any = [
123129
{
124130
name: 'delete-output-path',
125131
type: Boolean,
126-
default: true,
127132
aliases: ['dop'],
128-
description: 'Delete output path before build.'
133+
description: 'Delete output path before build.',
134+
default: buildConfigDefaults['deleteOutputPath'],
129135
},
130136
{
131137
name: 'preserve-symlinks',
132138
type: Boolean,
133-
default: false,
134-
description: 'Do not use the real path when resolving modules.'
139+
description: 'Do not use the real path when resolving modules.',
140+
default: buildConfigDefaults['preserveSymlinks']
135141
},
136142
{
137143
name: 'extract-licenses',
@@ -143,7 +149,8 @@ export const baseBuildCommandOptions: any = [
143149
name: 'show-circular-dependencies',
144150
type: Boolean,
145151
aliases: ['scd'],
146-
description: 'Show circular dependency warnings on builds.'
152+
description: 'Show circular dependency warnings on builds.',
153+
default: buildConfigDefaults['showCircularDependencies']
147154
}
148155
];
149156

@@ -158,12 +165,12 @@ const BuildCommand = Command.extend({
158165

159166
availableOptions: baseBuildCommandOptions.concat([
160167
{
161-
name: 'stats-json',
162-
type: Boolean,
163-
default: false,
164-
description: oneLine`Generates a \`stats.json\` file which can be analyzed using tools
168+
name: 'stats-json',
169+
type: Boolean,
170+
default: false,
171+
description: oneLine`Generates a \`stats.json\` file which can be analyzed using tools
165172
such as: \`webpack-bundle-analyzer\` or https://webpack.github.io/analyse.`
166-
}
173+
}
167174
]),
168175

169176
run: function (commandOptions: BuildTaskOptions) {

packages/@angular/cli/commands/serve.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import { overrideOptions } from '../utilities/override-options';
99
const Command = require('../ember-cli/lib/models/command');
1010

1111
const config = CliConfig.fromProject() || CliConfig.fromGlobal();
12-
const defaultPort = process.env.PORT || config.get('defaults.serve.port');
13-
const defaultHost = config.get('defaults.serve.host');
14-
const defaultSsl = config.get('defaults.serve.ssl');
15-
const defaultSslKey = config.get('defaults.serve.sslKey');
16-
const defaultSslCert = config.get('defaults.serve.sslCert');
17-
const defaultProxyConfig = config.get('defaults.serve.proxyConfig');
12+
const serveConfigDefaults = config.getPaths('defaults.serve', [
13+
'port', 'host', 'ssl', 'sslKey', 'sslCert', 'proxyConfig'
14+
]);
15+
const defaultPort = process.env.PORT || serveConfigDefaults['port'];
1816

1917
export interface ServeTaskOptions extends BuildOptions {
2018
port?: number;
@@ -43,33 +41,33 @@ export const baseServeCommandOptions: any = overrideOptions([
4341
{
4442
name: 'host',
4543
type: String,
46-
default: defaultHost,
44+
default: serveConfigDefaults['host'],
4745
aliases: ['H'],
48-
description: `Listens only on ${defaultHost} by default.`
46+
description: `Listens only on ${serveConfigDefaults['host']} by default.`
4947
},
5048
{
5149
name: 'proxy-config',
5250
type: 'Path',
53-
default: defaultProxyConfig,
51+
default: serveConfigDefaults['proxyConfig'],
5452
aliases: ['pc'],
5553
description: 'Proxy configuration file.'
5654
},
5755
{
5856
name: 'ssl',
5957
type: Boolean,
60-
default: defaultSsl,
58+
default: serveConfigDefaults['ssl'],
6159
description: 'Serve using HTTPS.'
6260
},
6361
{
6462
name: 'ssl-key',
6563
type: String,
66-
default: defaultSslKey,
64+
default: serveConfigDefaults['sslKey'],
6765
description: 'SSL key to use for serving HTTPS.'
6866
},
6967
{
7068
name: 'ssl-cert',
7169
type: String,
72-
default: defaultSslCert,
70+
default: serveConfigDefaults['sslCert'],
7371
description: 'SSL certificate to use for serving HTTPS.'
7472
},
7573
{

packages/@angular/cli/lib/config/schema.json

+38-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@
118118
"type": "boolean",
119119
"default": false
120120
},
121-
"showCircularDependencies": {
122-
"description": "Show circular dependency warnings on builds.",
123-
"type": "boolean",
124-
"default": true
125-
},
126121
"styles": {
127122
"description": "Global styles to be included in the build.",
128123
"type": "array",
@@ -451,6 +446,44 @@
451446
}
452447
}
453448
},
449+
"build": {
450+
"description": "Properties to be passed to the build command.",
451+
"type": "object",
452+
"properties": {
453+
"sourcemaps": {
454+
"description": "Output sourcemaps.",
455+
"type": "boolean"
456+
},
457+
"baseHref": {
458+
"description": "Base url for the application being built.",
459+
"type": "string"
460+
},
461+
"progress": {
462+
"description": "The ssl key used by the server.",
463+
"type": "boolean",
464+
"default": true
465+
},
466+
"poll": {
467+
"description": "Enable and define the file watching poll time period (milliseconds).",
468+
"type": "number"
469+
},
470+
"deleteOutputPath": {
471+
"description": "Delete output path before build.",
472+
"type": "boolean",
473+
"default": true
474+
},
475+
"preserveSymlinks": {
476+
"description": "Do not use the real path when resolving modules.",
477+
"type": "boolean",
478+
"default": false
479+
},
480+
"showCircularDependencies": {
481+
"description": "Show circular dependency warnings on builds.",
482+
"type": "boolean",
483+
"default": true
484+
}
485+
}
486+
},
454487
"serve": {
455488
"description": "Properties to be passed to the serve command.",
456489
"type": "object",

packages/@angular/cli/models/config.ts

+19-37
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,7 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
6565

6666
const cliConfig = CliConfigBase.fromConfigPath<ConfigInterface>(globalConfigPath);
6767

68-
const aliases = [
69-
cliConfig.alias('apps.0.root', 'defaults.sourceDir'),
70-
cliConfig.alias('apps.0.prefix', 'defaults.prefix')
71-
];
72-
73-
// Additional aliases which do not emit any messages.
74-
cliConfig.alias('defaults.interface.prefix', 'defaults.inline.prefixInterfaces');
75-
cliConfig.alias('defaults.component.inlineStyle', 'defaults.inline.style');
76-
cliConfig.alias('defaults.component.inlineTemplate', 'defaults.inline.template');
77-
cliConfig.alias('defaults.component.spec', 'defaults.spec.component');
78-
cliConfig.alias('defaults.class.spec', 'defaults.spec.class');
79-
cliConfig.alias('defaults.component.directive', 'defaults.spec.directive');
80-
cliConfig.alias('defaults.component.module', 'defaults.spec.module');
81-
cliConfig.alias('defaults.component.pipe', 'defaults.spec.pipe');
82-
cliConfig.alias('defaults.component.service', 'defaults.spec.service');
83-
84-
// If any of them returned true, output a deprecation warning.
85-
if (aliases.some(x => x)) {
86-
console.error(chalk.yellow(oneLine`
87-
The "defaults.prefix" and "defaults.sourceDir" properties of .angular-cli.json
88-
are deprecated in favor of "apps[0].root" and "apps[0].prefix".\n
89-
Please update in order to avoid errors in future versions of Angular CLI.
90-
`));
91-
}
92-
68+
CliConfig.addAliases(cliConfig);
9369
configCacheMap.set(globalConfigPath, cliConfig);
9470
return cliConfig;
9571
}
@@ -108,11 +84,28 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
10884
const globalConfigPath = CliConfig.globalConfigFilePath();
10985
const cliConfig = CliConfigBase.fromConfigPath<ConfigInterface>(configPath, [globalConfigPath]);
11086

87+
CliConfig.addAliases(cliConfig);
88+
configCacheMap.set(configPath, cliConfig);
89+
return cliConfig as CliConfig;
90+
}
91+
92+
static addAliases(cliConfig: CliConfigBase<ConfigInterface>) {
93+
94+
// Aliases with deprecation messages.
11195
const aliases = [
11296
cliConfig.alias('apps.0.root', 'defaults.sourceDir'),
11397
cliConfig.alias('apps.0.prefix', 'defaults.prefix')
11498
];
11599

100+
// If any of them returned true, output a deprecation warning.
101+
if (aliases.some(x => x)) {
102+
console.error(chalk.yellow(oneLine`
103+
The "defaults.prefix" and "defaults.sourceDir" properties of .angular-cli.json
104+
are deprecated in favor of "apps[0].root" and "apps[0].prefix".\n
105+
Please update in order to avoid errors in future versions of Angular CLI.
106+
`));
107+
}
108+
116109
// Additional aliases which do not emit any messages.
117110
cliConfig.alias('defaults.interface.prefix', 'defaults.inline.prefixInterfaces');
118111
cliConfig.alias('defaults.component.inlineStyle', 'defaults.inline.style');
@@ -123,17 +116,6 @@ export class CliConfig extends CliConfigBase<ConfigInterface> {
123116
cliConfig.alias('defaults.component.module', 'defaults.spec.module');
124117
cliConfig.alias('defaults.component.pipe', 'defaults.spec.pipe');
125118
cliConfig.alias('defaults.component.service', 'defaults.spec.service');
126-
127-
// If any of them returned true, output a deprecation warning.
128-
if (aliases.some(x => x)) {
129-
console.error(chalk.yellow(oneLine`
130-
The "defaults.prefix" and "defaults.sourceDir" properties of .angular-cli.json
131-
are deprecated in favor of "apps[0].root" and "apps[0].prefix".\n
132-
Please update in order to avoid errors in future versions of Angular CLI.
133-
`));
134-
}
135-
136-
configCacheMap.set(configPath, cliConfig);
137-
return cliConfig as CliConfig;
119+
cliConfig.alias('defaults.build.poll', 'defaults.poll');
138120
}
139121
}

packages/@angular/cli/models/config/config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export class CliConfig<JsonType> {
6161
this._config.$$set(jsonPath, value);
6262
}
6363

64+
getPaths(baseJsonPath: string, keys: string[]) {
65+
const ret: { [k: string]: any } = {};
66+
keys.forEach(key => ret[key] = this.get(`${baseJsonPath}.${key}`));
67+
return ret;
68+
}
69+
6470
static fromJson<ConfigType>(content: ConfigType, ...global: ConfigType[]) {
6571
const schemaContent = fs.readFileSync(DEFAULT_CONFIG_SCHEMA_PATH, 'utf-8');
6672
let schema: Object;

packages/@angular/cli/models/webpack-config.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ export class NgCliWebpackConfig {
9898
const mergeableOptions = {
9999
outputPath: appConfig.outDir,
100100
deployUrl: appConfig.deployUrl,
101-
baseHref: appConfig.baseHref,
102-
showCircularDependencies: appConfig.showCircularDependencies
101+
baseHref: appConfig.baseHref
103102
};
104103

105104
return Object.assign({}, mergeableOptions, buildOptions);

tests/e2e/tests/misc/circular-dependency.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default async function () {
1010
throw new Error('Expected to have circular dependency warning in output.');
1111
}
1212

13-
await ng('set', 'apps.0.showCircularDependencies=false');
13+
await ng('set', 'defaults.build.showCircularDependencies=false');
1414
output = await ng('build');
1515
if (output.stdout.match(/WARNING in Circular dependency detected/)) {
1616
throw new Error('Expected to not have circular dependency warning in output.');

0 commit comments

Comments
 (0)