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

refactor(server): provide createDevServer method instead of getServerAPIs #1818

Merged
merged 20 commits into from
Mar 19, 2024

Conversation

9aoy
Copy link
Contributor

@9aoy 9aoy commented Mar 13, 2024

Summary

Simplify the way users use custom servers. The original getServerAPIs is too complicated for users to use custom server (requires calling 6 API methods).

before:

+  const serverAPIs = await rsbuild.getServerAPIs();

  const {
    config: { host, port },
  } = serverAPIs;

+  const compileMiddlewareAPI = await serverAPIs.startCompile();

+  const { middlewares, close, onUpgrade } = await serverAPIs.getMiddlewares({
    compileMiddlewareAPI,
  });

+  middlewares.forEach((item) => {
    if (Array.isArray(item)) {
      app.use(...item);
    } else {
      app.use(item);
    }
  });

+  await serverAPIs.beforeStart();

  const { server } = app.listen({ host, port }, async () => {
+    await serverAPIs.afterStart();
  });

  // subscribe the server's http upgrade event to handle WebSocket upgrade
+  server.on('upgrade', serverAPIs.onUpgrade);

  return {
    close: async () => {
+     await serverAPIs.close();
      server.close();
    },
  };

after:

+  const rsbuildServer = await rsbuild.createDevServer();

+  app.use(rsbuildServer.middlewares);

  const {
    config: { port },
  } = rsbuildServer;

  const { server } = app.listen({ port }, async () => {
+   await rsbuildServer.afterListen();
  });

  // subscribe the server's http upgrade event to handle WebSocket upgrade
+  server.on('upgrade', rsbuildServer.onHTTPUpgrade);

  return {
    close: async () => {
+     await rsbuildServer.close();
      server.close();
    },
  };
}

Related Links

Checklist

  • Tests updated.
  • Documentation updated.

@9aoy
Copy link
Contributor Author

9aoy commented Mar 13, 2024

!canary

@9aoy 9aoy changed the title WIP: refactor: dev server api refactor(server): provide createDevServer method instead of getServerAPIs Mar 18, 2024
@9aoy
Copy link
Contributor Author

9aoy commented Mar 18, 2024

!canary

@chenjiahan chenjiahan merged commit 6963e79 into web-infra-dev:main Mar 19, 2024
6 checks passed
@chenjiahan chenjiahan mentioned this pull request Mar 19, 2024
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.

2 participants