From bf1d711196675388734ddf614a5f8a9c5340d1e4 Mon Sep 17 00:00:00 2001 From: wkj <278599135@qq.com> Date: Sun, 12 Nov 2023 20:34:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81json=E4=B8=AD=20componentGenerics=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/WechatMpPlugin.js | 71 +++++++++++++++++--- template/wechatMp/src/app.ts | 15 +---- template/wechatMp/src/configuration/index.ts | 4 +- template/wechatMp/src/initialize.ts | 18 +++++ template/wechatMp/src/utils/env.ts | 5 ++ 5 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 template/wechatMp/src/initialize.ts create mode 100644 template/wechatMp/src/utils/env.ts diff --git a/plugins/WechatMpPlugin.js b/plugins/WechatMpPlugin.js index 0494c88..fc38541 100644 --- a/plugins/WechatMpPlugin.js +++ b/plugins/WechatMpPlugin.js @@ -252,9 +252,8 @@ class OakWeChatMpPlugin { async getComponents(components, instance) { try { const { debugPanel } = this.options; - const { usingComponents = {} } = fsExtra.readJSONSync( - `${instance}.json` - ); + const { usingComponents = {}, componentGenerics = {} } = + fsExtra.readJSONSync(`${instance}.json`); const instanceDir = path.parse(instance).dir; for (const k of Object.keys(usingComponents)) { @@ -281,6 +280,32 @@ class OakWeChatMpPlugin { continue; } + const aliasPath = this.getAliasPath(c); + if (aliasPath) { + if (!components.has(aliasPath)) { + components.add(aliasPath); + await this.getComponents(components, aliasPath); + } + } else { + const component = replaceDoubleSlash( + path.resolve(instanceDir, c) + ); + if (!components.has(component)) { + const component2 = replaceDoubleSlash( + path.resolve(this.basePath, component) + ); + if (!components.has(component2)) { + components.add(component2); + await this.getComponents(components, component); + } + } + } + } + // 抽象节点的默认组件 + for (const k of Object.keys(componentGenerics)) { + const componentGeneric = componentGenerics[k]; + const c = componentGeneric.default; + const aliasPath = this.getAliasPath(c); if (aliasPath) { if (!components.has(aliasPath)) { @@ -565,13 +590,13 @@ class OakWeChatMpPlugin { let usingComponents = {}; if (appJson.usingComponents) { - for (let ck of Object.keys(appJson.usingComponents)) { - let component = appJson.usingComponents[ck]; - if (ck === debugPanel.name && !debugPanel.show) { + for (let c of Object.keys(appJson.usingComponents)) { + let component = appJson.usingComponents[c]; + if (c === debugPanel.name && !debugPanel.show) { continue; } component = this.getReplaceAlias(component); - usingComponents[ck] = component; + usingComponents[c] = component; } appJson.usingComponents = usingComponents; } @@ -591,8 +616,8 @@ class OakWeChatMpPlugin { let usingComponents = {}; if (json.usingComponents) { - for (let ck of Object.keys(json.usingComponents)) { - let component = json.usingComponents[ck]; + for (let c of Object.keys(json.usingComponents)) { + let component = json.usingComponents[c]; const alias = this.getAlias(component); if (alias) { @@ -607,11 +632,37 @@ class OakWeChatMpPlugin { ) ); } - usingComponents[ck] = component; + usingComponents[c] = component; } json.usingComponents = usingComponents; } + let componentGenerics = {}; + if (json.componentGenerics) { + for (let c of Object.keys(json.componentGenerics)) { + const componentGeneric = json.componentGenerics[c]; + let component = componentGeneric.default; + + const alias = this.getAlias(component); + if (alias) { + component = this.getReplaceAlias(component); + + const parentPath = path.resolve(this.basePath, entry); + const parentDir = path.parse(parentPath).dir; + component = replaceDoubleSlash( + path.relative( + parentDir, + path.resolve(this.basePath, component) + ) + ); + } + componentGenerics[c] = Object.assign(componentGeneric, { + default: component, + }); + } + json.componentGenerics = componentGenerics; + } + // 将locales中的pageTitle复制到小程序中的navigationBarTitleText const parsed = path.parse(assets); if (parsed.name === 'index') { diff --git a/template/wechatMp/src/app.ts b/template/wechatMp/src/app.ts index ff557f8..4153754 100644 --- a/template/wechatMp/src/app.ts +++ b/template/wechatMp/src/app.ts @@ -1,19 +1,10 @@ import './utils/polyfill'; -import initialize from '@project/initialize'; import { handler as exceptionHandler } from '@project/exceptionHandler'; -import { createComponent } from '@project/page'; import { compareVersion } from 'oak-domain/lib/utils/version'; -/** - * 初始化,小程序这里必须输入访问的目标域名,系统根据domain和system的关系来判定appId - */ -import { host, sdVersion } from './configuration'; -const { features } = initialize('wechatMp', host); -Object.assign(global, { - features, - OakComponent: (options: any) => createComponent(options, features), -}); +import { sdkVersion } from './configuration'; +import { features } from './initialize'; export interface IAppOption { globalData: { @@ -28,7 +19,7 @@ App({ async onLaunch() { //微信SDKVersion比较,检查小程序有没有新版本发布 const curVersion = wx.getSystemInfoSync().SDKVersion; - if (compareVersion(curVersion, sdVersion) >= 0) { + if (compareVersion(curVersion, sdkVersion) >= 0) { const updateManager = wx.getUpdateManager(); updateManager.onCheckForUpdate(({ hasUpdate }) => { if (hasUpdate) { diff --git a/template/wechatMp/src/configuration/index.ts b/template/wechatMp/src/configuration/index.ts index 8186bef..aad5856 100644 --- a/template/wechatMp/src/configuration/index.ts +++ b/template/wechatMp/src/configuration/index.ts @@ -12,9 +12,9 @@ const URL = { // 服务器地址数组,和domain中 }; const host = URL[envVersion]; -const sdVersion = '2.25.1'; // 小程序运行所需要的sdk最低版本 +const sdkVersion = '2.25.1'; // 小程序运行所需要的sdk最低版本 export { host, - sdVersion, + sdkVersion, }; \ No newline at end of file diff --git a/template/wechatMp/src/initialize.ts b/template/wechatMp/src/initialize.ts new file mode 100644 index 0000000..5637202 --- /dev/null +++ b/template/wechatMp/src/initialize.ts @@ -0,0 +1,18 @@ + +import initialize from '@project/initialize'; +import { createComponent } from '@project/page'; +import { host } from './configuration'; +import { getAppType } from './utils/env'; + +const appType = getAppType(); + +const { features } = initialize(appType, host); + +Object.assign(global, { + features, + OakComponent: (options: any) => createComponent(options, features), +}); + +export { + features, +}; diff --git a/template/wechatMp/src/utils/env.ts b/template/wechatMp/src/utils/env.ts new file mode 100644 index 0000000..b4b76ef --- /dev/null +++ b/template/wechatMp/src/utils/env.ts @@ -0,0 +1,5 @@ +type AppType = 'wechatMp'; + +export function getAppType() { + return 'wechatMp' as AppType; +} \ No newline at end of file