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