Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify expression results, add support for theme style, and fix bugs #85

Merged
merged 10 commits into from
Dec 24, 2024
42 changes: 40 additions & 2 deletions packages/hap-compiler/src/style/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,29 @@ const REGEXP_CUBIC_BEZIER =
const REGEXP_STEPS =
/steps\(\s*(\d+)\s*(?:,\s*(jump-start|jump-end|jump-none|jump-both|start|end))?\)/

const wrapperCached = new Map()
/**
* 封装一个校验器,使其接受 theme. 开头的变量值
*
* @param {Function} validator - 校验器
* @returns {Function}
*/
function builtInVarsWrapper(validator) {
let cached = wrapperCached.get(validator)
if (!cached) {
cached = function wrappedValidator() {
const result = validator.apply(null, arguments)
if (!result.value && arguments[0].startsWith('theme.')) {
result.value = arguments[0]
result.reason = undefined
}
return result
}
wrapperCached.set(validator, cached)
}
return cached
}

function toCamelCase(str) {
var re = /-(\w)/g
return str.replace(re, function ($0, $1) {
Expand Down Expand Up @@ -2377,6 +2400,21 @@ const validator = {
}
}

/**
* 为各个 validator 加上放行 theme 开头属性校验器
*/
function addBuiltInVarsWrapper() {
for (const key in validator) {
const validatorFn = validator[key]
const noNeed = ['arraynumber', 'arraylength', 'arraycolor', 'arraycolorstop', 'enum']
if (typeof validatorFn === 'function' && noNeed.indexOf(key) === -1) {
validator[key] = builtInVarsWrapper(validatorFn)
}
}
}

addBuiltInVarsWrapper()

/**
* 生成指定单位的长度校验函数
* @param units 单位值
Expand Down Expand Up @@ -2406,7 +2444,7 @@ function makeMultipleAttributesValidator(validatorMap) {
* @returns {validator_enum}
*/
function makeEnumValidator(list) {
return validator.enum.bind(null, list)
return builtInVarsWrapper(validator.enum.bind(null, list))
}

/**
Expand All @@ -2416,7 +2454,7 @@ function makeEnumValidator(list) {
* @returns {validator_$type}
*/
function makeAbbrAttrValidator(type, list) {
return validator[type].bind(null, list)
return builtInVarsWrapper(validator[type].bind(null, list))
}

// background属性校验表
Expand Down
1 change: 1 addition & 0 deletions packages/hap-compiler/src/template/exp.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function transExpr(expContent, toFunc, isLite) {
}

transExpr.isExpr = textParser.isExpr
transExpr.parseText = textParser.parseText
transExpr.singleExpr = textParser.singleExpr
transExpr.removeExprffix = textParser.removeExprffix
transExpr.addExprffix = textParser.addExprffix
Expand Down
25 changes: 24 additions & 1 deletion packages/hap-compiler/src/template/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,13 @@ function checkClass(className, output) {
classList = classList.filter((klass) => klass.trim())

if (isLite) {
if (!isValidClassArray(segs)) {
const err = new Error('轻卡 class 样式不支持动态数据绑定 {{}} 和静态样式混用')
err.isExpressionError = true
err.expression = className
throw err
}
output.result.class = className
output.result.classList = classList
} else if (hasBinding) {
const code = '(function () {return [' + classList.join(', ') + ']})'
Expand Down Expand Up @@ -1979,6 +1986,21 @@ function hasIfOrFor(nodes) {
return flag
}

function isValidClassArray(arr) {
const filterArr = arr.filter((clazz) => clazz.length > 0)

let hasTemplateString = false

for (let i = 0; i < filterArr.length; i++) {
if (exp.isExpr(filterArr[i])) {
hasTemplateString = true
break
}
}

return !hasTemplateString || filterArr.length === 1 // 如果全都为常量 className 或者只有一个变量的className,则返回true
}

export default {
checkTagName,
checkId,
Expand All @@ -2001,5 +2023,6 @@ export default {
isSupportedSelfClosing,
isEmptyElement,
isNotTextContentAtomic,
isExpr: exp.isExpr
isExpr: exp.isExpr,
parseText: exp.parseText
}
2 changes: 1 addition & 1 deletion packages/hap-dsl-xvm/src/loaders/action-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function actionLoader(source) {
const { parsed } = parseActions(jsonObj)
actionStr = parsed
} catch (e) {
throw new Error(`Invalid <data> in ${this.resourcePath}\n${e}`)
throw new Error(`${this.resourcePath} 中的 <data> 解析失败,请检查是否为标准的 JSON 格式\n${e}`)
}
return `module.exports = ${actionStr}`
}
2 changes: 1 addition & 1 deletion packages/hap-dsl-xvm/src/loaders/data-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function dataLoader(source) {
const obj = JSON.parse(source)
jsonObj = obj.uiData || obj.data || {}
} catch (e) {
throw new Error(`Invalid <data> in ${this.resourcePath}:: ${e}`)
throw new Error(`${this.resourcePath} 中的 <data> 解析失败,请检查是否为标准的 JSON 格式\n${e}`)
}
return `module.exports = ${JSON.stringify(jsonObj)}`
}
2 changes: 1 addition & 1 deletion packages/hap-dsl-xvm/src/loaders/props-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function propsLoader(source) {
const obj = JSON.parse(source)
jsonObj = obj.props || {}
} catch (e) {
throw new Error(`Invalid <data> in ${this.resourcePath}:: ${e}`)
throw new Error(`${this.resourcePath} 中的 <data> 解析失败,请检查是否为标准的 JSON 格式\n${e}`)
}
return `module.exports = ${JSON.stringify(jsonObj)}`
}
2 changes: 1 addition & 1 deletion packages/hap-dsl-xvm/test/unit/ux/card.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ describe('Card 编译测试', () => {
json.modules.forEach(module => {
expect(wipeDynamic(module.source)).toMatchSnapshot(module.id)
})
})
}, 50 * 60 * 1000)
})
2 changes: 1 addition & 1 deletion packages/hap-packager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"babel.tree.config.js"
],
"dependencies": {
"@aiot-toolkit/card-expression": "^1.0.12",
"@aiot-toolkit/card-expression": "^1.0.13",
"@babel/core": "^7.9.6",
"@babel/generator": "^7.9.6",
"@babel/parser": "^7.9.6",
Expand Down
4 changes: 2 additions & 2 deletions packages/hap-packager/src/common/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const BUILD_INFO_FILE = DIGEST_ZIP_DIR + '/build.txt'
// 文件列表摘要的名称
const DIGEST_HASH_JSON = 'hash.json'
// 分包的大小上限 1MB
const SINGLE_PKG_SIZE = 1024 * 1024
const SINGLE_PKG_SIZE = 2048 * 1024
// 整包的大小上限 4MB
const FULL_PKG_SIZE = SINGLE_PKG_SIZE * 4
const FULL_PKG_SIZE = SINGLE_PKG_SIZE * 10

// 压缩参数,设置输出 buffer,以便对 buffer 进行操作
const COMPRESS_OPTS = {
Expand Down
3 changes: 2 additions & 1 deletion packages/hap-packager/src/plugins/zip-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ ZipPlugin.prototype.apply = function (compiler) {
subPackages,
this.signConfig,
options.disableStreamPack,
compiler.watchMode
compiler.watchMode,
!options.disableSubpackages && options.subpackages && options.subpackages.length > 0
)

fs.ensureDirSync(options.output)
Expand Down
Loading
Loading