create a figma plugin in typescript

To create a Figma plugin using Typescript, the following steps may be followed:

  1. Install Node.js and NPM on your system, which are required for creating and managing the project dependencies.

  2. Create a new npm module by typing the following command in your terminal:

npm init
9 chars
2 lines
  1. Install the figma npm module, which provides an integration with the Figma API:
npm install --save figma
25 chars
2 lines
  1. Install the typescript npm module, which enables you to write TypeScript code:
npm install --save-dev typescript
34 chars
2 lines
  1. Create a tsconfig.json file with the following contents:
{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "moduleResolution": "node",
        "strict": true,
        "esModuleInterop": true
    },
    "exclude": [
        "node_modules"
    ]
}
229 chars
13 lines
  1. Create a src folder and a main.ts file within it. This is where you will write the code for your plugin:
index.ts
import * as Figma from 'figma';

console.log('Hello, Figma!');

// Your plugin code goes here
94 chars
6 lines
  1. Add a build script to your package.json file, which will compile the TypeScript code to JavaScript:
{
    "scripts": {
        "build": "tsc"
    }
}
50 chars
6 lines
  1. Run the build script to ensure that your TypeScript code compiles correctly:
npm run build
14 chars
2 lines
  1. Open Figma, and navigate to the Plugins menu. Click "Create a plugin" and follow the prompts to set up your new plugin.

  2. Once your plugin is set up, you can click "Open console" to open the JavaScript console. In this console, you can type require('./main.js') to run the compiled version of your TypeScript code.

Congratulations, you have now created a Figma plugin using Typescript!

gistlibby LogSnag