# Develop gadgets using Taro

**Taro** is an open solution that allows developers to develop gadgets using different frameworks and on different terminals. Taro can improve development efficiency and break the limitations of the operating platforms, reducing maintenance costs.

-   Supports common development frameworks. As editing with native code of gadgets is very complicated, Taro allows you to develop gadgets using **React**, **Vue**, **Vue 3**, or **Nerv**.
- Runs one set of code on multiple platforms. Taro supports conversion to H5, ReactNative, and any gadget platform. When you have to provide services on different platforms at the same time, it's very cost-consuming to write multiple sets of code for different terminals. With Taro, you only need to write one set of code that can be used on multiple terminals.

# Create a Feishu app

Operating platform: [Feishu Open Platform > Developer Console](https://open.feishu.cn/app/)

## Create a Feishu app

Go to [Feishu Open Platform > Developer Console](http://open.feishu.cn/app) and create an app named `hello-world`.

After the creation is complete, the app will be added to the company's custom app directory. Enter the details page to see the new app.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/864fe2a05af0cda1a9fc88c463e0b892_VaQusMqsGd.png?height=382&lazyload=true&width=1640)

On the **App Details** page, click **Credentials & Basic Info** on the left menu, view the App ID (a unique identifier of the app) under **App Credentials** on the right, and click to copy the App ID.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/bccb033f2e3795b59287d9ae187d338a_12s4LnzwMZ.png?height=456&lazyload=true&width=1635)

## Configure the Feishu app

-   Configure collaborative members

If your app requires the collaboration of multiple members, you can add administrators, developers, and operating personnel in "Collaborative Members."

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/6f8aa4c3e10bdd27ec2f3d2523c18baf_4rfAbMTdTm.png?height=736&lazyload=true&width=1897)
-   Request permissions as needed

If you need to call an API provided by Feishu, request specific permissions in "Permissions & Scopes" based on the requirements of the API.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/00639961445d893110c657e069b144fc_BhwM3Qd9bk.png?height=813&lazyload=true&width=1889)

-   Subscribe to events as needed

Feishu pushes subscription events to your app, such as department changes and attendance notifications. If you want to be notified as soon as certain events occur, configure in the "Event Subscription" module by specifying the URL to which Feishu pushes the events and selecting specific events.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/4c001e63f5b940a546986693fa891230_uLqJ0pxS4J.png?height=675&lazyload=true&width=1870)

# Develop a gadget using Taro
Operating Manual: [Taro Development Manual](https://docs.taro.zone/docs/GETTING-STARTED)

## Install the Taro development tool

> Taro projects are based on node. Ensure that the node version is 12.0.0 or later. It is recommended that you use the Node.js version [nvm](https://github.com/creationix/nvm) management tool to manage node. This helps you easily switch to the node version and sudo is not required in global installation.

First, use npm or yarn to install `@tarojs/cli` globally, or use [npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b):

```
 # Use npm to install CLI
$ npm install -g @tarojs/cli

# Use yarn to install CLI
$ yarn global add @tarojs/cli

# If cnpm is installed, use cnpm to install CLI
$ cnpm install -g @tarojs/cli
```

## Project initialization

If you are creating a project for the first time, you can quickly create a template project through the CLI tool and code based on it.

```shell
taro init myApp
```

## Compile gadgets

From Taro v3.1 and later, a plugin is provided for compiling Feishu gadgets. For details, see [Developing Feishu Gadgets Using Taro](https://docs.taro.zone/docs/GETTING-STARTED#%E9%A3%9E%E4%B9%A6%E5%B0%8F%E7%A8%8B%E5%BA%8F).

-   Install the plugin

```
yarn add @tarojs/plugin-platform-lark
```

-   Configure the plugin. For how to use the plugin, click [here](https://github.com/NervJS/taro-plugin-platform-lark).

```js
config = {
  // ...
  plugins: [
    '@tarojs/plugin-platform-lark'
  ]
}
```

-   Configure the project

Create project.lark.json or project.tt.json (included in the official template) in the project root directory. The following shows the commonly used configuration. Set "appid" to the App ID obtained when creating the Feishu app. This will be used in debugging and uploading Feishu gadget later.

```json
{
  "miniprogramRoot": "./",
  "projectname": "taro-lark",
  "description": "taro-lark",
  "appid": "touristappid",
  "setting": {
    "urlCheck": true,
    "es6": true,
    "postcss": false,
    "minified": false
  },
  "compileType": "miniprogram"
}
```

- Adding the compile command

Add the following package command to package.json

```json
{
  "scripts": {
    "build:lark": "taro build --type lark",
    "dev:lark": "npm run build:lark -- --watch"
  }
}
```

-   Compile the project

```
# yarn
$ yarn dev:lark
$ yarn build:lark

# npm script
$ npm run dev:lark
$ npm run build:lark

# Global installation only
$ taro build --type lark --watch
$ taro build --type lark

# Available to npx users
$ npx taro build --type lark --watch
$ npx taro build --type lark

# Enable watch and compression
$ set NODE_ENV=production && taro build --type lark --watch # Windows
$ NODE_ENV=production taro build --type lark --watch # Mac
```

After the compilation is complete, you'll see the file directories as follows:

```
├── dist: compilation result directory
├── config: configuration directory
|   ├── dev.js: configuration when developing
|   ├── index.js: default configuration
|   └── prod.js: configuration when packaging
├── src: source code directory
|   ├── pages: page file directory
|   |   ├── index: index page directory
|   |   |   ├── index.js: index page logic
|   |   |   └── index.css: index page style
|   ├── app.css: general project style
|   └── app.js: project entry file
└── package.json
```

##

# Debug and upload the gadget

Operating tool: [Feishu Developer Tools](https://open.feishu.cn/document/uYjL24iN/ucDOzYjL3gzM24yN4MjN)

## Debug the Feishu gadget

1.  Download and install [Feishu Developer Tools](https://open.feishu.cn/document/uYjL24iN/ucDOzYjL3gzM24yN4MjN).
2.  Click "Import Project" and select the **dist directory** compiled by Taro. (Note: Don't import the entire project file directly.)

3.  After you open the project, close the "Editor". You cannot modify the dist file directly because it was compiled by Taro.

4.  Use the simulator and debugger to debug the gadget on Feishu Developer Tools.
5.  Click "Preview" or "Real Device Debugging" to run the gadget on a real device.

Note: To complete the above step, you need to log in to Feishu Developer Tools as a collaborator. For details, see "Configure Collaborative Members."

6.  If the result fails to meet your expectation, modify the code using Taro development tools, recompile and repeat the above steps. If the result meets your expectation, code development is complete.

## Upload the gadget package

After you complete the code development, preview, and debugging steps, upload the gadget code using Feishu Developer Tools.

# Release the Feishu app
Operating platform: [Feishu Open Platform > Developer Console](https://open.feishu.cn/app/)

## Enable the gadget

In the Developer Console, go to **App Details > App Features > Gadget**, and enable the gadget. Select the latest version of the gadget, and click "Save."

## Update the gadget

In the Developer Console, go to **App Details > App Features > Gadget**, and enable the gadget. Select the latest version of the gadget, and click "Save."

![Snipaste_2024-03-20_11-28-42.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/a2f9d16b0421f7d7ce636d68f0b10fdd_amp2PcGiQf.png?height=1654&lazyload=true&width=2922)

## Release the app

1.  On the app details page, click **Version Management & Release** on the left, and click **Create Version**.
2.  On the version creation page, enter the following information:

-   Version number: **1.0.0**  or other correct version number

-   Minimum compatible Feishu version: **Compatible version required**

-   Update instructions: **Your update instructions**

-   Availability status: **You and other users who need to use the app**

Click "**Save**" at the bottom.

3. Click "**Apply for release**" on the upper right corner.

**After the admin approves the application**, the app will appear as **Released" at the Developer Console.

# FAQ

Q: An error about API or component type prompts occurred while developing Feishu gadgets using Taro and TypeScript. What can I do?

A: Manually import type prompts related to Feishu gadgets with the plugin. That is, add the following line to the head of the global.d.ts file in the project:

```
/// <reference path="./node_modules/@tarojs/plugin-platform-lark/types/shims-lark.d.ts" />
```

Q: An error occured while compiling projects with Taro. What can I do?

A: Submit your issue on Taro's official [problem feedback channel](https://github.com/NervJS/taro/issues).

Q: What can I do if the new Feishu APIs and components are not supported?

A: Contact us on the Feedback section of Feishu Developer Tools. It can also be solved by Taro's official [plugin](https://github.com/NervJS/taro-plugin-inject).
