Skip to content

Commit f3826e7

Browse files
committed
feat: read template and style json for jscard
Signed-off-by: lileirjyb <[email protected]>
1 parent 48a586d commit f3826e7

File tree

4 files changed

+144
-3
lines changed

4 files changed

+144
-3
lines changed

core/framework/src/dsls/xvm/page/interface.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ function initPage(page, code, query, globals) {
6161

6262
const instEvaluate = context.quickapp.dock.makeEvaluateBuildScript(globals)
6363

64+
const jsonRequire = (jsonModuleName, options) => {
65+
return context.quickapp.platform.requireJson(jsonModuleName, options)
66+
}
67+
6468
// 处理代码
6569
let functionBody
6670
if (typeof code === 'function') {
@@ -84,7 +88,8 @@ function initPage(page, code, query, globals) {
8488
$app_bootstrap$: instBootstrap,
8589
$app_require$: instRequireModule,
8690
$app_define_wrap$: instDefineWrap,
87-
$app_evaluate$: instEvaluate
91+
$app_evaluate$: instEvaluate,
92+
$json_require$: jsonRequire
8893
},
8994
globals
9095
)
@@ -99,6 +104,7 @@ function initPage(page, code, query, globals) {
99104
global.$app_require$ = instRequireModule
100105
global.$app_define_wrap$ = instDefineWrap
101106
global.$app_evaluate$ = instEvaluate
107+
global.$json_require$ = jsonRequire
102108

103109
global.setTimeout = globals.setTimeout
104110
global.setInterval = globals.setInterval

core/framework/src/dsls/xvm/vm/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,13 @@ export default class XVm {
405405
}
406406
options = options || {}
407407

408+
// 打包结果包含extracted字段,说明是新的js卡打包格式
409+
// template和style的前处理后,js卡option.template和option.style的格式和之前的快应用就保持一致了
410+
if (options.style && options.style.extracted) {
411+
preHandleTemplate(options.template)
412+
preHandleStyle(options)
413+
}
414+
408415
// 页面级Vm:i18n配置合并
409416
if (this._isPageVm() && this._page.app) {
410417
const ret = this._page.app.getLocaleConfig()
@@ -1193,3 +1200,64 @@ XVm.mixin = function(options) {
11931200
}
11941201
}
11951202
}
1203+
1204+
function inReservedKeys(key) {
1205+
const RESERVED_ATTRS = ['$listeners', '$attrs']
1206+
return RESERVED_ATTRS.includes(key)
1207+
}
1208+
1209+
function isFunctionStr(str) {
1210+
const pattern = /^\s*function\s*\([\w\s,$]*\)\s*\{[\s\S]*\}\s*$/
1211+
return pattern.test(str.trim())
1212+
}
1213+
1214+
/**
1215+
* 如果携带了新打包格式的参数,则先进行template和style的预处理
1216+
* 主要是将key的$符号去除
1217+
* 以及将字符串的"function"转成真正的function
1218+
* */
1219+
1220+
function preHandleTemplate(target) {
1221+
if (!target) return
1222+
1223+
Object.keys(target).forEach(key => {
1224+
if (Object.prototype.toString.call(target[key]) === '[object Object]') {
1225+
preHandleTemplate(target[key])
1226+
} else if (Object.prototype.toString.call(target[key]) === '[object Array]') {
1227+
target[key].forEach(item => {
1228+
preHandleTemplate(item)
1229+
})
1230+
} else if (typeof target[key] === 'string') {
1231+
if (!isFunctionStr(target[key])) {
1232+
// value非function字符串
1233+
if (key.startsWith('$') && !inReservedKeys(key)) {
1234+
// example: "$value": "name",代表name为变量
1235+
const trimKey = key.substring(1)
1236+
const func = `function () { return this.${target[key]} }`
1237+
target[trimKey] = new Function(`return ${func}`)()
1238+
delete target[key]
1239+
}
1240+
} else {
1241+
// value为function字符串
1242+
if (key.startsWith('$')) {
1243+
// example: "$value": "function() {return this.a + '-' + this.b}",代表name为变量
1244+
const trimKey = key.substring(1)
1245+
target[trimKey] = new Function(`return ${target[key]}`)()
1246+
delete target[key]
1247+
} else {
1248+
// 理论上不存在该情况,防止打包处理遗漏,在这里兜底
1249+
target[key] = new Function(`return ${target[key]}`)()
1250+
}
1251+
}
1252+
}
1253+
})
1254+
}
1255+
1256+
function preHandleStyle(options) {
1257+
if (!options.style) return
1258+
1259+
const styleObjectId = options.style['@info'].styleObjectId
1260+
const jsonPath = options.style.jsonPath
1261+
1262+
options.style = context.quickapp.platform.requireJson(jsonPath, { styleObjectId }) || {}
1263+
}

core/framework/src/infras/platform/chunk.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,64 @@ function requireBundleChunk(filePath) {
3737
return cont
3838
}
3939

40-
export { registerBundleChunks, requireBundleChunk }
40+
const templateJsonMap = new Map()
41+
const styleJsonMap = new Map()
42+
function registerComponentJson(templateJson, cssJson) {
43+
if (typeof templateJson === 'string') {
44+
templateJson = JSON.parse(templateJson)
45+
}
46+
for (const compPath in templateJson) {
47+
if (templateJson.hasOwnProperty(compPath)) {
48+
let pathKey = compPath
49+
if (pathKey.startsWith('/')) {
50+
pathKey = pathKey.replace(/^\/+/, '')
51+
}
52+
const templateObj = templateJson[compPath]
53+
templateJsonMap.set(pathKey, templateObj)
54+
}
55+
}
56+
57+
if (typeof cssJson === 'string') {
58+
cssJson = JSON.parse(cssJson)
59+
}
60+
for (const compPath in cssJson) {
61+
if (cssJson.hasOwnProperty(compPath)) {
62+
let pathKey = compPath
63+
if (pathKey.startsWith('/')) {
64+
pathKey = pathKey.replace(/^\/+/, '')
65+
}
66+
const styleObj = cssJson[compPath]
67+
styleJsonMap.set(pathKey, styleObj)
68+
}
69+
}
70+
}
71+
72+
function requireJson(compPath, options) {
73+
try {
74+
if (options && options.styleObjectId) {
75+
const styleObj = JSON.parse(styleJsonMap.get(compPath))
76+
if (!styleObj) {
77+
console.warn(
78+
`### App Framework ### requireJson not exist ${compPath} -- options: ${JSON.stringify(
79+
options
80+
)}`
81+
)
82+
}
83+
const style = styleObj[options.styleObjectId]
84+
return style
85+
} else if (options && options.componentPath) {
86+
const templateObj = JSON.parse(templateJsonMap.get(compPath))
87+
if (!templateObj) {
88+
console.warn(
89+
`### App Framework ### requireJson not exist ${compPath} -- options: ${JSON.stringify(
90+
options
91+
)}`
92+
)
93+
}
94+
const template = templateObj[options.componentPath].template
95+
return template
96+
}
97+
} catch (e) {}
98+
}
99+
100+
export { registerBundleChunks, requireBundleChunk, registerComponentJson, requireJson }

core/framework/src/infras/platform/interface.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { invokeScript } from 'src/shared/function'
1010
import ModuleHost from './module/index'
1111
import { registerModules, execInvokeCallback, requireModule } from './module/interface'
1212
import Session from './session'
13-
import { registerBundleChunks, requireBundleChunk } from './chunk'
13+
import {
14+
registerBundleChunks,
15+
requireBundleChunk,
16+
registerComponentJson,
17+
requireJson
18+
} from './chunk'
1419

1520
import {
1621
registerManifest,
@@ -140,10 +145,12 @@ export default {
140145
ModuleHost,
141146
requireModule,
142147
requireScriptFile,
148+
requireJson,
143149
initInterface,
144150
exposure: {
145151
registerModules,
146152
registerBundleChunks,
153+
registerComponentJson,
147154
execInvokeCallback,
148155
registerManifest,
149156
getManifestField,

0 commit comments

Comments
 (0)