@@ -17,6 +17,39 @@ const SilentError = require('silent-error');
17
17
const opn = require ( 'opn' ) ;
18
18
const yellow = require ( 'chalk' ) . yellow ;
19
19
20
+ function findDefaultServePath ( baseHref : string , deployUrl : string ) : string | null {
21
+ if ( ! baseHref && ! deployUrl ) {
22
+ return '' ;
23
+ }
24
+
25
+ if ( / ^ ( \w + : ) ? \/ \/ / . test ( baseHref ) || / ^ ( \w + : ) ? \/ \/ / . test ( deployUrl ) ) {
26
+ // If baseHref or deployUrl is absolute, unsupported by ng serve
27
+ return null ;
28
+ }
29
+
30
+ // normalize baseHref
31
+ // for ng serve the starting base is always `/` so a relative
32
+ // and root relative value are identical
33
+ const baseHrefParts = ( baseHref || '' )
34
+ . split ( '/' )
35
+ . filter ( part => part !== '' ) ;
36
+ if ( baseHref && ! baseHref . endsWith ( '/' ) ) {
37
+ baseHrefParts . pop ( ) ;
38
+ }
39
+ const normalizedBaseHref = baseHrefParts . length === 0 ? '/' : `/${ baseHrefParts . join ( '/' ) } /` ;
40
+
41
+ if ( deployUrl && deployUrl [ 0 ] === '/' ) {
42
+ if ( baseHref && baseHref [ 0 ] === '/' && normalizedBaseHref !== deployUrl ) {
43
+ // If baseHref and deployUrl are root relative and not equivalent, unsupported by ng serve
44
+ return null ;
45
+ }
46
+ return deployUrl ;
47
+ }
48
+
49
+ // Join together baseHref and deployUrl
50
+ return `${ normalizedBaseHref } ${ deployUrl || '' } ` ;
51
+ }
52
+
20
53
export default Task . extend ( {
21
54
run : function ( serveTaskOptions : ServeTaskOptions , rebuildDoneCb : any ) {
22
55
const ui = this . ui ;
@@ -156,10 +189,29 @@ export default Task.extend({
156
189
}
157
190
}
158
191
192
+ let servePath = serveTaskOptions . servePath ;
193
+ if ( ! servePath && servePath !== '' ) {
194
+ const defaultServePath =
195
+ findDefaultServePath ( serveTaskOptions . baseHref , serveTaskOptions . deployUrl ) ;
196
+ if ( defaultServePath == null ) {
197
+ ui . writeLine ( oneLine `
198
+ ${ chalk . yellow ( 'WARNING' ) } --deploy-url and/or --base-href contain
199
+ unsupported values for ng serve. Default serve path of '/' used.
200
+ Use --serve-path to override.
201
+ ` ) ;
202
+ }
203
+ servePath = defaultServePath || '' ;
204
+ }
205
+ if ( servePath . endsWith ( '/' ) ) {
206
+ servePath = servePath . substr ( 0 , servePath . length - 1 ) ;
207
+ }
208
+ if ( ! servePath . startsWith ( '/' ) ) {
209
+ servePath = `/${ servePath } ` ;
210
+ }
159
211
const webpackDevServerConfiguration : IWebpackDevServerConfigurationOptions = {
160
212
headers : { 'Access-Control-Allow-Origin' : '*' } ,
161
213
historyApiFallback : {
162
- index : `/${ appConfig . index } ` ,
214
+ index : `${ servePath } /${ appConfig . index } ` ,
163
215
disableDotRule : true ,
164
216
htmlAcceptHeaders : [ 'text/html' , 'application/xhtml+xml' ]
165
217
} ,
@@ -177,7 +229,8 @@ export default Task.extend({
177
229
} ,
178
230
contentBase : false ,
179
231
public : serveTaskOptions . publicHost ,
180
- disableHostCheck : serveTaskOptions . disableHostCheck
232
+ disableHostCheck : serveTaskOptions . disableHostCheck ,
233
+ publicPath : servePath
181
234
} ;
182
235
183
236
if ( sslKey != null && sslCert != null ) {
@@ -187,13 +240,6 @@ export default Task.extend({
187
240
188
241
webpackDevServerConfiguration . hot = serveTaskOptions . hmr ;
189
242
190
- // set publicPath property to be sent on webpack server config
191
- if ( serveTaskOptions . deployUrl ) {
192
- webpackDevServerConfiguration . publicPath = serveTaskOptions . deployUrl ;
193
- ( webpackDevServerConfiguration . historyApiFallback as any ) . index =
194
- serveTaskOptions . deployUrl + `/${ appConfig . index } ` ;
195
- }
196
-
197
243
if ( serveTaskOptions . target === 'production' ) {
198
244
ui . writeLine ( chalk . red ( stripIndents `
199
245
****************************************************************************************
@@ -208,7 +254,7 @@ export default Task.extend({
208
254
ui . writeLine ( chalk . green ( oneLine `
209
255
**
210
256
NG Live Development Server is listening on ${ serveTaskOptions . host } :${ serveTaskOptions . port } ,
211
- open your browser on ${ serverAddress }
257
+ open your browser on ${ serverAddress } ${ servePath }
212
258
**
213
259
` ) ) ;
214
260
0 commit comments