Skip to content

Commit a5a8844

Browse files
oneyouziyucongshuang
andauthored
校验卡片的size规范,不在范围内不能继续编译 (#88)
* fix: ide设置成inline-source-map生成的文件没有源文件 fix:ide传过来的项目地址是小写开发 fix:屏蔽colorconsole.attach(options.log) * fix: 删掉不必要的打印信息 * fix: ide编译的source-map源文件路径不对,修改webpack devtool产物做了适配 * 校验卡片size大小 * 校验卡片size大小格式化 * fix:获取vscode的语言环境 * fix:获取vscode的语言环境 --------- Co-authored-by: yucongshuang <[email protected]>
1 parent 9388ba5 commit a5a8844

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

packages/hap-server/src/preview/create-router.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,18 @@ export default async function createRouter(previewTarget) {
215215
const widgets = manifest.router.widgets || {}
216216
const type = requestRoute in widgets ? 'card' : 'app'
217217
const script = routes[requestRoute]
218+
const currentLanguage = JSON.parse(
219+
process.env.VSCODE_NLS_CONFIG || `{locale: 'zh-CN'}`
220+
).locale
218221
const html = await renderPage(TPL_PAGE_PATH, {
219222
title: manifest.name,
220223
routeName: requestRoute,
221224
routes: JSON.stringify(routes),
222225
type,
223226
script,
224227
scriptNotFound: !scriptExists(script),
225-
webJsUrl: genWebJsUrl(ctx.conf.options.webVersion)
228+
webJsUrl: genWebJsUrl(ctx.conf.options.webVersion),
229+
language: currentLanguage
226230
})
227231
ctx.type = 'text/html'
228232
ctx.body = html

packages/hap-server/src/preview/views/page.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<script type="text/javascript">
9797
var base = '/preview'
9898
var type = '{{ type }}'
99+
window.language = `{{language}}`
99100
;(function preview() {
100101
var routeName = '{{ routeName }}'
101102
Hap.init({

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ import {
3333
checkBabelModulesExists
3434
} from './helpers'
3535

36-
import { validateProject, validateManifest, validateSitemap, validateSkeleton } from './validate'
36+
import {
37+
validateProject,
38+
validateManifest,
39+
validateSitemap,
40+
validateSkeleton,
41+
validateCardSize
42+
} from './validate'
3743
import { postHook as idePostHook } from './ide.config'
3844

3945
const { PACKAGER_BUILD_PROGRESS } = eventBus
@@ -117,6 +123,7 @@ export default async function genWebpackConf(launchOptions, mode) {
117123

118124
// 清理 BUILD_DIR DIST_DIR
119125
cleanup(BUILD_DIR, DIST_DIR, mode)
126+
120127
const targetList = ['app', 'card', 'all']
121128
// 处理打包对象的校验,默认值之外的,只提示warning,默认为all
122129
if (!compileOptionsObject.target || targetList.indexOf(compileOptionsObject.target) === -1) {
@@ -136,6 +143,9 @@ export default async function genWebpackConf(launchOptions, mode) {
136143

137144
validateSkeleton(SRC_DIR, manifest)
138145

146+
// 检验工程size
147+
validateCardSize(SRC_DIR, manifest)
148+
139149
// 设置合适的v8版本
140150
setAdaptForV8Version(compileOptionsObject.disableScriptV8V65, manifest, cwd)
141151

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,31 @@ export function validateSkeleton(src, manifest) {
267267
})
268268
}
269269
}
270+
271+
/**
272+
* 卡片size校验,长宽都需要在['1','2','4','8','FULL','AUTO']范围内,卡片的size格式为['1x1','FULL','AUTO'],数组里面可多个
273+
* 需要解析长宽并验证都在范围内
274+
* @param {String} src - 项目src
275+
* @param {Object} manifest - manifest内容
276+
*/
277+
export function validateCardSize(src, manifest) {
278+
// 获取卡片配置的路由
279+
const widgetsConfig = manifest.router.widgets
280+
if (widgetsConfig) {
281+
const validateSizeRange = ['1', '2', '4', '8', 'FULL', 'AUTO']
282+
const keys = Object.keys(widgetsConfig)
283+
keys.forEach((widgetKey, index) => {
284+
// 将size字段的x处理拆分
285+
if (widgetsConfig[widgetKey]['size']) {
286+
const sizeArr = widgetsConfig[widgetKey]['size'].join(',').replace(/x/g, ',').split(',')
287+
sizeArr.forEach((size) => {
288+
if (!validateSizeRange.includes(size)) {
289+
colorconsole.throw(
290+
`manifest.json配置的卡片 ${widgetKey} 的size不合规范,长宽都要在['1','2','4','8','FULL','AUTO']范围内`
291+
)
292+
}
293+
})
294+
}
295+
})
296+
}
297+
}

0 commit comments

Comments
 (0)