diff --git a/docs/documentation/angular-cli.md b/docs/documentation/angular-cli.md index 4adad010f34f..16f2f657c7f5 100644 --- a/docs/documentation/angular-cli.md +++ b/docs/documentation/angular-cli.md @@ -82,6 +82,7 @@ - *ssl* (`boolean`): Enables ssl for the application. Default is `false`. - *sslKey* (`string`): The ssl key used by the server. Default is `ssl/server.key`. - *sslCert* (`string`): The ssl certificate used by the server. Default is `ssl/server.crt`. + - *proxyConfig* (`string`): Proxy configuration file. - **packageManager** (`string`): Specify which package manager tool to use. Options include `npm`, `cnpm` and `yarn`. diff --git a/packages/@angular/cli/commands/serve.ts b/packages/@angular/cli/commands/serve.ts index 3d3169f2ce1d..d930b606b258 100644 --- a/packages/@angular/cli/commands/serve.ts +++ b/packages/@angular/cli/commands/serve.ts @@ -14,6 +14,7 @@ const defaultHost = config.get('defaults.serve.host'); const defaultSsl = config.get('defaults.serve.ssl'); const defaultSslKey = config.get('defaults.serve.sslKey'); const defaultSslCert = config.get('defaults.serve.sslCert'); +const defaultProxyConfig = config.get('defaults.serve.proxyConfig'); export interface ServeTaskOptions extends BuildOptions { port?: number; @@ -49,6 +50,7 @@ export const baseServeCommandOptions: any = overrideOptions([ { name: 'proxy-config', type: 'Path', + default: defaultProxyConfig, aliases: ['pc'], description: 'Proxy configuration file.' }, diff --git a/packages/@angular/cli/lib/config/schema.json b/packages/@angular/cli/lib/config/schema.json index a18bae04ae31..26b62e6ef766 100644 --- a/packages/@angular/cli/lib/config/schema.json +++ b/packages/@angular/cli/lib/config/schema.json @@ -473,6 +473,10 @@ "description": "The ssl certificate used by the server.", "type": "string", "default": "ssl/server.crt" + }, + "proxyConfig": { + "description": "Proxy configuration file.", + "type": "string" } } } diff --git a/tests/e2e/tests/misc/proxy.ts b/tests/e2e/tests/misc/proxy-config.ts similarity index 57% rename from tests/e2e/tests/misc/proxy.ts rename to tests/e2e/tests/misc/proxy-config.ts index 26e058c863b3..1a165276d441 100644 --- a/tests/e2e/tests/misc/proxy.ts +++ b/tests/e2e/tests/misc/proxy-config.ts @@ -5,8 +5,8 @@ import {writeFile} from '../../utils/fs'; import {request} from '../../utils/http'; import {killAllProcesses, ng} from '../../utils/process'; import {ngServe} from '../../utils/project'; -import {expectToFail} from '../../utils/utils'; - +import {updateJsonFile} from '../../utils/project'; +import {expectToFail} from "../../utils/utils"; export default function() { // Create an express app that serves as a proxy. @@ -31,16 +31,39 @@ export default function() { return Promise.resolve() .then(() => writeFile(proxyConfigFile, JSON.stringify(proxyConfig, null, 2))) - .then(() => ngServe('--proxy', proxyConfigFile)) + .then(() => ngServe('--proxy-config', proxyConfigFile)) + .then(() => request('http://localhost:4200/api/test')) + .then(body => { + if (!body.match(/TEST_API_RETURN/)) { + throw new Error('Response does not match expected value.'); + } + }) + .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }) + + .then(() => updateJsonFile('.angular-cli.json', configJson => { + const app = configJson.defaults; + app.serve = { + proxyConfig: proxyConfigFile + }; + })) + .then(() => ngServe()) .then(() => request('http://localhost:4200/api/test')) .then(body => { if (!body.match(/TEST_API_RETURN/)) { throw new Error('Response does not match expected value.'); } }) - .then(() => server.close(), (err) => { server.close(); throw err; }) .then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; }) + .then(() => server.close(), (err) => { server.close(); throw err; }) + // A non-existing proxy file should error. - .then(() => expectToFail(() => ng('serve', '--proxy', 'proxy.non-existent.json'))); + .then(() => expectToFail(() => ng('serve', '--proxy-config', 'proxy.non-existent.json'))) + .then(() => updateJsonFile('.angular-cli.json', configJson => { + const app = configJson.defaults; + app.serve = { + proxyConfig: 'proxy.non-existent.json' + }; + })) + .then(() => expectToFail(() => ng('serve'))); }