yuanqing / create-figma-plugin

:battery: The comprehensive toolkit for developing plugins and widgets for Figma and FigJam
https://yuanqing.github.io/create-figma-plugin/
MIT License
948 stars 91 forks source link

Plugin with menu and codegen doesn't work #202

Closed psharanda closed 1 year ago

psharanda commented 1 year ago

Hi @yuanqing, First of all thanks a lot for creating such a useful toolkit!

Here is my issue. I have an existing plugin which has a menu with a number of features that can be launched from it. Now, I'd like to add a codegen feature for my plugin. I've added all the necessary fields to the package.json

{
  "dependencies": { },
  "devDependencies": { },
  "scripts": {
    "build": "build-figma-plugin --typecheck --minify",
    "watch": "build-figma-plugin --typecheck --watch"
  },
  "figma-plugin": {
    "editorType": [
      "figma",
      "dev"
    ],
    "id": "...",
    "name": "...",
    "capabilities": [
      "codegen"
    ],
    "codegenLanguages": [
      {
        "label": "Typescript",
        "value": "typescript"
      }
    ],
    "main": "src/main.ts",
    "menu": [
      {
        "name": "Feature 1",
        "main": "src/feature1/main.ts",
        "ui": "src/feature1/ui.tsx"
      },
      {
        "name": "Feature 2",
        "main": "src/feature1/main.ts",
        "ui": "src/feature2/ui.tsx"
      },
      {
        "name": "Feature 3",
        "main": "src/feature1/main.ts",
        "ui": "src/feature3/ui.tsx"
      }
    ]
  }
}

I can see my plugin in a Dev Mode in codegen section, but whenever I select something it fails with exception.

figma_app.min.js.br:5 TypeError: modules[commandId] is not a function
    at Proxy.eval (/file/ReoMftOwDV6u2NEV2nHBQQ/PLUGIN_2_SOURCE:5547:19)
    at Object.eval (eval at createScopedEvaluatorFactory (aaded2e9-ae4b-4139-94d3-bc13678f3004:1:6906), <anonymous>:8:16)
    at aaded2e9-ae4b-4139-94d3-bc13678f3004:9:1235
    at aaded2e9-ae4b-4139-94d3-bc13678f3004:9:1908
    at realmEvaluate (aaded2e9-ae4b-4139-94d3-bc13678f3004:9:10651)
    at eval (eval at createCallAndWrapError (aaded2e9-ae4b-4139-94d3-bc13678f3004:1:1794), <anonymous>:1:615)
    at Realm.evaluate (aaded2e9-ae4b-4139-94d3-bc13678f3004:1:1291)
    at MZo.evalCode (figma_app.min.js.br:654:5024)
    at MZo.evalTopLevelCode (figma_app.min.js.br:654:5269)
    at figma_app.min.js.br:1810:178
(anonymous) @ figma_app.min.js.br:5

commandId in that case is a string 'generate'

As a workaround I was able to patch the generated build/main.js by taking in account 'generate' command which comes from figma.command:

  var modules = {
    'src/main.ts--default': (init_main(), __toCommonJS(main_exports))['default'],
    'src/feature1/main.ts--default': (init_main2(), __toCommonJS(main_exports2))['default'],
    'src/feature2/main.ts--default': (init_main3(), __toCommonJS(main_exports3))['default'],
    'src/feature3/main.ts--default': (init_main4(), __toCommonJS(main_exports4))['default']
  };

  // THIS IS THE LINE I PATCHED BY ADDING ' || figma.command === "generate"'
  var commandId = typeof figma.command === "undefined" || figma.command === "" || figma.command === "generate" ? "src/main.ts--default" : figma.command;

  modules[commandId]();

I am wondering if this can be fixed on official level.

Thanks!

psharanda commented 1 year ago

I've created a PR with a potential fix

yuanqing commented 1 year ago

Can you share a GitHub repo with a minimal reproduction of the issue?

psharanda commented 1 year ago

Here is the minimal plugin which reproduces the issue figma_plugin_repro.zip

yuanqing commented 1 year ago

Possible to put the files in that zip as either a repo or a GitHub gist? Thanks!

psharanda commented 1 year ago

Ok, here is the repro repo https://github.com/psharanda/create-figma-plugin-codegen-issue And just in case, here is the PR which fixes the issue https://github.com/yuanqing/create-figma-plugin/pull/203

yuanqing commented 1 year ago

Thanks! Can confirm that this is a bug