小程序 支持json中 componentGenerics的编译

This commit is contained in:
Wang Kejun 2023-11-12 20:34:48 +08:00
parent 1d1328cb5d
commit bf1d711196
5 changed files with 89 additions and 24 deletions

View File

@ -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') {

View File

@ -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<IAppOption>({
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) {

View File

@ -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,
};

View File

@ -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,
};

View File

@ -0,0 +1,5 @@
type AppType = 'wechatMp';
export function getAppType() {
return 'wechatMp' as AppType;
}