storybook 使用

用了这个知名 storybook (opens in a new tab),a frontend workshop for building UI components and pages in isolation,目的是为了进行组件库的开发、测试、文档站

Intro

踩坑

开启 dev server 的 proxy

希望在 dev 的时候把接口请求 map 到指定的 api 域名上

搜了很多,也没个啥结果,插件体系说实话感觉也不太好用?最终,在这个 pr (opens in a new tab) 上找到在 .storybook/middleware.jsintentionally undocumented feature (opens in a new tab), but given that it has been stable for at least 5 years (opens in a new tab),)里面写中间件

离谱的是这个 middleware.js 居然没有官方的文档说明,但是也跑了好几年

这个 issue (opens in a new tab) 给出了用 http-proxy-middleware 来作为 app(express 应用?)的中间件

const { createProxyMiddleware } = require("http-proxy-middleware");
 
module.exports = function (app) {
  app.use(
    "/api",
    createProxyMiddleware({
      target: "https://xxx.com",
      changeOrigin: true,
    })
  );
};