HarmonyOS鸿蒙Next中使用HMRouter编译插件导致模块动态import失败

HarmonyOS鸿蒙Next中使用HMRouter编译插件导致模块动态import失败

项目中使用 hvigorconfig.ts 动态编译引入子模块

function moduleIncludeSetting(){
  console.log('hello moduleIncludeSetting!');

  const hvigorConfig = hvigor.getHvigorConfig();

  const modules = parseJson();
  modules.forEach((module, index) => {
    console.log('  Git Module:', module.git.module);
    console.log('  Git Path:', module.git.path);
    console.log('  Git Branch:', module.git.branch);

    hvigorConfig.includeNode(module.git.module, `./${module.git.path}`);
  });
}
moduleIncludeSetting()

common 的 hvigorfile.ts 中动态引入子模块

export function moduleLoaderPlugin(): HvigorPlugin {
  return {
    pluginId: 'moduleLoaderPlugin',
    async apply(currentNode: HvigorNode): Promise<void> {

      console.log('hello common moduleLoaderPlugin!');

      const harContext = currentNode.getContext(OhosPluginId.OHOS_HAR_PLUGIN) as OhosHarContext;
      const dependency = harContext.getDependenciesOpt(); //获取dependency依赖

      const modules = parseJson();
      modules.forEach((module, index) => {
        console.log('  Git Module:', module.git.module);
        console.log('  Git Path:', module.git.path);
        console.log('  Git Branch:', module.git.branch);

        dependency[module.git.module] = `file:../../${module.git.path}`;
      });
      harContext.setDependenciesOpt(dependency);
      console.log(harContext.getDependenciesOpt());
    }
  }
}

export default {
  system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  plugins: [
    moduleLoaderPlugin(),
  ]         /* Custom plugin to extend the functionality of Hvigor. */
}

common 中有 ProtocolManager 用于 动态import 模块 进行初始化

export class ProtocolManager {
  private static sInstance: ProtocolManager;

  static getInstance() {
    if (!ProtocolManager.sInstance) {
      ProtocolManager.sInstance = new ProtocolManager();
    }
    return ProtocolManager.sInstance;
  }

  private static TAG_TEST = 'biz_test';
  private static TAG_WEB = 'biz_web';

  constructor() {
    Logger.info('ProtocolManager init');

    this.importProtocol();
  }

  private async importProtocol() {
    Logger.info('ProtocolManager importProtocol');
    await import(ProtocolManager.TAG_TEST).then((ns: ESObject) => {
      Logger.info(`ProtocolManager importProtocol ${ProtocolManager.TAG_TEST}`);
    });
    await import(ProtocolManager.TAG_WEB).then((ns: ESObject) => {
      Logger.info(`ProtocolManager importProtocol ${ProtocolManager.TAG_WEB}`);
    });

  }
}

当 entry 引用 common 时,上面的代码可以动态初始化

当 entry 加入使用 HMRouter 的编译插件时

import { hapTasks } from '@ohos/hvigor-ohos-plugin';
import { hapPlugin } from '@hadss/hmrouter-plugin';

export default {
    system: hapTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
    plugins: [hapPlugin()] 
}

ProtocolManager 中用于 动态import 模块 就无法正常初始化


更多关于HarmonyOS鸿蒙Next中使用HMRouter编译插件导致模块动态import失败的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中使用HMRouter编译插件时,如果遇到模块动态import失败的问题,可能是由于以下原因导致的:

  1. HMRouter插件兼容性问题:HMRouter插件可能与当前版本的HarmonyOS Next不完全兼容,导致动态import无法正常工作。

  2. 模块路径配置错误:在HMRouter的配置中,模块路径可能未正确指定,导致动态import时无法找到对应的模块。

  3. 编译时优化冲突:HMRouter插件在编译时可能进行了某些优化,这些优化与HarmonyOS Next的编译机制产生冲突,进而影响动态import的执行。

  4. 依赖版本不匹配:项目中使用的HMRouter插件版本与HarmonyOS Next的依赖版本不匹配,导致动态import功能无法正常使用。

  5. 插件配置缺失或错误:HMRouter插件的配置文件可能缺少必要的参数或配置错误,导致动态import失败。

  6. HarmonyOS Next的模块加载机制限制:HarmonyOS Next的模块加载机制可能与HMRouter插件的动态import实现方式存在冲突,导致无法正常加载模块。

解决此类问题时,通常需要检查HMRouter插件的配置文件、模块路径、依赖版本等,确保它们与HarmonyOS Next的兼容性。

更多关于HarmonyOS鸿蒙Next中使用HMRouter编译插件导致模块动态import失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中使用HMRouter编译插件时,若遇到模块动态import失败的问题,可能是由于以下原因导致:

  1. 编译配置问题:检查babel.config.jstsconfig.json中的编译配置,确保动态import语法被正确支持。
  2. 插件兼容性:确认HMRouter插件版本与HarmonyOS Next版本兼容,必要时升级或降级插件。
  3. 路径解析问题:动态import的模块路径需确保正确,避免相对路径或绝对路径解析错误。
  4. 代码分割配置:检查webpackrollup等打包工具的配置,确保代码分割功能正常。

建议逐一排查以上问题,或参考官方文档进行配置调整。

回到顶部