Skip to content

Commit b362a76

Browse files
committed
feat: compatibility with older js widget
Signed-off-by: lileirjyb <[email protected]>
1 parent 8d404e7 commit b362a76

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

packages/hap-packager/src/common/shared.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ function populateWidgetFields(widgetsObj) {
228228
`WARN: manifest.json 文件中 widgets 字段 ${key} 缺少 type 属性,默认设置为 js`
229229
)
230230
}
231+
if (widgetsObj[key].type === 'js' && widgetsObj[key].minCardPlatformVersion) {
232+
// 写了 minCardPlatformVersion 字段
233+
// 赋值给 minPlatformVersion(兼容旧引擎),引导引擎升级
234+
widgetsObj[key].minPlatformVersion = widgetsObj[key].minCardPlatformVersion
235+
}
231236
})
232237
}
233238

packages/hap-packager/src/plugins/card-plugin.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CardPlugin {
3838
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
3939
},
4040
() => {
41-
let { pathSrc } = this.options
41+
let { pathSrc, isCardMinVersion } = this.options
4242
pathSrc = pathSrc.replace(/\\/g, '/')
4343
const moduleGraph = compilation.moduleGraph
4444
for (const chunk of compilation.chunks) {
@@ -52,7 +52,7 @@ class CardPlugin {
5252
request,
5353
pathSrc
5454
)
55-
const templateRes = {
55+
let templateRes = {
5656
[CARD_ENTRY]: {}
5757
}
5858
const styleRes = {}
@@ -65,16 +65,20 @@ class CardPlugin {
6565
pathSrc,
6666
bundleFilePath
6767
)
68-
let handledTemplateCardRes
68+
6969
if (this.isLiteCard(entryRawRequest)) {
70-
handledTemplateCardRes = postHandleLiteCardRes(templateRes)
70+
templateRes = postHandleLiteCardRes(templateRes)
71+
} else if (isCardMinVersion) {
72+
// 快应用 2.0 标准的 JS 卡,输出 .template.json 和 .css.json
73+
templateRes = postHandleJSCardRes(templateRes)
7174
} else {
72-
handledTemplateCardRes = postHandleJSCardRes(templateRes)
75+
// 非快应用 2.0 标准的 JS 卡,不输出 .template.json 和 .css.json
76+
continue
7377
}
7478

7579
// 用于修改 template 的 key 的 stringify 的顺序,type 放第一个,children 放最后一个
7680
let templateKeys = []
77-
recordKeys(handledTemplateCardRes, templateKeys)
81+
recordKeys(templateRes, templateKeys)
7882

7983
templateKeys = [...new Set(templateKeys.sort())]
8084
.filter(
@@ -85,15 +89,15 @@ class CardPlugin {
8589
templateKeys.unshift('type', 'template', 'data')
8690

8791
// 自定义组件 template 抽取成独立 JSON —— 卡片不采用该方式
88-
// Object.keys(handledTemplateCardRes).forEach((key) => {
89-
// const res = handledTemplateCardRes[key]
92+
// Object.keys(templateRes).forEach((key) => {
93+
// const res = templateRes[key]
9094
// const fileName = key === CARD_ENTRY ? templateFileName : `${key}.template.json`
9195
// const templateJsonStr = JSON.stringify(res, templateKeys)
9296
// compilation.assets[fileName] = new ConcatSource(templateJsonStr)
9397
// })
9498

9599
// 处理 template
96-
const templateJsonStr = JSON.stringify(handledTemplateCardRes, templateKeys)
100+
const templateJsonStr = JSON.stringify(templateRes, templateKeys)
97101
compilation.assets[templateFileName] = new ConcatSource(templateJsonStr)
98102

99103
// 处理 css

packages/hap-packager/src/webpack.post.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
4848
subpackages,
4949
workers,
5050
originType,
51-
useTreeShaking
51+
useTreeShaking,
52+
isCardMinVersion
5253
} = defaultsOptions
5354

5455
const manifestObj = readJson(path.join(pathSrc, 'manifest.json'))
@@ -125,10 +126,12 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
125126
)
126127
}
127128

129+
if (isCardMinVersion) {
130+
// 如果工程中有卡片配置为快应用 2.0 卡片,则使用插件抽取 template 和 style 的 json
131+
webpackConf.plugins.push(new CardScriptHandlePlugin({ pathSrc }), new RemoveModulesPlugin())
132+
}
128133
webpackConf.plugins.push(
129-
new CardScriptHandlePlugin({ pathSrc }),
130-
new RemoveModulesPlugin(),
131-
new CardPlugin({ pathSrc }),
134+
new CardPlugin({ pathSrc, isCardMinVersion }),
132135
// 框架Handler包装
133136
new HandlerPlugin({
134137
pathSrc: pathSrc,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { name, postHook as packagerPostHook } from '@hap-toolkit/packager'
2424
import { postHook as xvmPostHook } from '@hap-toolkit/dsl-xvm'
2525
import ManifestWatchPlugin from '../plugins/manifest-watch-plugin'
26-
import { resolveEntries } from '../utils'
26+
import { resolveEntries, resolveCardMinVersion } from '../utils'
2727
import getDevtool from './get-devtool'
2828
import {
2929
getConfigPath,
@@ -154,6 +154,8 @@ export default async function genWebpackConf(launchOptions, mode) {
154154
// 页面文件
155155
const entries = resolveEntries(manifest, SRC_DIR, cwd)
156156

157+
const isCardMinVersion = resolveCardMinVersion(manifest)
158+
157159
// 环境变量
158160
const env = {
159161
// 平台:native
@@ -496,7 +498,8 @@ export default async function genWebpackConf(launchOptions, mode) {
496498
workers,
497499
cwd,
498500
originType: compileOptionsObject.originType,
499-
ideConfig: launchOptions.ideConfig
501+
ideConfig: launchOptions.ideConfig,
502+
isCardMinVersion
500503
},
501504
quickappConfig
502505
)

packages/hap-toolkit/src/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,28 @@ export function resolveEntries(manifest, basedir, cwd) {
126126
}
127127
return entries
128128
}
129+
export function resolveCardMinVersion(manifest) {
130+
if (!manifest.router) {
131+
throw Error('manifest.json 中未配置路由!')
132+
}
133+
let isCardMinVersion = false
134+
// 卡片配置
135+
const widgetsConf = manifest.router.widgets || {}
136+
137+
for (let key in widgetsConf) {
138+
if (!widgetsConf[key].type || widgetsConf[key].type === 'js') {
139+
// 针对 JS 卡的兼容
140+
if (widgetsConf[key].minCardPlatformVersion) {
141+
// 任意有一个 JS 卡写了 minCardPlatformVersion 字段
142+
isCardMinVersion = true
143+
} else {
144+
// 没有 JS 卡写 minCardPlatformVersion 字段,说明是旧的 JS 卡片包
145+
if (!widgetsConf[key].minPlatformVersion) {
146+
// 同时没有写 minPlatformVersion 字段,提示补齐 minCardPlatformVersion
147+
throw new Error(`manifest.json 中 widgets 必须包含 minCardPlatformVersion 字段`)
148+
}
149+
}
150+
}
151+
}
152+
return isCardMinVersion
153+
}

0 commit comments

Comments
 (0)