|
1 | 1 | import * as path from 'path'; |
2 | 2 |
|
3 | | -import { fsReadFile } from '@ionic/cli-framework/utils/fs'; |
| 3 | +import { fsReadFile, pathExists } from '@ionic/cli-framework/utils/fs'; |
4 | 4 | import chalk from 'chalk'; |
5 | 5 |
|
6 | 6 | import * as ζexpress from 'express'; |
@@ -47,6 +47,8 @@ export interface ServeOptions { |
47 | 47 | devPort: number; |
48 | 48 | livereloadPort: number; |
49 | 49 | wwwDir: string; |
| 50 | + engine: string; |
| 51 | + platform?: string; |
50 | 52 | watchPatterns: string[]; |
51 | 53 | proxies: ProxyConfig[]; |
52 | 54 | } |
@@ -113,9 +115,28 @@ async function createHttpServer(options: ServeOptions): Promise<ζexpress.Applic |
113 | 115 | res.send(indexHtml); |
114 | 116 | }; |
115 | 117 |
|
| 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 | + |
116 | 132 | app.get('/', serveIndex); |
117 | 133 | app.use('/', express.static(options.wwwDir)); |
118 | 134 |
|
| 135 | + // Cordova |
| 136 | + app.get('/cordova.js', serveCordovaPlatformResource, serveMockCordovaJS); |
| 137 | + app.get('/cordova_plugins.js', serveCordovaPlatformResource); |
| 138 | + app.get('/plugins/*', serveCordovaPlatformResource); |
| 139 | + |
119 | 140 | const livereloadUrl = `http://localhost:${options.livereloadPort}`; |
120 | 141 | const pathPrefix = `/${DEV_SERVER_PREFIX}/tiny-lr`; |
121 | 142 |
|
@@ -151,3 +172,8 @@ async function attachProxy(app: ζexpress.Application, config: ProxyConfig) { |
151 | 172 | const proxyMiddleware = await import('http-proxy-middleware'); |
152 | 173 | app.use(config.mount, proxyMiddleware(config.mount, config)); |
153 | 174 | } |
| 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