Foreword
this article API>= 13, based on DevEco Studio 5.1.0 Release, version number 5.1.0.828
in the previous article, we realized the operation of a single module for the modules in Hongmeng development, using the form of hvigor plug-in. Through hvigor plug-in, we can see that it automatically executes some specific tasks, reduces our manual intervention, and ensures the consistent reliability of the construction process. It can be said that in actual development, our development efficiency has been greatly improved. Today, our article, implement an hvigor plug-in together.
At present, there are two ways to implement the hvigor plug-in, one is based on the hvigorfile script, and the other is based on the typescript project. There is a certain difference between the two. The hvigorfile script form can be written directly into the project, which is convenient for us to change the script. The typescript project needs to be released before it can be used. If you want to develop the plug-in for more projects or more developers, recommend typescript project form, such as the componentized running plug-in in the previous article, users can use it with simple configuration. If it is only a single project and you want to use it conveniently, you recommend use hvigorfile script.
The main differences between the two are as follows:
based on hvigorfile script development
this development method is directly edited and developed in the hvigorfile.ts file of the project or the hvigorfile.ts file under the module. It can be said that it is very convenient and does not require other additional configuration, but it also has potential shortcomings, that is, it is not convenient to reuse plug-ins in multiple projects.
Generally speaking, it is very simple to implement. We can implement it in only three steps.
Step 1: Import Interface
import { HvigorPlugin, HvigorNode } from '@ohos/hvigor';
step 2: Implement a Custom plug-in
function myPlugin(): HvigorPlugin {
return {
pluginId: 'myPlugin',
apply(node: HvigorNode) {
console.log('I am a simple plugin');
}
}
}
step 3: using plug-ins
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [
myPlugin()
]
}
full Code
import { appTasks } from '@ohos/hvigor-ohos-plugin';
import { HvigorPlugin, HvigorNode } from '@ohos/hvigor';
export default {
system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
plugins: [
myPlugin()
]
}
function myPlugin(): HvigorPlugin {
return {
pluginId: 'myPlugin',
apply(node: HvigorNode) {
console.log('I am a simple plugin');
}
}
}
after the above steps are completed, when we compile or run the module, we will first execute our plug-in body:
for example, here, we can do many actions, such as code checking, configuration checking, specification checking, etc., and you can implement anything you want to do before building.
Project development based on typescript
although the hvigorfile-based script development method is very convenient, it is not easy to expand. If you want more projects or developers to use it quickly, you still need typescript project development.
The first step: initialize the typescript project
installing the typescript module
npm install typescript -g
create a new directory in which to initialize the npm project
npm init
when executing npm init, there will be many steps. We can directly use the default and press Enter. We can also show off this information separately after it is generated.
After the above command is executed, a package.json file will be created in the newly created directory. This is the information in the command:
generate typescript configuration file
tsc --init
the generation information is as follows:
step 2: Develop the plug-in
After the first step is completed, we can start plug-in development. First, configure the npm mirror warehouse address. We can create or open the. npmrc file in the user directory and configure the following information:
registry=https://repo.huaweicloud.com/repository/npm/
@ohos:registry=https://repo.harmonyos.com/npm/
after the above information configuration is completed, open the package.json file generated in the first step and configure the following information in devDependencies:
"devDependencies": {
"@ohos/hvigor": "5.2.2"
}
after the configuration is completed, execute the following command to download the resource package:
npm install
after executing the above commands, we can create our own ts file for plug-in code writing, writing, and the development method based on the hvigorfile script is basically the same.
import { HvigorNode, HvigorPlugin } from '@ohos/hvigor';
export function myPlugin(): HvigorPlugin {
return {
pluginId: 'myPlugin',
apply(node: HvigorNode) {
Console.log ('I am a simple plugin');
}
}
}
After the plug-in is complete, then create the index.ts file and declare the export of the plug-in method in the file, as follows
export { myPlugin } from './src/plugin/my-plugin';
step 3: Publish the plug-in
the developed plug-in needs to be released to the npm platform for others to use. The release method also follows the npm release specification. Here is a brief summary. First, configure the pm mirror warehouse address, create a. npmrc file in the user directory, and configure the mirror warehouse you need to release.
registry=[npm address]
After configuring the pm mirror warehouse address, log in to npm and execute the following command. If you do not have an account, please register with npm.
npm login
After the login is complete, you can publish. Execute the following command to package and publish the npm project to the Mirror repository.
npm publish
Step 4: Use the plug-in
the use of plug-ins is very simple. There has been an overview in the previous article. You can check the previous article.
Related Summary
It should be noted that the core logic of the two methods is basically the same, and both Task methods are written in ts files. The main difference is that in the plug-in reuse mechanism, if a single project is recommend if the hvigorfile-based script development method is shared with others, you recommend use the typescript-based project development method.
This article label: Hongmeng Development Tools/DevEco Studio
Top comments (0)