Skip to content

Commit f31e79d

Browse files
committed
fix(serve): fix --livereload for device/emulator
Add logic for Cordova assets: cordova.js, cordova_plugins.js, and Cordova plugin assets. fixes #3461
1 parent bf3e775 commit f31e79d

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

packages/@ionic/cli-utils/src/lib/project/ionic1/serve.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,24 @@ class Ionic1ServeCLI extends ServeCLI<Ionic1ServeOptions> {
121121
protected async buildArgs(options: Ionic1ServeOptions): Promise<string[]> {
122122
const { pkgManagerArgs } = await import('../../utils/npm');
123123

124-
const args = ['--host', options.address, '--port', String(options.port), '--livereload-port', String(options.livereloadPort), '--dev-port', String(options.notificationPort)];
125-
126-
if (this.resolvedProgram === this.program) {
127-
const v1utilArgs = ['serve'];
124+
const args = [
125+
`--host=${options.address}`,
126+
`--port=${String(options.port)}`,
127+
`--livereload-port=${String(options.livereloadPort)}`,
128+
`--dev-port=${String(options.notificationPort)}`,
129+
`--engine=${options.engine}`,
130+
];
131+
132+
if (options.platform) {
133+
args.push(`--platform=${options.platform}`);
134+
}
128135

129-
if (options.consolelogs) {
130-
v1utilArgs.push('-c');
131-
}
136+
if (options.consolelogs) {
137+
args.push('-c');
138+
}
132139

133-
return [...v1utilArgs, ...args];
140+
if (this.resolvedProgram === this.program) {
141+
return ['serve', ...args];
134142
} else {
135143
const [ , ...pkgArgs ] = await pkgManagerArgs(this.e.config.get('npmClient'), { command: 'run', script: this.script, scriptArgs: [...args] });
136144
return pkgArgs;

packages/@ionic/v1-toolkit/src/commands/serve.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ export class ServeCommand extends Command {
4848
type: Boolean,
4949
aliases: ['c'],
5050
},
51+
{
52+
name: 'engine',
53+
summary: `Target engine (e.g. ${['browser', 'cordova'].map(e => chalk.green(e)).join(', ')})`,
54+
default: 'browser',
55+
},
56+
{
57+
name: 'platform',
58+
summary: `Target platform on chosen engine (e.g. ${['ios', 'android'].map(e => chalk.green(e)).join(', ')})`,
59+
},
5160
],
5261
};
5362
}
@@ -59,6 +68,8 @@ export class ServeCommand extends Command {
5968
const livereload = options['livereload'] ? true : false;
6069
const livereloadPort = str2num(options['livereload-port']);
6170
const consolelogs = options['consolelogs'] ? true : false;
71+
const engine = String(options['engine']);
72+
const platform = options['platform'] ? String(options['platform']) : undefined;
6273

6374
const url = `http://${host}:${port}`;
6475

@@ -79,7 +90,19 @@ export class ServeCommand extends Command {
7990

8091
process.stdout.write(`${timestamp()} Serving directory ${chalk.bold(wwwDir)}\n`);
8192

82-
await runServer({ host, port, livereload, consolelogs, devPort, livereloadPort, wwwDir, watchPatterns: c.watchPatterns, proxies });
93+
await runServer({
94+
host,
95+
port,
96+
engine,
97+
platform,
98+
livereload,
99+
consolelogs,
100+
devPort,
101+
livereloadPort,
102+
wwwDir,
103+
watchPatterns: c.watchPatterns,
104+
proxies,
105+
});
83106

84107
process.stdout.write(`${timestamp()} Dev server running at ${chalk.bold(url)}\n`);
85108
}

packages/@ionic/v1-toolkit/src/lib/serve.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'path';
22

3-
import { fsReadFile } from '@ionic/cli-framework/utils/fs';
3+
import { fsReadFile, pathExists } from '@ionic/cli-framework/utils/fs';
44
import chalk from 'chalk';
55

66
import * as ζexpress from 'express';
@@ -47,6 +47,8 @@ export interface ServeOptions {
4747
devPort: number;
4848
livereloadPort: number;
4949
wwwDir: string;
50+
engine: string;
51+
platform?: string;
5052
watchPatterns: string[];
5153
proxies: ProxyConfig[];
5254
}
@@ -113,9 +115,28 @@ async function createHttpServer(options: ServeOptions): Promise<ζexpress.Applic
113115
res.send(indexHtml);
114116
};
115117

118+
const serveCordovaPlatformResource = async (req: ζexpress.Request, res: ζexpress.Response, next: ζexpress.NextFunction) => {
119+
if (options.engine !== 'cordova' || !options.platform) {
120+
return next();
121+
}
122+
123+
const resourcePath = path.resolve('platforms', options.platform, 'platform_www');
124+
125+
if (await pathExists(path.join(resourcePath, req.url))) {
126+
res.sendFile(req.url, { root: resourcePath });
127+
} else {
128+
next();
129+
}
130+
};
131+
116132
app.get('/', serveIndex);
117133
app.use('/', express.static(options.wwwDir));
118134

135+
// Cordova
136+
app.get('/cordova.js', serveCordovaPlatformResource, serveMockCordovaJS);
137+
app.get('/cordova_plugins.js', serveCordovaPlatformResource);
138+
app.get('/plugins/*', serveCordovaPlatformResource);
139+
119140
const livereloadUrl = `http://localhost:${options.livereloadPort}`;
120141
const pathPrefix = `/${DEV_SERVER_PREFIX}/tiny-lr`;
121142

@@ -151,3 +172,8 @@ async function attachProxy(app: ζexpress.Application, config: ProxyConfig) {
151172
const proxyMiddleware = await import('http-proxy-middleware');
152173
app.use(config.mount, proxyMiddleware(config.mount, config));
153174
}
175+
176+
function serveMockCordovaJS(req: ζexpress.Request, res: ζexpress.Response) {
177+
res.set('Content-Type', 'application/javascript');
178+
res.send('// mock cordova file during development');
179+
}

0 commit comments

Comments
 (0)