Skip to content

Invisible 'server' panel doesn't play well with the HMR feature #87

@avibiton13

Description

@avibiton13

I've added another panel aside the main one, called 'server' which intended to expose my main panel to outside apps.
My folder structure is as follows:

├── js
...
│   ├── lib
...
│   ├── main
...
│   ├── server
│   │   ├── index.html
│   │   └── index.ts
...

src/serve/index.ts code:

import { http } from "../lib/cep/node";

const port = 8088;
const server = http.createServer((req, res) => {
    console.log(req);
    if (req.url === '/ping') {
        res.setHeader('Content-Type', 'text/plain');
        res.end('pong');
    } else {
        res.setHeader('Content-Type', 'text/plain');
        res.statusCode = 400;
        res.end('METHOD_NOT_ALLOWED');
    }
});

server.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
    console.log(server.listening);
});

The problem:
Each time I save any file HMR is triggered for all the files under src(and I do get that it's expected behavior) causing the listen function to re-run and raise the EADDRINUSE error.

My workaround:
Adding the following line to the triggerHMR function that's inside vite.es.config.ts:

if (panel.name === "server") return;

triggerHMR looks like that right now:

  const triggerHMR = () => {
    // No built-in way to trigger Vite's HMR reload from outside the root folder
    // Workaround will read and save index.html file for each panel to triggger reload
    console.log("ExtendScript Change");
    cepConfig.panels.map((panel) => {
      if (panel.name === "server") return; // this is my addition
      const tmpPath = path.join(process.cwd(), "src", "js", panel.mainPath);
      if (fs.existsSync(tmpPath)) {
        const txt = fs.readFileSync(tmpPath, { encoding: "utf-8" });
        fs.writeFileSync(tmpPath, txt, { encoding: "utf-8" });
      }
    });
  };

So ultimately I'm losing the HMR feature for that panel, and each time I want it's code changes to take place I have to rebuild and restart After Effects..
Any ideas how to properly overcome that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions