zoubingwu / msw-auto-mock

A cli tool to generate random mock data from OpenAPI definition for msw.
273 stars 57 forks source link

browser only support #13

Closed teone closed 1 year ago

teone commented 2 years ago

Hi @zoubingwu, first of all thanks for this, it's a great tool and saves a lot of time!

There is one bit of the implementation that could simplify (in my opinion) the usage for a few people.

When using Webpack in a client side app (eg a React app generated with https://create-react-app.dev/), there are issues in importing modules supposed to run in a node environment.

Because of that, this bit causes a bit of issues:

export const startWorker = () => {
  if (typeof window === "undefined") {
    const { setupServer } = require("msw/node");
    const server = setupServer(...handlers);
    server.listen();
  } else {
    const worker = setupWorker(...handlers);
    worker.start();
  }
};

do you think it would make sense to add a flag like --browser to change that piece of code to smaller when set? For example:

export const startWorker = () => {
    const worker = setupWorker(...handlers);
    worker.start();
};
zoubingwu commented 2 years ago

sure, I'm also having the same issue, will fix asap