Skip to content

Commit be5f59d

Browse files
zhushuaiCUMTz00029491
andauthored
增加轻卡data属性中honor特有字段 (#136)
* 增加轻卡honor特有字段 * 修改App属性为全小写 --------- Co-authored-by: z00029491 <[email protected]>
1 parent f34340f commit be5f59d

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Copyright (c) 2024-present, the hapjs-platform Project Contributors
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
export default function commandLoader(source) {
5+
let jsonObj = {}
6+
try {
7+
const obj = JSON.parse(source)
8+
jsonObj = obj.commands || {}
9+
} catch (e) {
10+
throw new Error(`${this.resourcePath} 中的 <data> 解析失败,请检查是否为标准的 JSON 格式\n${e}`)
11+
}
12+
return `module.exports = ${JSON.stringify(jsonObj)}`
13+
}

packages/hap-dsl-xvm/src/loaders/common/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,9 @@ export const FRAG_TYPE = {
248248
SCRIPT: 'script',
249249
DATA: 'data',
250250
ACTIONS: 'actions',
251-
PROPS: 'props'
251+
PROPS: 'props',
252+
// honor frag
253+
DATAPP: 'app',
254+
COMMANDS: 'commands'
255+
// end honor frag
252256
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2024-present, the hapjs-platform Project Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
export default function dataAppLoader(source) {
6+
let jsonObj = {}
7+
try {
8+
const obj = JSON.parse(source)
9+
jsonObj = obj.app || {}
10+
} catch (e) {
11+
throw new Error(`${this.resourcePath} 中的 <data> 解析失败,请检查是否为标准的 JSON 格式\n${e}`)
12+
}
13+
return `module.exports = ${JSON.stringify(jsonObj)}`
14+
}

packages/hap-dsl-xvm/src/loaders/ux-fragment-utils.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ const defaultLoaders = {
4141
mainfest: resolveSync('@hap-toolkit/packager/lib/loaders/manifest-loader.js'),
4242
// page
4343
access: resolveSync('./access-loader.js'),
44-
'extract-css': resolveSync('./extract-css-loader.js')
44+
'extract-css': resolveSync('./extract-css-loader.js'),
45+
// honor loader
46+
datapp: resolveSync('./datapp-loader.js'),
47+
command: resolveSync('./command-loader.js'),
48+
// end honor loader
4549
}
4650

4751
/**
@@ -119,6 +123,40 @@ function makeLoaderString(type, config, newJSCard, uxType) {
119123
return stringifyLoaders(loaders)
120124
}
121125

126+
// honor loader
127+
if (type === FRAG_TYPE.DATAPP) {
128+
loaders = [
129+
{
130+
name: defaultLoaders.datapp
131+
},
132+
{
133+
name: defaultLoaders.fragment,
134+
query: {
135+
index: 0,
136+
type: FRAG_TYPE.DATA
137+
}
138+
}
139+
]
140+
return stringifyLoaders(loaders)
141+
}
142+
143+
if (type === FRAG_TYPE.COMMANDS) {
144+
loaders = [
145+
{
146+
name: defaultLoaders.command
147+
},
148+
{
149+
name: defaultLoaders.fragment,
150+
query: {
151+
index: 0,
152+
type: FRAG_TYPE.DATA
153+
}
154+
}
155+
]
156+
return stringifyLoaders(loaders)
157+
}
158+
// end honor loader
159+
122160
if (type === FRAG_TYPE.PROPS) {
123161
loaders = [
124162
{
@@ -543,6 +581,60 @@ function processPropsFrag($loader, datas, uxType) {
543581
return code
544582
}
545583

584+
/**
585+
* 处理轻卡<data>片段中app(only honor)
586+
* @param $loader
587+
* @param scripts
588+
* @param uxType
589+
* @returns {string}
590+
*/
591+
function processDataAppFrag($loader, datas, uxType) {
592+
let code = 'null'
593+
if (datas.length) {
594+
// 有且仅有一个<data>节点
595+
const data = datas[0]
596+
// 文件绝对路径
597+
let src = $loader.resourcePath
598+
const fragAttrsSrc = data.attrs.src
599+
if (fragAttrsSrc) {
600+
src = fragAttrsSrc
601+
}
602+
code = makeRequireString(
603+
$loader,
604+
makeLoaderString(FRAG_TYPE.DATAPP, {}, true, uxType),
605+
`${src}?index=0&lite=1`
606+
)
607+
}
608+
return code
609+
}
610+
611+
/**
612+
* 处理轻卡<data>片段中command(only honor)
613+
* @param $loader
614+
* @param scripts
615+
* @param uxType
616+
* @returns {string}
617+
*/
618+
function processCommandFrag($loader, datas, uxType) {
619+
let code = 'null'
620+
if (datas.length) {
621+
// 有且仅有一个<data>节点
622+
const data = datas[0]
623+
// 文件绝对路径
624+
let src = $loader.resourcePath
625+
const fragAttrsSrc = data.attrs.src
626+
if (fragAttrsSrc) {
627+
src = fragAttrsSrc
628+
}
629+
code = makeRequireString(
630+
$loader,
631+
makeLoaderString(FRAG_TYPE.COMMANDS, {}, true, uxType),
632+
`${src}?index=0&lite=1`
633+
)
634+
}
635+
return code
636+
}
637+
546638
/**
547639
* 统一解析全部后处理
548640
* @param $loader
@@ -591,5 +683,9 @@ export {
591683
processDataFrag,
592684
processActionFrag,
593685
processPropsFrag,
594-
parseImportList
686+
parseImportList,
687+
// honor process frag
688+
processDataAppFrag,
689+
processCommandFrag,
690+
// end honor process frag
595691
}

packages/hap-dsl-xvm/src/loaders/ux-loader.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
processDataFrag,
1717
processActionFrag,
1818
processPropsFrag,
19-
parseImportList
19+
parseImportList,
20+
// honor process frag
21+
processCommandFrag,
22+
processDataAppFrag
23+
// end honor process frag
2024
} from './ux-fragment-utils'
2125
import { getNameByPath, print, isUXRender, FRAG_TYPE } from './common/utils'
2226

@@ -96,6 +100,20 @@ function assemble($loader, frags, name, queryOptions) {
96100
uxType,
97101
FRAG_TYPE.PROPS
98102
)}\n`
103+
// process honor frag
104+
content += ` $app_module$.exports.app = ${processDataAppFrag(
105+
$loader,
106+
frags.data,
107+
uxType,
108+
FRAG_TYPE.APP
109+
)}\n`
110+
content += ` $app_module$.exports.commands = ${processCommandFrag(
111+
$loader,
112+
frags.data,
113+
uxType,
114+
FRAG_TYPE.COMMANDS
115+
)}\n`
116+
// end process honor frag
99117
content += ` $app_data$($app_module$, $app_require$)\n`
100118
}
101119
content +=

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ const LOADER_INFO_LIST = [
7474
{
7575
path: '/props-loader.js',
7676
type: 'props'
77+
},
78+
// honor loader
79+
{
80+
path: '/datapp-loader.js',
81+
type: 'app'
82+
},
83+
{
84+
path: '/command-loader.js',
85+
type: 'commands'
7786
}
87+
// end honor loader
7888
]
7989

8090
export {

0 commit comments

Comments
 (0)