Skip to content

Commit 4a95f03

Browse files
oneyouziyucongshuang
andauthored
预览服务配置反向代理 (#143)
* server配置代理,插件给预览页传调试js url * 格式修正 * 增加备注 * ide新建模板卡片传入参数控制是否每次编译编译更新用户自定义变量 * 格式调整 * 还原scriptNotFound判断 --------- Co-authored-by: yucongshuang <v-yucongshuang@oppo.com>
1 parent 9156ecf commit 4a95f03

File tree

7 files changed

+46
-4
lines changed

7 files changed

+46
-4
lines changed

packages/hap-server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"qr-image": "^3.2.0",
4545
"qrcode-terminal": "^0.12.0",
4646
"request": "^2.88.2",
47-
"resolve": "^1.22.2"
47+
"resolve": "^1.22.2",
48+
"koa-proxies": "^0.12.4"
4849
}
4950
}

packages/hap-server/src/preview/create-router.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export default async function createRouter(previewTarget) {
228228
type,
229229
script,
230230
scriptNotFound: !scriptExists(script),
231+
devtoolUrl: browerOptions.options.devtoolUrl || '',
231232
webJsUrl: genWebJsUrl(browerOptions.options.version || ctx.conf.options.webVersion), // 更改预览版本号修改为同媒介查询一样的传参方式,同时兼容之前的方式
232233
language: currentLanguage,
233234
mediaQueryParams: JSON.stringify(mediaQueryParams) // 传给页面的媒介查询参数

packages/hap-server/src/preview/views/page.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@
9393
document.write('<script src="/preview-static/web.js">\x3C/script>')
9494
}
9595
</script>
96+
<script type="text/javascript">
97+
if (`{{devtoolUrl}}`) {
98+
document.write('<script src="{{devtoolUrl}}">\x3C/script>')
99+
}
100+
</script>
96101
<script type="text/javascript">
97102
var base = '/preview'
98103
var type = '{{ type }}'
@@ -102,7 +107,8 @@
102107
var routeName = '{{ routeName }}'
103108
Hap.init({
104109
base,
105-
type
110+
type,
111+
idePlatform: `{{devtoolUrl}}` ? 'extensionOnline' : undefined
106112
})
107113
})()
108114
</script>

packages/hap-server/src/server.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from '@hap-toolkit/shared-utils'
1717
import { browerOptions } from './config'
1818
const moduler = [require('@hap-toolkit/debugger')]
19+
const proxies = require('koa-proxies')
1920
let server = null
2021
export async function launch(conf) {
2122
return new Promise(async (resolve) => {
@@ -25,6 +26,20 @@ export async function launch(conf) {
2526
moduler.push((await import('@hap-toolkit/packager')).router, require('./preview/index.js'))
2627
}
2728
const app = new Koa()
29+
// 插件方式的预览页面是iframe,快应用开发ajax请求会受同源策略限制,需要做代理转发
30+
app.use(async (ctx, next) => {
31+
if (ctx.request.url.startsWith('/api/proxy')) {
32+
const target = ctx.request.url.split('target=')[1] // 从查询参数获取实际请求的目标地址
33+
// const apiReg = new RegExp(`^${api}/`);
34+
return proxies('/api/proxy', {
35+
target: target,
36+
changeOrigin: true,
37+
logs: true,
38+
rewrite: (path) => path.replace(/^\/api\/proxy/, '')
39+
})(ctx, next)
40+
}
41+
return next()
42+
})
2843
let serverPort = globalConfig.server.port
2944
// 如果设置的端口被占用,则自动递增获取可用端口
3045
serverPort = await portfinder.getPortPromise({

packages/hap-toolkit/src/commands/compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ showVersion()
4141
* @param {Function} [options.onerror] - 错误回调函数
4242
* @param {String} [options.buildPreviewRpkOptions] - 预览包保存路径,由IDE传入
4343
* @param {Object} [options.compileOptions] - 编译参数,由IDE传入
44+
* @param {Object} [options.isUpdateDefine=undefined] - 执行compile是否更新webpack注入的变量,ide新建模板卡片会传入
4445
* @param {Object} [options.ideConfig] - cli,由 IDE 传入
4546
* @returns {Promise} - 返回成功与否的信息
4647
*/

packages/hap-toolkit/src/gen-webpack-conf/helpers.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ export function getConfigPath(cwd) {
2525
} while (++index < configFileList.length)
2626
return configPath
2727
}
28-
28+
/** ide新建模板执行编译获取用户配置的路径,变量可变,需要复制一份
29+
* @param {String} cwd
30+
*/
31+
export function getNewTemplateConfigPath(cwd) {
32+
const defaultConfig = 'quickapp.config.js'
33+
const defaultConfigPath = path.join(cwd, defaultConfig)
34+
const copyConfig = new Date().getTime() + '.js'
35+
const copyConfigPath = path.join(cwd, copyConfig)
36+
fs.copyFileSync(defaultConfigPath, copyConfigPath)
37+
return copyConfigPath
38+
}
2939
/**
3040
* 清理 BUILD_DIR DIST_DIR
3141
*/

packages/hap-toolkit/src/gen-webpack-conf/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { resolveEntries } from '../utils'
2727
import getDevtool from './get-devtool'
2828
import {
2929
getConfigPath,
30+
getNewTemplateConfigPath,
3031
cleanup,
3132
checkBuiltinModules,
3233
setAdaptForV8Version,
@@ -64,6 +65,7 @@ const SPLIT_CHUNKS_SUPPORT_VERSION_FROM = 1080
6465
* @param {boolean} [launchOptions.optimizeStyleAppLevel=false] - 优化 app 样式等级
6566
* @param {boolean} [launchOptions.optimizeStylePageLevel=false] - 优化 app 样式等级
6667
* @param {boolean} [launchOptions.splitChunksMode=undefined] - 抽取公共JS
68+
* @param {boolean} [launchOptions.isUpdateDefine=undefined] - 执行compile是否更新webpack注入的变量,ide新建模板卡片会传入
6769
* @param {Object} [options.compileOptions] - 编译参数,由IDE传入
6870
* @param {production|development} mode - webpack mode
6971
* @returns {WebpackConfiguration}
@@ -76,7 +78,9 @@ export default async function genWebpackConf(launchOptions, mode) {
7678
globalConfig.projectPath = path.resolve(globalConfig.projectPath)
7779
const cwd = globalConfig.projectPath
7880

79-
const hapConfigPath = getConfigPath(cwd)
81+
const hapConfigPath = launchOptions.isUpdateDefine
82+
? getNewTemplateConfigPath(cwd)
83+
: getConfigPath(cwd)
8084
// 用于接受quickapp.config.js 或者 hap.config.js中的配置
8185
let quickappConfig
8286
// 接受命令行
@@ -92,6 +96,10 @@ export default async function genWebpackConf(launchOptions, mode) {
9296
colorconsole.error(`加载webpack配置文件[${hapConfigPath}]出错:${err.message}`)
9397
}
9498
}
99+
// 获取到更新后的用户配置文件后要删除复制配置文件
100+
if (launchOptions.isUpdateDefine) {
101+
fs.unlinkSync(hapConfigPath)
102+
}
95103
// 接收ide命令行
96104
if (launchOptions.ideConfig && typeof launchOptions.ideConfig.cli === 'object') {
97105
launchOptions = Object.assign({}, launchOptions.ideConfig.cli, launchOptions)

0 commit comments

Comments
 (0)