Skip to content
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
8 changes: 3 additions & 5 deletions packages/hap-packager/src/plugins/card-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CardPlugin {
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
() => {
let { pathSrc, isCardMinVersion } = this.options
let { pathSrc } = this.options
pathSrc = pathSrc.replace(/\\/g, '/')
const moduleGraph = compilation.moduleGraph
for (const chunk of compilation.chunks) {
Expand All @@ -47,6 +47,7 @@ class CardPlugin {
continue
}
const { rawRequest: entryRawRequest, request } = entryModule
// 只有轻卡和新打包格式的JS卡会处理template和style
if (this.needExtractTemplateAndStyle(entryRawRequest)) {
const { templateFileName, cssFileName, bundleFilePath } = this.getCardBuildPath(
request,
Expand All @@ -68,12 +69,9 @@ class CardPlugin {

if (this.isLiteCard(entryRawRequest)) {
templateRes = postHandleLiteCardRes(templateRes)
} else if (isCardMinVersion) {
} else {
// 快应用 2.0 标准的 JS 卡,输出 .template.json 和 .css.json
templateRes = postHandleJSCardRes(templateRes)
} else {
// 非快应用 2.0 标准的 JS 卡,不输出 .template.json 和 .css.json
continue
}

// 用于修改 template 的 key 的 stringify 的顺序,type 放第一个,children 放最后一个
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class CardScriptHandlePlugin {
continue
}
const { rawRequest: entryRawRequest, request } = entryModule
// 只有新打包格式的JS卡会进行script的处理
if (this.isNewJsCard(entryRawRequest)) {
const { templateFileName, cssFileName } = this.getCardBuildPath(request, pathSrc)
const jsAssetesName = this.getJsAssetsName(request, pathSrc)
Expand Down
1 change: 1 addition & 0 deletions packages/hap-packager/src/plugins/remove-modules-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RemoveModulesPlugin {
compilation.hooks.optimizeModules.tap('RemoveModulesPlugin', (modules) => {
modules.forEach((module) => {
const { _source, request } = module
// 只有新打包格式的JS卡会进行script的处理
if (this.isNewJsCardAndCardcomp(request)) {
const { _valueAsString, _valueAsBuffer } = _source || {}
const moduleSource = _valueAsString || _valueAsBuffer?.toString() || ''
Expand Down
5 changes: 4 additions & 1 deletion packages/hap-packager/src/plugins/resource-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ function minifyWidgetI18nJSONFiles(targetDir) {
}
const widgetPath = widgetsOption[key].path
const dir = path.join(targetDir, widgetPath, 'i18n')
// 轻卡和写了minCardPlatformVersion的新打包格式的JS卡会进行多语言扁平化处理
const needFlatten =
widgetsOption[key].type === 'lite' || widgetsOption[key].minCardPlatformVersion
if (fs.existsSync(dir)) {
const jsonFiles = getFiles('*.json', dir)
jsonFiles.forEach((filePath) => {
arr.push(filePath)
minifyJson(filePath, filePath, true)
minifyJson(filePath, filePath, needFlatten)
})
}
}
Expand Down
11 changes: 4 additions & 7 deletions packages/hap-packager/src/webpack.post.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ function postHook(webpackConf, defaultsOptions, quickappConfig = {}) {
subpackages,
workers,
originType,
useTreeShaking,
isCardMinVersion
useTreeShaking
} = defaultsOptions

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

if (isCardMinVersion) {
// 如果工程中有卡片配置为快应用 2.0 卡片,则使用插件抽取 template 和 style 的 json
webpackConf.plugins.push(new CardScriptHandlePlugin({ pathSrc }), new RemoveModulesPlugin())
}
webpackConf.plugins.push(
new CardPlugin({ pathSrc, isCardMinVersion }),
new CardScriptHandlePlugin({ pathSrc }),
new RemoveModulesPlugin(),
new CardPlugin({ pathSrc }),
// 框架Handler包装
new HandlerPlugin({
pathSrc: pathSrc,
Expand Down
8 changes: 3 additions & 5 deletions packages/hap-toolkit/src/gen-webpack-conf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { name, postHook as packagerPostHook } from '@hap-toolkit/packager'
import { postHook as xvmPostHook } from '@hap-toolkit/dsl-xvm'
import ManifestWatchPlugin from '../plugins/manifest-watch-plugin'
import { resolveEntries, resolveCardMinVersion } from '../utils'
import { resolveEntries } from '../utils'
import getDevtool from './get-devtool'
import {
getConfigPath,
Expand Down Expand Up @@ -151,10 +151,9 @@ export default async function genWebpackConf(launchOptions, mode) {
setAdaptForV8Version(compileOptionsObject.disableScriptV8V65, manifest, cwd)

const isProduction = mode === 'production'
const isCardMinVersion = resolveCardMinVersion(manifest)

// 页面文件
const entries = resolveEntries(manifest, SRC_DIR, cwd, isCardMinVersion)
const entries = resolveEntries(manifest, SRC_DIR, cwd)

// 环境变量
const env = {
Expand Down Expand Up @@ -498,8 +497,7 @@ export default async function genWebpackConf(launchOptions, mode) {
workers,
cwd,
originType: compileOptionsObject.originType,
ideConfig: launchOptions.ideConfig,
isCardMinVersion
ideConfig: launchOptions.ideConfig
},
quickappConfig
)
Expand Down
23 changes: 2 additions & 21 deletions packages/hap-toolkit/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const NOT_ALLOW_CHARS = [
* @param {String} cwd - 工作目录
* @returns {Array<Object>}
*/
export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
export function resolveEntries(manifest, basedir, cwd) {
if (!manifest.router) {
throw Error('manifest.json 中未配置路由!')
}
Expand Down Expand Up @@ -110,7 +110,7 @@ export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
if (type === ENTRY_TYPE.CARD) {
if (conf.type === 'lite') {
sourceFile += '&lite=1' // lite card
} else if (isCardMinVersion) {
} else if (conf.minCardPlatformVersion) {
sourceFile += '&newJSCard=1' // new jscard
}
}
Expand All @@ -127,22 +127,3 @@ export function resolveEntries(manifest, basedir, cwd, isCardMinVersion) {
}
return entries
}
export function resolveCardMinVersion(manifest) {
if (!manifest.router) {
throw Error('manifest.json 中未配置路由!')
}
let isCardMinVersion = false
// 卡片配置
const widgetsConf = manifest.router.widgets || {}

for (let key in widgetsConf) {
if (!widgetsConf[key].type || widgetsConf[key].type === 'js') {
// 针对 JS 卡的兼容
if (widgetsConf[key].minCardPlatformVersion) {
// 任意有一个 JS 卡写了 minCardPlatformVersion 字段
isCardMinVersion = true
}
}
}
return isCardMinVersion
}