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

Browser plugin preview #2748

Open
wants to merge 22 commits into
base: release-139
Choose a base branch
from
Open

Conversation

macintoshhelper
Copy link

@macintoshhelper macintoshhelper commented May 13, 2024

Why does this PR exist?

Closes #2709

There is now an optional web preview mode for development, which starts an Express.js server locally with a WebSockets server. The plugin UI forwards messages between the controller and the browser version.

What does this pull request do?

  • Browser dev preview mode – npm run preview:browser + npm run preview:plugin
  • React Fast Refresh in web (HMR)
  • npm run build + npm run serve working
  • Small startup refactor to allow the plugin to run outside of the Figma sandbox/UI (entrypoint render). STARTUP handler is moved to useEffect in the startup component.

yarn build + yarn serve

image

yarn preview:browser

image

yarn preview:plugin

image

Testing this change

Start WebSockets server and webpack-dev-server for web preview and HMR

npm run preview:browser

Start Figma plugin dev

npm run preview:plugin

Additional Notes (if any)

May need to reload the plugin in Figma if STARTUP runs too early. Am thinking to implement a sort of dev SDK preview window in the browser, with the ability to simulate STARTUP with custom data, would need something like this to work in a Next.js preview and handle the case of having no WebSocket server or Figma plugin environment running

The preview:browser script is running an Express server with a WebSockets server, so npm run serve isn't working yet.

Plugin UI is disabled when PREVIEW_MODE/browser mode is active.

Currently Fast Refresh requires stopping the plugin preview in the command line, to prevent STARTUP from restarting the browser plugin UI.


Unrelated change, but I added dev npm packages for BundleAnalyzerPlugin and SpeedMeasurePlugin to make debugging build time/bundle size easier when needed (pass Webpack arg). These affect build times so are disabled by default.

Copy link

changeset-bot bot commented May 13, 2024

⚠️ No Changeset found

Latest commit: 178b5b8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@macintoshhelper macintoshhelper marked this pull request as ready for review May 16, 2024 12:57
Copy link
Collaborator

@six7 six7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice overall! let's hold off merging this until 2.0 is out, too much risk (and its the basis for devex anyway)

@@ -26,6 +26,50 @@ export type AsyncMessageChannelHandlers = {
>
};

const WEBSOCKET_SERVER_URL = 'ws://localhost:9001/ws';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move this to a dedicated file? as in just import those functions here - to keep things cleaner?

packages/tokens-studio-for-figma/src/app/index.tsx Outdated Show resolved Hide resolved

```
+------------------------------------+
| Figma Plugin Controller |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want to turn this into a mermaid diagram?

const possiblePromise = callback(event.data.pluginMessage);
if (possiblePromise === false || (possiblePromise && await possiblePromise === false)) {
window.removeEventListener('message', listener);
private startWebSocketConnection() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lots of these functions feel like we should move them to either a shared websocket file or their own files alltogether in order to improve maintainabiltiy

const possiblePromise = callback(msg);
if (possiblePromise === false || (possiblePromise && await possiblePromise === false)) {
figma.ui.off('message', listener);
switch (this.environment) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any way where we can make this function overall more readable? lots of nested ifs and switch statements 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants