Derived Modular Arch
指南

Vite + React

Vite + React 上可运行的规范 DMA 示例。

这是参考 DMA 示例:可运行的 mini-shop、完整导入图、就近放置的 store、cart 提升(promotion)到 services/。其他框架指南是同一故事的不同变体。

源码:examples/vite-react

组合根

DMA 将 src/app/ 视为组合根(composition root)——与其他栈中的 pages/routes/ 角色相同。

src/app/
├── app.tsx         # nav, badges, feature mounts, prop wiring
├── providers.tsx   # ThemeProvider from shared/ui
└── app.css         # global styles only here

app.tsx 导入 */public/*

import { CatalogPage } from "@/features/catalog/public/catalog-page";
import { addNotification } from "@/features/notifications/public/notifications-api";

<CatalogPage
  onAddedToCart={(name) => addNotification(`Added ${name}`)}
/>

Catalog → notifications 通过 props 胶水——见跨模块接线

Mini-shop:谁拥有什么

模块状态所有者导入
Catalogcatalog.store.ts(filters)services/cartshared/*
Checkoutcheckout.store.ts(delivery)services/cartshared/*
Notificationsnotifications.store.tsshared/*
Cartservices/cart/public/cart.tsshared/model
Profilestage-0 profile.tsxshared/ui

catalog → notifications 边——只有 app → 两个模块。

提升(promotion):checkout → cart

购物车并非一开始就在 services/。当 catalog 也需要 addCartItem 时,cart 被提升:

Before (1 consumer)              After (2 consumers)
features/checkout/              features/catalog/public/ ──┐
  checkout.store (cart)         features/checkout/public/ ─┼──► services/cart/public/cart.ts
                                checkout.store (delivery only)

dma check 看到入边(inbound edge)并确认 cart 属于 services/,而非 feature 内部。

代码放哪里

任务文件夹示例
从 app 挂载的屏幕features/<name>/public/ 或 stage-0 features/<name>.tsxcatalog-page.tsx
单 feature 状态就近放置的 *.store.tscheckout.store.ts
2+ 模块的场景services/<name>/public/cart.ts
无产品逻辑的 UI 原语shared/ui/button.tsx
纯 helpershared/lib/format-currency.ts
2+ 模块的类型shared/model/product.ts
Providersapp/providers.tsxThemeProvider

数据获取

文件角色
传输shared/api/http.ts精简的 get<T>()
Feature APIfeatures/catalog/catalog.api.ts仅 catalog 导入
演示数据public/catalog.jsonVite 静态资源

其他 feature 不导入 catalog.api.ts。共享 HTTP 经 shared/api/http.ts

样式

方式位置示例
全局 CSSapp/app.cssreset、外壳
CSS Modules紧邻 public/ 入口catalog-page.module.css
纯 CSSfeature 内就近放置checkout-page.css
SCSS modulestage-0 featureprofile.module.scss
CSS-in-JSfeature 内部notifications.styles.ts

全局样式——app/。Feature 专属皮肤不属于 shared/

测试

与代码就近放置的 *.test.ts(Vitest):

  • catalog.store.test.ts——内部 store
  • services/cart/public/cart.test.ts——service 公共 API
  • shared/lib/format-currency.test.ts——可移植 helper

E2E(示例中未包含)——在 app 根(e2e/)。

命令

bun run dev
bun run build
bun run dma-check   # dma check .
bun run lint        # ESLint + @derived-modular/eslint-plugin
bun run test

下一步

On this page