@@ -4,7 +4,13 @@ import { injectLiveReloadScript } from './live-reload';
4
4
import * as express from 'express' ;
5
5
import * as fs from 'fs' ;
6
6
import * as url from 'url' ;
7
- import { ServeConfig , LOGGER_DIR , IONIC_LAB_URL } from './serve-config' ;
7
+ import {
8
+ ServeConfig ,
9
+ LOGGER_DIR ,
10
+ IONIC_LAB_URL ,
11
+ IOS_PLATFORM_PATH ,
12
+ ANDROID_PLATFORM_PATH
13
+ } from './serve-config' ;
8
14
import { Logger } from '../logger/logger' ;
9
15
import * as proxyMiddleware from 'proxy-middleware' ;
10
16
import { injectDiagnosticsHtml } from '../logger/logger-diagnostics' ;
@@ -27,7 +33,9 @@ export function createHttpServer(config: ServeConfig): express.Application {
27
33
app . use ( `/${ LOGGER_DIR } ` , express . static ( path . join ( __dirname , '..' , '..' , 'bin' ) , { maxAge : 31536000 } ) ) ;
28
34
app . get ( IONIC_LAB_URL , ( req , res ) =>
29
35
res . sendFile ( 'ionic-lab.html' , { root : path . join ( __dirname , '..' , '..' , 'bin' ) } ) ) ;
30
- app . get ( '/cordova.js' , serveCordovaJS ) ;
36
+ app . get ( '/cordova.js' , servePlatformResource , serveMockCordovaJS ) ;
37
+ app . get ( '/cordova_plugins.js' , servePlatformResource ) ;
38
+ app . get ( '/plugins/*' , servePlatformResource ) ;
31
39
32
40
if ( config . useProxy ) {
33
41
setupProxies ( app ) ;
@@ -40,7 +48,7 @@ function setupProxies(app: express.Application) {
40
48
41
49
getProjectJson ( ) . then ( function ( projectConfig : IonicProject ) {
42
50
for ( const proxy of projectConfig . proxies || [ ] ) {
43
- var opts : any = url . parse ( proxy . proxyUrl ) ;
51
+ let opts : any = url . parse ( proxy . proxyUrl ) ;
44
52
if ( proxy . proxyNoAgent ) {
45
53
opts . agent = false ;
46
54
}
@@ -78,7 +86,41 @@ function serveIndex(req: express.Request, res: express.Response) {
78
86
/**
79
87
* http responder for cordova.js file
80
88
*/
81
- function serveCordovaJS ( req : express . Request , res : express . Response ) {
89
+ function serveMockCordovaJS ( req : express . Request , res : express . Response ) {
82
90
res . set ( 'Content-Type' , 'application/javascript' ) ;
83
91
res . send ( '// mock cordova file during development' ) ;
84
92
}
93
+
94
+ /**
95
+ * Middleware to serve platform resources
96
+ */
97
+ function servePlatformResource ( req : express . Request , res : express . Response , next : express . NextFunction ) {
98
+ const config : ServeConfig = req . app . get ( 'serveConfig' ) ;
99
+ const userAgent = req . header ( 'user-agent' ) ;
100
+ let resourcePath = config . wwwDir ;
101
+
102
+ if ( isUserAgentIOS ( userAgent ) ) {
103
+ resourcePath = path . join ( config . rootDir , IOS_PLATFORM_PATH ) ;
104
+ } else if ( isUserAgentAndroid ( userAgent ) ) {
105
+ resourcePath = path . join ( config . rootDir , ANDROID_PLATFORM_PATH ) ;
106
+ }
107
+
108
+ fs . stat ( path . join ( resourcePath , req . url ) , ( err , stats ) => {
109
+ if ( err ) {
110
+ return next ( ) ;
111
+ }
112
+ res . sendFile ( req . url , { root : resourcePath } ) ;
113
+ } ) ;
114
+ }
115
+
116
+
117
+
118
+ function isUserAgentIOS ( ua : string ) {
119
+ ua = ua . toLowerCase ( ) ;
120
+ return ( ua . indexOf ( 'iphone' ) > - 1 || ua . indexOf ( 'ipad' ) > - 1 || ua . indexOf ( 'ipod' ) > - 1 ) ;
121
+ }
122
+
123
+ function isUserAgentAndroid ( ua : string ) {
124
+ ua = ua . toLowerCase ( ) ;
125
+ return ua . indexOf ( 'android' ) > - 1 ;
126
+ }
0 commit comments