畫面
目錄結構
- workspace
- apps/
- fe-news
- libs/
- fe-common-ui
- fe-common-utils
Monorepo
- 建立 workspace: create-nx-workspace
npx create-nx-workspace@latest
--preset=next
--packageManager=pnpm
- 目錄結構
- workspace
- apps
- libs
- tools
-
新增專案到 apps
- @nx/next:app (使用 nextjs 模板)
pnpm nx g @nx/next:app fe-news
-
新增專案到 libs (使用@nx/react:lib 模板建立 fe-common-ui)
pnpm nx g @nx/react:lib fe-common-ui
-
新增元件模板到 fe-news 專案下
pnpm nx g @nx/react:component header --project=fe-news
-
會使用@nx/react:component 模板生成程式碼 (不太放心他會加什麼可以加--dry-run,不會真的加,但會告訴你這個指令會新增什麼檔案)
> NX Generating @nx/react:component ✔ Which stylesheet format would you like to use? · none ✔ Should this component be exported in the project? (y/N) · false CREATE apps/fe-news/app/header/header.spec.tsx CREATE apps/fe-news/app/header/header.tsx NOTE: The "dryRun" flag means no changes were made.
-
-
新增元件到 fe-common-ui 下
pnpm nx g @nx/react:component section --project=fe-common-ui
-
刪除專案
pnpm nx g rm fe-news-e2e pnpm nx g rm fe-news
-
重新命名專案
pnpm nx g mv --project <old-name> <new-name>
-
執行專案
pnpm nx serve fe-news # 預設跑在port 4200,可到專案下的project.json修改 # project.json會紀錄每個專案可用的指令,類似package.json
-
執行多個專案
pnpm nx run-many -t start -p fe-news fe-common-ui # -t (target) # -p (project)
CSS Library(Linaria)
-
callstack/linaria: Zero-runtime CSS in JS library
pnpm install next-with-linaria @linaria/babel-preset @linaria/core @linaria/react
-
next-with-linaria next13 需要額外裝 next-with-linaria,但這個專案沒有更新了,有點疑慮。
// next.config.ts const withLinaria = require('next-with-linaria'); const plugins = [withNx, withLinaria];
-
linaria 基本語法
import { styled } from 'linaria/react'; import { css } from 'linaria'; const Title = styled.h1` font-size: 36px; `;
<span className={css` color: red; `} > Hello there </span>
開發
-
可執行的指令定義在 project.json
-
dev
- 新聞列表 API CORS 問題要加(--hostname=0.0.0.0) →
dev.beta.cnyes.cool
pnpm nx serve fe-news --hostname=0.0.0.0
- 新聞列表 API CORS 問題要加(--hostname=0.0.0.0) →
Next13
Lint
-
使用 airbnb lint
-
eslint-config-airbnb-typescript - npm
- 修改
.eslintrc.json
- 修改
-
eslint-config-airbnb-typescript - npm
Infinite-loader
fe-common-ui
-
安裝 storybook
pnpm nx g @nx/storybook:configuration fe-common-ui --uiFramework=@storybook/react-vite --tsConfiguration=true
-
在 fe-common-ui 下新增樣板
pnpm nx g @nx/react:component button --project=fe-common-ui
-
幫所有 components 新增 stories
pnpm nx g @nx/react:stories --project=fe-common-ui
fe-common-utils
- libs
- fe-common-utils
- time
- html
其他
-
打開全部專案關係圖
pnpm nx graph
Reference
- Package Manager
- Monorepo
- Package-Based Monorepo | Nx
- Pnpm and Nx monorepo. Part 1 | Javier Brea
-
Create a Shared React Library in an Nx Workspace | egghead.io
- 新增一個 ui library
-
tsconfig.base.json
- "experimentalDecorators": true
- Linaria