Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit a0c3f5d

Browse files
jthoms1danbucholtz
authored andcommitted
fix(livereload): livereload now correctly serves cordova plugins on run and emulate.
1 parent 0821afd commit a0c3f5d

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/dev-server/http-server.ts

+46-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { injectLiveReloadScript } from './live-reload';
44
import * as express from 'express';
55
import * as fs from 'fs';
66
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';
814
import { Logger } from '../logger/logger';
915
import * as proxyMiddleware from 'proxy-middleware';
1016
import { 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+
}

src/dev-server/serve-config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'path';
2+
13
export interface ServeConfig {
24
httpPort: number;
35
host: string;
@@ -16,3 +18,6 @@ export interface ServeConfig {
1618
}
1719
export const LOGGER_DIR = '__ion-dev-server';
1820
export const IONIC_LAB_URL = '/ionic-lab';
21+
22+
export const IOS_PLATFORM_PATH = path.join('platforms', 'ios', 'www');
23+
export const ANDROID_PLATFORM_PATH = path.join('platforms', 'android', 'assets', 'www');

0 commit comments

Comments
 (0)