Skip to content

Commit 561c7ff

Browse files
authored
fix: fix bugs for older js widget (#101)
Signed-off-by: lileirjyb <lileirjyb@vivo.com>
1 parent 5800e0b commit 561c7ff

File tree

7 files changed

+18
-39
lines changed

7 files changed

+18
-39
lines changed

packages/hap-packager/src/plugins/card-plugin.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CardPlugin {
3838
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
3939
},
4040
() => {
41-
let { pathSrc, isCardMinVersion } = this.options
41+
let { pathSrc } = this.options
4242
pathSrc = pathSrc.replace(/\\/g, '/')
4343
const moduleGraph = compilation.moduleGraph
4444
for (const chunk of compilation.chunks) {
@@ -47,6 +47,7 @@ class CardPlugin {
4747
continue
4848
}
4949
const { rawRequest: entryRawRequest, request } = entryModule
50+
// 只有轻卡和新打包格式的JS卡会处理template和style
5051
if (this.needExtractTemplateAndStyle(entryRawRequest)) {
5152
const { templateFileName, cssFileName, bundleFilePath } = this.getCardBuildPath(
5253
request,
@@ -68,12 +69,9 @@ class CardPlugin {
6869

6970
if (this.isLiteCard(entryRawRequest)) {
7071
templateRes = postHandleLiteCardRes(templateRes)
71-
} else if (isCardMinVersion) {
72+
} else {
7273
// 快应用 2.0 标准的 JS 卡,输出 .template.json 和 .css.json
7374
templateRes = postHandleJSCardRes(templateRes)
74-
} else {
75-
// 非快应用 2.0 标准的 JS 卡,不输出 .template.json 和 .css.json
76-
continue
7775
}
7876

7977
// 用于修改 template 的 key 的 stringify 的顺序,type 放第一个,children 放最后一个

packages/hap-packager/src/plugins/card-script-handle-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CardScriptHandlePlugin {
4343
continue
4444
}
4545
const { rawRequest: entryRawRequest, request } = entryModule
46+
// 只有新打包格式的JS卡会进行script的处理
4647
if (this.isNewJsCard(entryRawRequest)) {
4748
const { templateFileName, cssFileName } = this.getCardBuildPath(request, pathSrc)
4849
const jsAssetesName = this.getJsAssetsName(request, pathSrc)

packages/hap-packager/src/plugins/remove-modules-plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RemoveModulesPlugin {
2222
compilation.hooks.optimizeModules.tap('RemoveModulesPlugin', (modules) => {
2323
modules.forEach((module) => {
2424
const { _source, request } = module
25+
// 只有新打包格式的JS卡会进行script的处理
2526
if (this.isNewJsCardAndCardcomp(request)) {
2627
const { _valueAsString, _valueAsBuffer } = _source || {}
2728
const moduleSource = _valueAsString || _valueAsBuffer?.toString() || ''

packages/hap-packager/src/plugins/resource-plugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ function minifyWidgetI18nJSONFiles(targetDir) {
130130
}
131131
const widgetPath = widgetsOption[key].path
132132
const dir = path.join(targetDir, widgetPath, 'i18n')
133+
// 轻卡和写了minCardPlatformVersion的新打包格式的JS卡会进行多语言扁平化处理
134+
const needFlatten =
135+
widgetsOption[key].type === 'lite' || widgetsOption[key].minCardPlatformVersion
133136
if (fs.existsSync(dir)) {
134137
const jsonFiles = getFiles('*.json', dir)
135138
jsonFiles.forEach((filePath) => {
136139
arr.push(filePath)
137-
minifyJson(filePath, filePath, true)
140+
minifyJson(filePath, filePath, needFlatten)
138141
})
139142
}
140143
}

packages/hap-packager/src/webpack.post.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
4848
subpackages,
4949
workers,
5050
originType,
51-
useTreeShaking,
52-
isCardMinVersion
51+
useTreeShaking
5352
} = defaultsOptions
5453

5554
const manifestObj = readJson(path.join(pathSrc, 'manifest.json'))
@@ -126,12 +125,10 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
126125
)
127126
}
128127

129-
if (isCardMinVersion) {
130-
// 如果工程中有卡片配置为快应用 2.0 卡片,则使用插件抽取 template 和 style 的 json
131-
webpackConf.plugins.push(new CardScriptHandlePlugin({ pathSrc }), new RemoveModulesPlugin())
132-
}
133128
webpackConf.plugins.push(
134-
new CardPlugin({ pathSrc, isCardMinVersion }),
129+
new CardScriptHandlePlugin({ pathSrc }),
130+
new RemoveModulesPlugin(),
131+
new CardPlugin({ pathSrc }),
135132
// 框架Handler包装
136133
new HandlerPlugin({
137134
pathSrc: pathSrc,

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { name, postHook as packagerPostHook } from '@hap-toolkit/packager'
2424
import { postHook as xvmPostHook } from '@hap-toolkit/dsl-xvm'
2525
import ManifestWatchPlugin from '../plugins/manifest-watch-plugin'
26-
import { resolveEntries, resolveCardMinVersion } from '../utils'
26+
import { resolveEntries } from '../utils'
2727
import getDevtool from './get-devtool'
2828
import {
2929
getConfigPath,
@@ -151,10 +151,9 @@ export default async function genWebpackConf(launchOptions, mode) {
151151
setAdaptForV8Version(compileOptionsObject.disableScriptV8V65, manifest, cwd)
152152

153153
const isProduction = mode === 'production'
154-
const isCardMinVersion = resolveCardMinVersion(manifest)
155154

156155
// 页面文件
157-
const entries = resolveEntries(manifest, SRC_DIR, cwd, isCardMinVersion)
156+
const entries = resolveEntries(manifest, SRC_DIR, cwd)
158157

159158
// 环境变量
160159
const env = {
@@ -498,8 +497,7 @@ export default async function genWebpackConf(launchOptions, mode) {
498497
workers,
499498
cwd,
500499
originType: compileOptionsObject.originType,
501-
ideConfig: launchOptions.ideConfig,
502-
isCardMinVersion
500+
ideConfig: launchOptions.ideConfig
503501
},
504502
quickappConfig
505503
)

packages/hap-toolkit/src/utils.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const NOT_ALLOW_CHARS = [
3838
* @param {String} cwd - 工作目录
3939
* @returns {Array<Object>}
4040
*/
41-
export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
41+
export function resolveEntries(manifest, basedir, cwd) {
4242
if (!manifest.router) {
4343
throw Error('manifest.json 中未配置路由!')
4444
}
@@ -110,7 +110,7 @@ export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
110110
if (type === ENTRY_TYPE.CARD) {
111111
if (conf.type === 'lite') {
112112
sourceFile += '&lite=1' // lite card
113-
} else if (isCardMinVersion) {
113+
} else if (conf.minCardPlatformVersion) {
114114
sourceFile += '&newJSCard=1' // new jscard
115115
}
116116
}
@@ -127,22 +127,3 @@ export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
127127
}
128128
return entries
129129
}
130-
export function resolveCardMinVersion(manifest) {
131-
if (!manifest.router) {
132-
throw Error('manifest.json 中未配置路由!')
133-
}
134-
let isCardMinVersion = false
135-
// 卡片配置
136-
const widgetsConf = manifest.router.widgets || {}
137-
138-
for (let key in widgetsConf) {
139-
if (!widgetsConf[key].type || widgetsConf[key].type === 'js') {
140-
// 针对 JS 卡的兼容
141-
if (widgetsConf[key].minCardPlatformVersion) {
142-
// 任意有一个 JS 卡写了 minCardPlatformVersion 字段
143-
isCardMinVersion = true
144-
}
145-
}
146-
}
147-
return isCardMinVersion
148-
}

0 commit comments

Comments
 (0)