This document will show you how to build an extension step by step.

Create Extension

In HBuilderX, Click the New button on the toolbar or the shortcut key ctrl+n to open the new menu and select Project.

Note: The extension name can only use English letters and numbers, and spaces are not supported

Create Extension - 图1

Run Extension

  1. After creating the extension project, select the project or open a file under the project
  2. Click the Run button on the toolbar or the shortcut key ctrl+r to open the run menu
  3. Select Run This Extension: xxx to open a new HBuilderX form, and this new form will automatically load the extension.

Create Extension - 图2

The display results are as follows:

Create Extension - 图3

Extension development template example, register a Hello World menu on the right-click menu of the code editor.

Create Extension - 图4

At this time, we open a document, and then right-click to see the Hello World menu. After clicking the menu to run, a prompt box will be displayed in the window.

Create Extension - 图5

Note: After modifying the extension code, you need to re-run the extension.

Log

The running extension will print the log in the console of the old Window.

You can use APIs such as console.log and console.error to print information to the debugging console of the old Window.

Extension description

package.json

package.json must exist in every extension, contains extension information and configuration contribution points.

The following is the package.json code description of the sample extension.

Note: that you cannot directly copy this code into the editor, and package.json currently does not support comments. The comments in this code are only used to explain the code.

  1. {
  2. "name": "your extension name",
  3. "displayName": "your display name",
  4. "description": "your extension description",
  5. "version": "0.0.0",
  6. "publisher": "your name",
  7. "engines": {
  8. "HBuilderX": "^2.6.8"
  9. },
  10. "categories": [
  11. "Other"
  12. ],
  13. "main": "./extension",
  14. "activationEvents": [
  15. "onCommand:extension.helloWorld"
  16. ],
  17. "contributes": {
  18. "commands": [{
  19. "command": "extension.helloWorld",
  20. "title": "Hello World"
  21. }],
  22. "menus": {
  23. "editor/context": [
  24. {
  25. "command": "extension.helloWorld",
  26. "group": "z_commands",
  27. "when": "editorTextFocus"
  28. },
  29. {
  30. "group": "z_commands"
  31. }
  32. ]
  33. }
  34. },
  35. "dependencies": {}
  36. }

extension entry file

The extension entry file in this example is extension.js, and the extension entry file must have the exports activate method, which is called when the extension is activated.

  1. var hx = require("hbuilderx");
  2. function activate(context) {
  3. let disposable = hx.commands.registerCommand('extension.helloWorld', () => {
  4. hx.window.showInformationMessage('Hello,this is my first extension。');
  5. console.log("Hello My First HBuilderX Extension.");
  6. });
  7. context.subscriptions.push(disposable)
  8. };
  9. function deactivate() {
  10. };
  11. module.exports = {
  12. activate,
  13. deactivate
  14. }