hvigor自定义扩展demo

如在default@PreBuild或者其他task之前/之后写任务。

HarmonyOS
2024-05-28 22:07:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
幸运小面

使用的核心API

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

核心代码

例如在entry模块级hvigorfile.ts中设置:

import { hapTasks } from '@ohos/hvigor-ohos-plugin'; 
 
export function add(a: number, b: number) { 
  return a + b; 
} 
 
export function customPluginFunction1(str?: string) { 
  return { 
    pluginId: 'CustomPluginID1', 
    apply(pluginContext) { 
      //注册自定义任务 接口pluginContext 方法registerTask 
      pluginContext.registerTask({ 
        // 编写自定义任务 
        name: 'customTask1', 
        run: (taskContext) => { 
          // taskContext.moduleName; 
          // taskContext.modulePath; 
          // add(1,2); 
          // console.info(add(1,2)); 
          //接口 taskContext  模块名称 moduleName 模块的绝对路径 modulePath 
          console.log('customTask1: ', taskContext.moduleName, taskContext.modulePath); 
        }, 
        // 确认自定义任务插入位置 
        dependencies: ['default@BuildJS'], 
        postDependencies: ['default@CompileArkTS'] 
      }) 
    } 
  } 
} 
 
export function customPluginFunction2(str?: string) { 
  return { 
    pluginId: 'CustomPluginID2', 
    apply(pluginContext) { 
      pluginContext.registerTask({ 
        name: 'customTask2', 
        run: (taskContext) => { 
          console.log('customTask2: ', taskContext.moduleName, taskContext.modulePath); 
        }, 
        dependencies: ['default@BuildJS'], 
        postDependencies: ['default@CompileArkTS'] 
      }) 
    } 
  } 
} 
 
export function customPluginFunction3(str?: string) { 
  return { 
    pluginId: 'CustomPluginID3', 
    apply(pluginContext) { 
      pluginContext.registerTask({ 
        name: 'customTask3', 
        run: (taskContext) => { 
          console.log('customTask3: ', taskContext.moduleName, taskContext.modulePath); 
        }, 
        // dependencies: ['default@BuildJS'], 
        // postDependencies: ['default@CompileArkTS'] 
        dependencies: ['customTask1'], 
        postDependencies: ['customTask2'] 
 
      }) 
    } 
  } 
} 
 
export default { 
  system: hapTasks, // Hvigor内置插件,不可修改 
  plugins: [customPluginFunction1(), customPluginFunction2(), customPluginFunction3()]       // 自定义插件 
  // plugins: [customPluginFunction1()]       // 自定义插件 
}

实现效果

分享
微博
QQ
微信
回复
2024-05-29 23:23:36
相关问题
多module场景Hvigor自定义扩展咨询
504浏览 • 1回复 待解决
自定义hvigor任务,定制编译产物
716浏览 • 1回复 待解决
hvigor自定义任务中使用npm包
482浏览 • 1回复 待解决
Hvigor自定义编译任务如何引用三方库
917浏览 • 1回复 待解决
如何在hvigor自定义任务中使用npm包
674浏览 • 1回复 待解决
自定义弹窗自定义转场动画
691浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
266浏览 • 1回复 待解决
自定义组件嵌套子组件
9067浏览 • 3回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
199浏览 • 1回复 待解决
如何自定义模拟Tabs组件
623浏览 • 1回复 待解决
自定义弹窗如何嵌套使用
889浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
366浏览 • 1回复 待解决
HarmonyOS Checkbox如何自定义图标
50浏览 • 0回复 待解决
ArkTs如何自定义容器组件?
2709浏览 • 1回复 待解决
如何自定义组件原型菜单
649浏览 • 1回复 待解决