Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin with menu and codegen doesn't work #202

Closed
psharanda opened this issue Oct 17, 2023 · 6 comments · Fixed by #201
Closed

Plugin with menu and codegen doesn't work #202

psharanda opened this issue Oct 17, 2023 · 6 comments · Fixed by #201
Labels
bug Something isn't working

Comments

@psharanda
Copy link
Contributor

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
Copy link
Contributor Author

I've created a PR with a potential fix

@yuanqing
Copy link
Owner

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

@psharanda
Copy link
Contributor Author

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

@yuanqing
Copy link
Owner

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

@psharanda
Copy link
Contributor Author

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 #203

@yuanqing
Copy link
Owner

Thanks! Can confirm that this is a bug

@yuanqing yuanqing added the bug Something isn't working label Oct 19, 2023
@yuanqing yuanqing mentioned this issue Oct 19, 2023
@yuanqing yuanqing linked a pull request Oct 19, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants