@@ -4,7 +4,13 @@ import { injectLiveReloadScript } from './live-reload';
44import * as express from 'express' ;
55import * as fs from 'fs' ;
66import * 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' ;
814import { Logger } from '../logger/logger' ;
915import * as proxyMiddleware from 'proxy-middleware' ;
1016import { injectDiagnosticsHtml } from '../logger/logger-diagnostics' ;
@@ -27,7 +33,9 @@ export function createHttpServer(config: ServeConfig): express.Application {
2733 app . use ( `/${ LOGGER_DIR } ` , express . static ( path . join ( __dirname , '..' , '..' , 'bin' ) , { maxAge : 31536000 } ) ) ;
2834 app . get ( IONIC_LAB_URL , ( req , res ) =>
2935 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 ) ;
3139
3240 if ( config . useProxy ) {
3341 setupProxies ( app ) ;
@@ -40,7 +48,7 @@ function setupProxies(app: express.Application) {
4048
4149 getProjectJson ( ) . then ( function ( projectConfig : IonicProject ) {
4250 for ( const proxy of projectConfig . proxies || [ ] ) {
43- var opts : any = url . parse ( proxy . proxyUrl ) ;
51+ let opts : any = url . parse ( proxy . proxyUrl ) ;
4452 if ( proxy . proxyNoAgent ) {
4553 opts . agent = false ;
4654 }
@@ -78,7 +86,41 @@ function serveIndex(req: express.Request, res: express.Response) {
7886/**
7987 * http responder for cordova.js file
8088 */
81- function serveCordovaJS ( req : express . Request , res : express . Response ) {
89+ function serveMockCordovaJS ( req : express . Request , res : express . Response ) {
8290 res . set ( 'Content-Type' , 'application/javascript' ) ;
8391 res . send ( '// mock cordova file during development' ) ;
8492}
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