提交 21547aeb 编写于 作者: fxy060608's avatar fxy060608

fix(mp): support condition (#3079)

上级 0152af33
......@@ -256,7 +256,13 @@ declare namespace UniApp {
}
condition?: {
current?: number
list?: { name?: string; path: string; query?: string }[]
list?: {
id?: string | number
name?: string
path?: string
pathName?: string
query?: string
}[]
}
}
......
import { hasOwn } from '@vue/shared'
import { hasOwn, isArray } from '@vue/shared'
import { parseJson } from '../json'
interface ParseMiniProgramProjectJsonOptions {
template: Record<string, any>
pagesJson: UniApp.PagesJson
}
interface ProjectConfig {
projectname: string
condition?: {
miniprogram?: UniApp.PagesJson['condition']
}
}
const projectKeys = [
......@@ -31,20 +35,57 @@ export function isMiniProgramProjectJsonKey(name: string) {
export function parseMiniProgramProjectJson(
jsonStr: string,
platform: UniApp.PLATFORM,
{ template }: ParseMiniProgramProjectJsonOptions
{ template, pagesJson }: ParseMiniProgramProjectJsonOptions
) {
const project = JSON.parse(JSON.stringify(template)) as ProjectConfig
const projectJson = JSON.parse(JSON.stringify(template)) as ProjectConfig
const manifestJson = parseJson(jsonStr)
if (manifestJson) {
project.projectname = manifestJson.name
projectJson.projectname = manifestJson.name
const platformConfig = manifestJson[platform]
if (platformConfig) {
projectKeys.forEach((name) => {
if (hasOwn(platformConfig, name)) {
;(project as Record<string, any>)[name] = platformConfig[name]
;(projectJson as Record<string, any>)[name] = platformConfig[name]
}
})
}
}
return project
// 其实仅开发期间 condition 生效即可,暂不做判断
const miniprogram = parseMiniProgramCondition(pagesJson)
if (miniprogram) {
if (!projectJson.condition) {
projectJson.condition = {}
}
projectJson.condition.miniprogram = miniprogram
}
return projectJson
}
function parseMiniProgramCondition(pagesJson: UniApp.PagesJson) {
const launchPagePath = process.env.UNI_CLI_LAUNCH_PAGE_PATH || ''
if (launchPagePath) {
return {
current: 0,
list: [
{
id: 0,
name: launchPagePath, // 模式名称
pathName: launchPagePath, // 启动页面,必选
query: process.env.UNI_CLI_LAUNCH_PAGE_QUERY || '', // 启动参数,在页面的onLoad函数里面得到。
},
],
}
}
const condition = pagesJson.condition
if (!condition || !isArray(condition.list) || !condition.list.length) {
return
}
condition.list.forEach(function (item, index) {
item.id = item.id || index
if (item.path) {
item.pathName = item.path
delete item.path
}
})
return condition
}
......@@ -5,6 +5,7 @@ import {
defineUniManifestJsonPlugin,
getLocaleFiles,
parseMiniProgramProjectJson,
parsePagesJsonOnce,
} from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '../plugin'
......@@ -21,6 +22,7 @@ export function uniManifestJsonPlugin(
return
}
const inputDir = process.env.UNI_INPUT_DIR
const platform = process.env.UNI_PLATFORM
this.addWatchFile(path.resolve(inputDir, 'manifest.json'))
getLocaleFiles(path.resolve(inputDir, 'locale')).forEach((filepath) => {
this.addWatchFile(filepath)
......@@ -40,11 +42,10 @@ export function uniManifestJsonPlugin(
}
template.setting.urlCheck = false
}
projectJson = parseMiniProgramProjectJson(
code,
process.env.UNI_PLATFORM,
{ template }
)
projectJson = parseMiniProgramProjectJson(code, platform, {
template,
pagesJson: parsePagesJsonOnce(inputDir, platform),
})
} else {
// 无需解析,直接拷贝,如 quickapp-webview
projectJson = template
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册