Skip to content

Commit 45e2985

Browse files
authored
fix(serve): fallback to config.app[0].index (#3813)
Fix #3748
1 parent 39b9522 commit 45e2985

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

packages/angular-cli/tasks/serve-webpack.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export default Task.extend({
1919
const ui = this.ui;
2020

2121
let webpackCompiler: any;
22+
const projectConfig = CliConfig.fromProject().config;
23+
const appConfig = projectConfig.apps[0];
2224

2325
let config = new NgCliWebpackConfig(
2426
this.project,
@@ -84,12 +86,10 @@ export default Task.extend({
8486
}
8587

8688
const webpackDevServerConfiguration: IWebpackDevServerConfigurationOptions = {
87-
contentBase: path.resolve(
88-
this.project.root,
89-
`./${CliConfig.fromProject().config.apps[0].root}`
90-
),
89+
contentBase: path.join(this.project.root, `./${appConfig.root}`),
9190
headers: { 'Access-Control-Allow-Origin': '*' },
9291
historyApiFallback: {
92+
index: `/${appConfig.index}`,
9393
disableDotRule: true,
9494
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']
9595
},
@@ -98,8 +98,7 @@ export default Task.extend({
9898
proxy: proxyConfig,
9999
compress: serveTaskOptions.target === 'production',
100100
watchOptions: {
101-
poll: CliConfig.fromProject().config.defaults &&
102-
CliConfig.fromProject().config.defaults.poll
101+
poll: projectConfig.defaults && projectConfig.defaults.poll
103102
},
104103
https: serveTaskOptions.ssl
105104
};

tests/e2e/tests/misc/fallback.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { request } from '../../utils/http';
2+
import { killAllProcesses } from '../../utils/process';
3+
import { ngServe } from '../../utils/project';
4+
import { updateJsonFile } from '../../utils/project';
5+
import { moveFile } from '../../utils/fs';
6+
7+
8+
export default function () {
9+
// should fallback to config.app[0].index (index.html by default)
10+
return Promise.resolve()
11+
.then(() => ngServe())
12+
.then(() => request('http://localhost:4200/'))
13+
.then(body => {
14+
if (!body.match(/<app-root>Loading...<\/app-root>/)) {
15+
throw new Error('Response does not match expected value.');
16+
}
17+
})
18+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; })
19+
// should correctly fallback to a changed index
20+
.then(() => moveFile('src/index.html', 'src/not-index.html'))
21+
.then(() => updateJsonFile('angular-cli.json', configJson => {
22+
const app = configJson['apps'][0];
23+
app['index'] = 'not-index.html';
24+
}))
25+
.then(() => ngServe())
26+
.then(() => request('http://localhost:4200/'))
27+
.then(body => {
28+
if (!body.match(/<app-root>Loading...<\/app-root>/)) {
29+
throw new Error('Response does not match expected value.');
30+
}
31+
})
32+
.then(() => killAllProcesses(), (err) => { killAllProcesses(); throw err; });
33+
}

tests/e2e/utils/http.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import * as _request from 'request';
44

55
export function request(url: string): Promise<string> {
66
return new Promise((resolve, reject) => {
7-
let options = { url: url, agentOptions: { rejectUnauthorized: false }};
7+
let options = {
8+
url: url,
9+
headers: { 'Accept': 'text/html' },
10+
agentOptions: { rejectUnauthorized: false }
11+
};
812
_request(options, (error: any, response: IncomingMessage, body: string) => {
913
if (error) {
1014
reject(error);

0 commit comments

Comments
 (0)