1- import * as fs from 'fs' ;
21import * as path from 'path' ;
32
43import * as chalk from 'chalk' ;
54
65import * as expressType from 'express' ;
76
8- import { IonicEnvironment , ServeDetails , ServeOptions } from '../../definitions' ;
7+ import { IonicEnvironment , LiveReloadFunction , ServeDetails , ServeOptions } from '../../definitions' ;
98
109import { BIND_ALL_ADDRESS , IONIC_LAB_URL , LOCAL_ADDRESSES } from '../serve' ;
1110import { FatalException } from '../errors' ;
11+ import { fsReadFile , pathExists } from '../utils/fs' ;
1212
1313const WATCH_PATTERNS = [
1414 'www/**/*' ,
@@ -99,7 +99,13 @@ export async function serve({ env, options }: { env: IonicEnvironment; options:
9999}
100100
101101async function setupServer ( env : IonicEnvironment , options : ServeMetaOptions ) : Promise < ServeMetaOptions > {
102- const liveReloadBrowser = await createLiveReloadServer ( env , options ) ;
102+ let reloadfn : LiveReloadFunction | undefined ;
103+
104+ if ( options . livereload ) {
105+ const { createLiveReloadServer } = await import ( '../livereload' ) ;
106+ reloadfn = await createLiveReloadServer ( env , { port : options . livereloadPort , wwwDir : options . wwwDir } ) ;
107+ }
108+
103109 await createHttpServer ( env , options ) ;
104110
105111 const chokidar = await import ( 'chokidar' ) ;
@@ -116,7 +122,11 @@ async function setupServer(env: IonicEnvironment, options: ServeMetaOptions): Pr
116122
117123 watcher . on ( 'change' , ( filePath : string ) => {
118124 env . log . info ( `[${ new Date ( ) . toTimeString ( ) . slice ( 0 , 8 ) } ] ${ chalk . bold ( filePath ) } changed` ) ;
119- liveReloadBrowser ( [ filePath ] ) ;
125+
126+ if ( reloadfn ) {
127+ reloadfn ( [ filePath ] ) ;
128+ }
129+
120130 env . events . emit ( 'watch:change' , filePath ) ;
121131 } ) ;
122132
@@ -127,54 +137,6 @@ async function setupServer(env: IonicEnvironment, options: ServeMetaOptions): Pr
127137 return options ;
128138}
129139
130- async function createLiveReloadServer ( env : IonicEnvironment , options : ServeMetaOptions ) : Promise < ( changedFile : string [ ] ) => void > {
131- const tinylr = await import ( 'tiny-lr' ) ;
132- const liveReloadServer = tinylr ( ) ;
133- liveReloadServer . listen ( options . livereloadPort ) ;
134-
135- return ( changedFiles ) => {
136- liveReloadServer . changed ( {
137- body : {
138- files : changedFiles . map ( changedFile => (
139- '/' + path . relative ( options . wwwDir , changedFile )
140- ) )
141- }
142- } ) ;
143- } ;
144- }
145-
146- export function injectLiveReloadScript ( content : any , host : string , port : number ) : any {
147- let contentStr = content . toString ( ) ;
148- const liveReloadScript = getLiveReloadScript ( host , port ) ;
149-
150- if ( contentStr . indexOf ( '/livereload.js' ) > - 1 ) {
151- // already added script
152- return content ;
153- }
154-
155- let match = contentStr . match ( / < \/ b o d y > (? ! [ \s \S ] * < \/ b o d y > ) / i) ;
156- if ( ! match ) {
157- match = contentStr . match ( / < \/ h t m l > (? ! [ \s \S ] * < \/ h t m l > ) / i) ;
158- }
159- if ( match ) {
160- contentStr = contentStr . replace ( match [ 0 ] , `${ liveReloadScript } \n${ match [ 0 ] } ` ) ;
161- } else {
162- contentStr += liveReloadScript ;
163- }
164-
165- return contentStr ;
166- }
167-
168- function getLiveReloadScript ( host : string , port : number ) {
169- if ( host === BIND_ALL_ADDRESS ) {
170- host = 'localhost' ;
171- }
172- const src = `//${ host } :${ port } /livereload.js?snipver=1` ;
173-
174- return ` <!-- Ionic Dev Server: Injected LiveReload Script -->\n` +
175- ` <script src="${ src } " async="" defer=""></script>` ;
176- }
177-
178140/**
179141 * Create HTTP server
180142 */
@@ -186,23 +148,24 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
186148 /**
187149 * http responder for /index.html base entrypoint
188150 */
189- const serveIndex = ( req : expressType . Request , res : expressType . Response ) => {
151+ const serveIndex = async ( req : expressType . Request , res : expressType . Response ) => {
190152 // respond with the index.html file
191153 const indexFileName = path . join ( options . wwwDir , 'index.html' ) ;
192- fs . readFile ( indexFileName , ( err , indexHtml ) => {
193- if ( ! options . nolivereload ) {
194- indexHtml = injectLiveReloadScript ( indexHtml , options . externalIP , options . livereloadPort ) ;
195- }
196-
197- res . set ( 'Content-Type' , 'text/html' ) ;
198- res . send ( indexHtml ) ;
199- } ) ;
154+ let indexHtml = await fsReadFile ( indexFileName , { encoding : 'utf8' } ) ;
155+
156+ if ( options . livereload ) {
157+ const { injectLiveReloadScript } = await import ( '../livereload' ) ;
158+ indexHtml = injectLiveReloadScript ( indexHtml , options . externalIP , options . livereloadPort ) ;
159+ }
160+
161+ res . set ( 'Content-Type' , 'text/html' ) ;
162+ res . send ( indexHtml ) ;
200163 } ;
201164
202165 /**
203166 * Middleware to serve platform resources
204167 */
205- const servePlatformResource = ( req : expressType . Request , res : expressType . Response , next : expressType . NextFunction ) => {
168+ const servePlatformResource = async ( req : expressType . Request , res : expressType . Response , next : expressType . NextFunction ) => {
206169 const userAgent = req . header ( 'user-agent' ) || '' ;
207170 let resourcePath = options . wwwDir ;
208171
@@ -216,12 +179,11 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
216179 resourcePath = path . join ( env . project . directory , ANDROID_PLATFORM_PATH ) ;
217180 }
218181
219- fs . stat ( path . join ( resourcePath , req . url ) , ( err , stats ) => {
220- if ( err ) {
221- return next ( ) ;
222- }
182+ if ( await pathExists ( path . join ( resourcePath , req . url ) ) ) {
223183 res . sendFile ( req . url , { root : resourcePath } ) ;
224- } ) ;
184+ } else {
185+ next ( ) ;
186+ }
225187 } ;
226188
227189 app . get ( '/' , serveIndex ) ;
@@ -244,7 +206,7 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
244206 app . get ( '/cordova_plugins.js' , servePlatformResource ) ;
245207 app . get ( '/plugins/*' , servePlatformResource ) ;
246208
247- if ( ! options . noproxy ) {
209+ if ( options . proxy ) {
248210 await setupProxies ( env , app ) ;
249211 }
250212
0 commit comments