Skip to content

feat: modify new jscard judgement by minCardPlatformVersion #120

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

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/hap-compiler/src/template/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ const tagNatives = {
}
},
picker: {
minRequiredRuntimeVersion: 1201,
supportCard: true,
selfClosing: true,
atomic: true,
Expand Down Expand Up @@ -588,6 +589,8 @@ const tagNatives = {
events: ['change', 'selectionchange', 'linechange']
},
video: {
supportCard: true,
minRequiredRuntimeVersion: 1201,
empty: true,
attrs: {
src: {},
Expand Down Expand Up @@ -806,6 +809,7 @@ const tagNatives = {
excludeRoot: true
},
marquee: {
minRequiredRuntimeVersion: 1201,
supportCard: true,
textContent: true,
atomic: true,
Expand Down Expand Up @@ -1097,6 +1101,25 @@ function checkTagName(node, output, options = {}) {
})
}

if (
(options.uxType === ENTRY_TYPE.CARD || options.uxType === ENTRY_TYPE.COMP) &&
tagNatives[tagName] &&
options.minCardRuntimeVersion &&
options.minCardRuntimeVersion < tagNatives[tagName].minRequiredRuntimeVersion
) {
const locationAddr =
location.line && location.column ? '@' + location.line + ':' + location.column : ''
colorconsole.throw(
'ERROR: ' +
locationAddr +
' 使用 `' +
tagName +
'` 组件,要求卡片配置 minCardPlatformVersion 或 minPlatformVersion 不低于 ' +
tagNatives[tagName].minRequiredRuntimeVersion +
', 请修改'
)
}

// 检测根组件合法性
if (node._isroot && tagNotRoot.hasOwnProperty(tagName)) {
log.push({
Expand Down
14 changes: 10 additions & 4 deletions packages/hap-dsl-xvm/src/loaders/ux-fragment-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,15 @@ function makeLoaderString(type, config, newJSCard, uxType) {
* @returns {string}
*/
function processImportFrag($loader, imports, importNames, queryOptions = {}) {
const { newJSCard, lite, cardEntry } = queryOptions
const { newJSCard, lite, cardEntry, minCardRuntimeVersion } = queryOptions
let retStr = ''
if (imports.length) {
const newJSCardParam = newJSCard ? `&newJSCard=${newJSCard}` : ''
const liteParam = lite ? `&lite=${lite}` : ''
const cardEntryParam = cardEntry ? `&cardEntry=${cardEntry}` : ''
const minCardRuntimeVersionParam = minCardRuntimeVersion
? `&minCardRuntimeVersion=${minCardRuntimeVersion}`
: ''
for (let i = 0; i < imports.length; i++) {
const imp = imports[i]
let importSrc = imp.attrs.src
Expand Down Expand Up @@ -311,7 +314,7 @@ function processImportFrag($loader, imports, importNames, queryOptions = {}) {
let reqStr = makeRequireString(
$loader,
makeLoaderString(FRAG_TYPE.IMPORT, null, newJSCard),
`${importSrc}?uxType=${ENTRY_TYPE.COMP}&name=${importName}${newJSCardParam}${liteParam}${cardEntryParam}`
`${importSrc}?uxType=${ENTRY_TYPE.COMP}&name=${importName}${newJSCardParam}${liteParam}${cardEntryParam}${minCardRuntimeVersionParam}`
)

if (compileOptionsObject.stats) {
Expand All @@ -335,7 +338,7 @@ function processImportFrag($loader, imports, importNames, queryOptions = {}) {
* @param {number} lite 轻卡
*/
function processTemplateFrag($loader, templates, importNames, queryOptions = {}) {
const { uxType, newJSCard, lite, cardEntry } = queryOptions
const { uxType, newJSCard, lite, cardEntry, minCardRuntimeVersion } = queryOptions
let retStr = '{}'
if (!templates.length) {
$loader.emitError(new Error('需要模板 <template> 片段'))
Expand All @@ -354,6 +357,9 @@ function processTemplateFrag($loader, templates, importNames, queryOptions = {})
const liteParam = lite ? `&lite=${lite}` : ''
const pathParam = newJSCard ? `&uxPath=${encodeURIComponent(src)}` : ''
const cardEntryParam = cardEntry ? `&cardEntry=${encodeURIComponent(cardEntry)}` : ''
const minCardRuntimeVersionParam = minCardRuntimeVersion
? `&minCardRuntimeVersion=${minCardRuntimeVersion}`
: ''
// 解析成类似url中key[]=xxx 的形式,便于loader-utils解析
importNames = importNames.map((item) => 'importNames[]=' + item)
retStr = makeRequireString(
Expand All @@ -367,7 +373,7 @@ function processTemplateFrag($loader, templates, importNames, queryOptions = {})
),
`${src}?uxType=${uxType}&${importNames.join(
','
)}${newJSCardParam}${liteParam}${pathParam}${cardEntryParam}`
)}${newJSCardParam}${liteParam}${pathParam}${cardEntryParam}${minCardRuntimeVersionParam}`
)
}
return retStr
Expand Down
3 changes: 2 additions & 1 deletion packages/hap-dsl-xvm/src/loaders/ux-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export default function uxLoader(source) {
uxType: resourceQuery.uxType, // 文件类型
newJSCard: resourceQuery.newJSCard, // 新打包格式的 JS卡
lite: resourceQuery.lite, // 轻卡
cardEntry: resourceQuery.cardEntry // 卡片入口
cardEntry: resourceQuery.cardEntry, // 卡片入口
minCardRuntimeVersion: resourceQuery.minCardRuntimeVersion // 卡片配置的最小平台版本
}
// 使用原有文件名(不包含扩展名)
const name = resourceQuery.name || getNameByPath(resourcePath)
Expand Down
10 changes: 4 additions & 6 deletions packages/hap-packager/src/common/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ function searchModuleImport(fileCont, options = {}) {
/**
* 检测manifest里面的widget,包括:
* 检测不支持的feature
* 增加 widget 中 path 缺失时的默认值
*/
function checkFeatureInCard(obj = {}) {
Object.keys(obj).forEach((key) => {
Expand All @@ -207,8 +206,6 @@ function checkFeatureInCard(obj = {}) {
}
})
})

populateWidgetFields(obj)
}

/**
Expand Down Expand Up @@ -237,13 +234,13 @@ function populateWidgetFields(widgetsObj) {
conf.minPlatformVersion = conf.minCardPlatformVersion
} else if (!conf.minPlatformVersion) {
// 没写 minCardPlatformVersion 和 minPlatformVersion 字段
throw new Error(
colorconsole.throw(
`manifest.json 文件 widgets 字段下,${routePath} 缺少 minCardPlatformVersion 字段`
)
}
}
if (conf.type === 'lite' && !conf.minCardPlatformVersion) {
throw new Error(
colorconsole.throw(
`manifest.json 文件 widgets 字段下,轻卡 ${routePath} 缺少 minCardPlatformVersion 字段`
)
}
Expand All @@ -268,9 +265,10 @@ function updateManifest(manifest, debug) {

// 更新features
manifest.features = manifest.features || []

checkFeatureInCard(manifest.router.widgets || {})

populateWidgetFields(manifest.router.widgets || {})

// 在项目所有引用的模块列表中删除manifest已声明的模块
const projectFeatureList = [].concat(global.framework.project.featureList)
manifest.features.forEach((feature) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/hap-packager/src/plugins/card-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class CardPlugin {
}
const { _valueAsString, _valueAsBuffer } = _source || {}
const sourceValueStr = _valueAsString || _valueAsBuffer?.toString() || ''
let obj
let obj = {}
let jsonStr = sourceValueStr
const sourceValueStrTrim = sourceValueStr.trim()
if (sourceValueStrTrim.indexOf('module.exports') === 0) {
Expand Down
4 changes: 3 additions & 1 deletion packages/hap-packager/src/plugins/resource-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ function minifyWidgetJSONFiles(targetDir, specifiedDirArray) {
// 轻卡和写了minCardPlatformVersion的新打包格式的JS卡会进行多语言扁平化处理
const needFlatten =
directoryName === I18N_DIRECTORY &&
(widgetsOption[key].type === 'lite' || widgetsOption[key].minCardPlatformVersion)
(widgetsOption[key].type === 'lite' ||
(widgetsOption[key].minCardPlatformVersion &&
widgetsOption[key].minCardPlatformVersion >= 2000))
if (fs.existsSync(dir)) {
// onlyRoot: 是否仅遍历根目录
const jsonPath = onlyRoot ? '*.json' : '**/**.json'
Expand Down
4 changes: 2 additions & 2 deletions packages/hap-packager/src/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
globalConfig,
recordClient,
getRecordClient,
isNeedFullPackage
getUseFullPackage
} from '@hap-toolkit/shared-utils'

import {
Expand Down Expand Up @@ -67,7 +67,7 @@ async function bundle(context, next) {
let useFullPackage = false
// 调试器选择应用模式下
if (parseInt(context.request.query.mode || 0) === 0) {
useFullPackage = isNeedFullPackage(projectPath)
useFullPackage = getUseFullPackage(projectPath)
}

if (platformVersion && platformVersion >= RPKS_SUPPORT_VERSION_FROM && !useFullPackage) {
Expand Down
35 changes: 27 additions & 8 deletions packages/hap-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,33 @@ function getHash(compPath) {
return res
}

// 读取 manifest
let manifest

export function isNeedFullPackage() {
// 读取 manifest
let manifest
let needFullPackage = false
try {
manifest = readJson(path.join(globalConfig.projectPath, './src/manifest.json'))
} catch (err) {
manifest = null
if (!manifest) {
try {
manifest = readJson(path.join(globalConfig.projectPath, './src/manifest.json'))
} catch (err) {
manifest = null
}
}
// 存在卡片
if (manifest.router.widgets && Object.keys(manifest.router.widgets).length > 0) {
needFullPackage = true
}
return needFullPackage
}

export function getUseFullPackage() {
let useFullPackage = false
if (!manifest) {
try {
manifest = readJson(path.join(globalConfig.projectPath, './src/manifest.json'))
} catch (err) {
manifest = null
}
}
// 快应用项目,不存在分包
if (
Expand All @@ -455,8 +474,8 @@ export function isNeedFullPackage() {
) {
// 存在卡片
if (manifest.router.widgets && Object.keys(manifest.router.widgets).length > 0) {
needFullPackage = true
useFullPackage = true
}
}
return needFullPackage
return useFullPackage
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $app_define$('@app-component/index', [], function($app_require$, $app_exports$,
if ($app_exports$.__esModule && $app_exports$.default) {
$app_module$.exports = $app_exports$.default
}
$app_module$.exports.template = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/template-loader.js!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=template!./index.ux?uxType=card&&cardEntry=CardDemo\\")
$app_module$.exports.template = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/template-loader.js!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=template!./index.ux?uxType=card&&cardEntry=CardDemo&minCardRuntimeVersion=2000\\")
$app_module$.exports.style = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/style-loader.js?index=0&type=style!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=style!./index.ux?uxType=card&cardEntry=CardDemo\\")
});
$app_bootstrap$('@app-component/index',{ packagerVersion: QUICKAPP_TOOLKIT_VERSION });"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ $app_define$('@app-component/index', [], function($app_require$, $app_exports$,
if ($app_exports$.__esModule && $app_exports$.default) {
$app_module$.exports = $app_exports$.default
}
$app_module$.exports.template = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/template-loader.js!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=template!./index.ux?uxType=card&&cardEntry=CardDemo\\")
$app_module$.exports.template = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/template-loader.js!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=template!./index.ux?uxType=card&&cardEntry=CardDemo&minCardRuntimeVersion=2000\\")
$app_module$.exports.style = require(\\"!../../../../../hap-dsl-xvm/lib/loaders/style-loader.js?index=0&type=style!../../../../../hap-dsl-xvm/lib/loaders/fragment-loader.js?index=0&type=style!./index.ux?uxType=card&cardEntry=CardDemo\\")
});
$app_bootstrap$('@app-component/index',{ packagerVersion: QUICKAPP_TOOLKIT_VERSION });"
Expand Down Expand Up @@ -355,7 +355,7 @@ Array [

exports[`测试compile generate webpack config 1`] = `
Object {
"CardDemo/index": "./src/CardDemo/index.ux?uxType=card&cardEntry=CardDemo",
"CardDemo/index": "./src/CardDemo/index.ux?uxType=card&minCardRuntimeVersion=2000&cardEntry=CardDemo",
"Demo/index": "./src/Demo/index.ux?uxType=page",
"app": "./src/app.ux?uxType=app",
"workers/request/index": "./src/workers/request/index.js",
Expand Down
Loading
Loading