@coze-arch/cli 0.0.1-beta.6 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/lib/__templates__/expo/.coze +7 -2
- package/lib/__templates__/expo/.cozeproj/scripts/dev_build.sh +46 -0
- package/lib/__templates__/expo/.cozeproj/scripts/dev_run.sh +229 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_build.sh +47 -0
- package/lib/__templates__/expo/.cozeproj/scripts/prod_run.sh +34 -0
- package/lib/__templates__/expo/.cozeproj/scripts/server_dev_run.sh +46 -0
- package/lib/__templates__/expo/README.md +68 -7
- package/lib/__templates__/expo/_gitignore +1 -1
- package/lib/__templates__/expo/_npmrc +4 -5
- package/lib/__templates__/expo/client/app/+not-found.tsx +15 -64
- package/lib/__templates__/expo/client/app/_layout.tsx +15 -12
- package/lib/__templates__/expo/client/app/index.tsx +1 -0
- package/lib/__templates__/expo/client/app.config.ts +76 -0
- package/lib/__templates__/expo/client/components/Screen.tsx +3 -19
- package/lib/__templates__/expo/client/components/ThemedText.tsx +33 -0
- package/lib/__templates__/expo/client/components/ThemedView.tsx +37 -0
- package/lib/__templates__/expo/client/constants/theme.ts +117 -58
- package/lib/__templates__/expo/client/contexts/AuthContext.tsx +14 -107
- package/lib/__templates__/expo/client/declarations.d.ts +5 -0
- package/lib/__templates__/expo/{eslint.config.mjs → client/eslint.config.mjs} +40 -10
- package/lib/__templates__/expo/client/hooks/useColorScheme.tsx +48 -0
- package/lib/__templates__/expo/client/hooks/useSafeRouter.ts +152 -0
- package/lib/__templates__/expo/client/hooks/useTheme.ts +26 -6
- package/lib/__templates__/expo/client/metro.config.js +124 -0
- package/lib/__templates__/expo/client/package.json +95 -0
- package/lib/__templates__/expo/client/screens/demo/index.tsx +25 -0
- package/lib/__templates__/expo/client/screens/demo/styles.ts +28 -0
- package/lib/__templates__/expo/client/scripts/install-missing-deps.js +11 -10
- package/lib/__templates__/expo/client/tsconfig.json +24 -0
- package/lib/__templates__/expo/client/utils/index.ts +23 -2
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/names.js +1889 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/rule.js +174 -0
- package/lib/__templates__/expo/eslint-plugins/fontawesome6/v5-only-names.js +388 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/rule.js +112 -0
- package/lib/__templates__/expo/eslint-plugins/forbid-emoji/tech.md +94 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/react-native/rule.js +64 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/reanimated/rule.js +88 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/index.js +9 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/rule.js +120 -0
- package/lib/__templates__/expo/eslint-plugins/restrict-linear-gradient/tech.md +58 -0
- package/lib/__templates__/expo/package.json +16 -101
- package/lib/__templates__/expo/patches/expo@54.0.33.patch +45 -0
- package/lib/__templates__/expo/pnpm-lock.yaml +1622 -3274
- package/lib/__templates__/expo/pnpm-workspace.yaml +3 -0
- package/lib/__templates__/expo/server/build.js +21 -0
- package/lib/__templates__/expo/server/package.json +34 -0
- package/lib/__templates__/expo/server/src/index.ts +20 -0
- package/lib/__templates__/expo/server/tsconfig.json +24 -0
- package/lib/__templates__/expo/template.config.js +58 -1
- package/lib/__templates__/expo/tsconfig.json +1 -24
- package/lib/__templates__/native-static/.coze +11 -0
- package/lib/__templates__/native-static/index.html +33 -0
- package/lib/__templates__/native-static/styles/main.css +136 -0
- package/lib/__templates__/native-static/template.config.js +22 -0
- package/lib/__templates__/nextjs/.coze +4 -3
- package/lib/__templates__/nextjs/README.md +5 -0
- package/lib/__templates__/nextjs/_npmrc +2 -1
- package/lib/__templates__/nextjs/eslint.config.mjs +5 -0
- package/lib/__templates__/nextjs/next.config.ts +11 -0
- package/lib/__templates__/nextjs/package.json +15 -1
- package/lib/__templates__/nextjs/pnpm-lock.yaml +7694 -4394
- package/lib/__templates__/nextjs/scripts/build.sh +4 -1
- package/lib/__templates__/nextjs/scripts/dev.sh +15 -28
- package/lib/__templates__/nextjs/scripts/prepare.sh +9 -0
- package/lib/__templates__/nextjs/scripts/start.sh +7 -1
- package/lib/__templates__/nextjs/src/app/globals.css +109 -89
- package/lib/__templates__/nextjs/src/app/layout.tsx +20 -33
- package/lib/__templates__/nextjs/src/app/page.tsx +18 -49
- package/lib/__templates__/nextjs/src/components/ui/resizable.tsx +29 -22
- package/lib/__templates__/nextjs/src/components/ui/sidebar.tsx +228 -230
- package/lib/__templates__/nextjs/src/server.ts +35 -0
- package/lib/__templates__/nextjs/template.config.js +68 -3
- package/lib/__templates__/nextjs/tsconfig.json +1 -1
- package/lib/__templates__/nuxt-vue/.coze +12 -0
- package/lib/__templates__/nuxt-vue/README.md +73 -0
- package/lib/__templates__/nuxt-vue/_gitignore +24 -0
- package/lib/__templates__/nuxt-vue/_npmrc +23 -0
- package/lib/__templates__/nuxt-vue/app/app.vue +6 -0
- package/lib/__templates__/nuxt-vue/app/pages/index.vue +23 -0
- package/lib/__templates__/nuxt-vue/assets/css/main.css +24 -0
- package/lib/__templates__/nuxt-vue/nuxt.config.ts +116 -0
- package/lib/__templates__/nuxt-vue/package.json +35 -0
- package/lib/__templates__/nuxt-vue/pnpm-lock.yaml +8759 -0
- package/lib/__templates__/nuxt-vue/postcss.config.mjs +8 -0
- package/lib/__templates__/nuxt-vue/public/favicon.ico +0 -0
- package/lib/__templates__/nuxt-vue/public/robots.txt +2 -0
- package/lib/__templates__/nuxt-vue/scripts/build.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/dev.sh +39 -0
- package/lib/__templates__/nuxt-vue/scripts/prepare.sh +14 -0
- package/lib/__templates__/nuxt-vue/scripts/start.sh +21 -0
- package/lib/__templates__/nuxt-vue/server/api/hello.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/middleware/logger.ts +10 -0
- package/lib/__templates__/nuxt-vue/server/routes/health.ts +10 -0
- package/lib/__templates__/nuxt-vue/tailwind.config.js +13 -0
- package/lib/__templates__/nuxt-vue/template.config.js +87 -0
- package/lib/__templates__/nuxt-vue/tsconfig.json +18 -0
- package/lib/__templates__/taro/.coze +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_build.sh +19 -0
- package/lib/__templates__/taro/.cozeproj/scripts/deploy_run.sh +14 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_build.sh +2 -0
- package/lib/__templates__/taro/.cozeproj/scripts/dev_run.sh +151 -0
- package/lib/__templates__/taro/.cozeproj/scripts/init_env.sh +5 -0
- package/lib/__templates__/taro/.cozeproj/scripts/pack.sh +24 -0
- package/lib/__templates__/taro/README.md +763 -0
- package/lib/__templates__/taro/_gitignore +40 -0
- package/lib/__templates__/taro/_npmrc +18 -0
- package/lib/__templates__/taro/babel.config.js +12 -0
- package/lib/__templates__/taro/config/dev.ts +9 -0
- package/lib/__templates__/taro/config/index.ts +238 -0
- package/lib/__templates__/taro/config/prod.ts +34 -0
- package/lib/__templates__/taro/eslint.config.mjs +135 -0
- package/lib/__templates__/taro/key/private.appid.key +0 -0
- package/lib/__templates__/taro/package.json +112 -0
- package/lib/__templates__/taro/patches/@tarojs__plugin-mini-ci@4.1.9.patch +30 -0
- package/lib/__templates__/taro/pnpm-lock.yaml +23412 -0
- package/lib/__templates__/taro/pnpm-workspace.yaml +2 -0
- package/lib/__templates__/taro/project.config.json +15 -0
- package/lib/__templates__/taro/server/nest-cli.json +10 -0
- package/lib/__templates__/taro/server/package.json +40 -0
- package/lib/__templates__/taro/server/src/app.controller.ts +23 -0
- package/lib/__templates__/taro/server/src/app.module.ts +10 -0
- package/lib/__templates__/taro/server/src/app.service.ts +8 -0
- package/lib/__templates__/taro/server/src/interceptors/http-status.interceptor.ts +23 -0
- package/lib/__templates__/taro/server/src/main.ts +49 -0
- package/lib/__templates__/taro/server/tsconfig.json +24 -0
- package/lib/__templates__/taro/src/app.config.ts +11 -0
- package/lib/__templates__/taro/src/app.css +156 -0
- package/lib/__templates__/taro/src/app.tsx +9 -0
- package/lib/__templates__/taro/src/components/ui/accordion.tsx +159 -0
- package/lib/__templates__/taro/src/components/ui/alert-dialog.tsx +260 -0
- package/lib/__templates__/taro/src/components/ui/alert.tsx +60 -0
- package/lib/__templates__/taro/src/components/ui/aspect-ratio.tsx +36 -0
- package/lib/__templates__/taro/src/components/ui/avatar.tsx +84 -0
- package/lib/__templates__/taro/src/components/ui/badge.tsx +37 -0
- package/lib/__templates__/taro/src/components/ui/breadcrumb.tsx +117 -0
- package/lib/__templates__/taro/src/components/ui/button-group.tsx +83 -0
- package/lib/__templates__/taro/src/components/ui/button.tsx +67 -0
- package/lib/__templates__/taro/src/components/ui/calendar.tsx +394 -0
- package/lib/__templates__/taro/src/components/ui/card.tsx +108 -0
- package/lib/__templates__/taro/src/components/ui/carousel.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/checkbox.tsx +58 -0
- package/lib/__templates__/taro/src/components/ui/code-block.tsx +169 -0
- package/lib/__templates__/taro/src/components/ui/collapsible.tsx +71 -0
- package/lib/__templates__/taro/src/components/ui/command.tsx +385 -0
- package/lib/__templates__/taro/src/components/ui/context-menu.tsx +614 -0
- package/lib/__templates__/taro/src/components/ui/dialog.tsx +256 -0
- package/lib/__templates__/taro/src/components/ui/drawer.tsx +192 -0
- package/lib/__templates__/taro/src/components/ui/dropdown-menu.tsx +561 -0
- package/lib/__templates__/taro/src/components/ui/field.tsx +228 -0
- package/lib/__templates__/taro/src/components/ui/hover-card.tsx +282 -0
- package/lib/__templates__/taro/src/components/ui/input-group.tsx +197 -0
- package/lib/__templates__/taro/src/components/ui/input-otp.tsx +136 -0
- package/lib/__templates__/taro/src/components/ui/input.tsx +56 -0
- package/lib/__templates__/taro/src/components/ui/label.tsx +24 -0
- package/lib/__templates__/taro/src/components/ui/menubar.tsx +595 -0
- package/lib/__templates__/taro/src/components/ui/navigation-menu.tsx +264 -0
- package/lib/__templates__/taro/src/components/ui/pagination.tsx +118 -0
- package/lib/__templates__/taro/src/components/ui/popover.tsx +291 -0
- package/lib/__templates__/taro/src/components/ui/portal.tsx +19 -0
- package/lib/__templates__/taro/src/components/ui/progress.tsx +28 -0
- package/lib/__templates__/taro/src/components/ui/radio-group.tsx +64 -0
- package/lib/__templates__/taro/src/components/ui/resizable.tsx +346 -0
- package/lib/__templates__/taro/src/components/ui/scroll-area.tsx +34 -0
- package/lib/__templates__/taro/src/components/ui/select.tsx +438 -0
- package/lib/__templates__/taro/src/components/ui/separator.tsx +30 -0
- package/lib/__templates__/taro/src/components/ui/sheet.tsx +262 -0
- package/lib/__templates__/taro/src/components/ui/skeleton.tsx +17 -0
- package/lib/__templates__/taro/src/components/ui/slider.tsx +203 -0
- package/lib/__templates__/taro/src/components/ui/sonner.tsx +1 -0
- package/lib/__templates__/taro/src/components/ui/switch.tsx +55 -0
- package/lib/__templates__/taro/src/components/ui/table.tsx +142 -0
- package/lib/__templates__/taro/src/components/ui/tabs.tsx +114 -0
- package/lib/__templates__/taro/src/components/ui/textarea.tsx +54 -0
- package/lib/__templates__/taro/src/components/ui/toast.tsx +517 -0
- package/lib/__templates__/taro/src/components/ui/toggle-group.tsx +120 -0
- package/lib/__templates__/taro/src/components/ui/toggle.tsx +77 -0
- package/lib/__templates__/taro/src/components/ui/tooltip.tsx +455 -0
- package/lib/__templates__/taro/src/index.html +39 -0
- package/lib/__templates__/taro/src/lib/hooks/use-keyboard-offset.ts +37 -0
- package/lib/__templates__/taro/src/lib/measure.ts +115 -0
- package/lib/__templates__/taro/src/lib/platform.ts +12 -0
- package/lib/__templates__/taro/src/lib/utils.ts +6 -0
- package/lib/__templates__/taro/src/network.ts +39 -0
- package/lib/__templates__/taro/src/pages/index/index.config.ts +3 -0
- package/lib/__templates__/taro/src/pages/index/index.css +1 -0
- package/lib/__templates__/taro/src/pages/index/index.tsx +33 -0
- package/lib/__templates__/taro/src/presets/dev-debug.ts +23 -0
- package/lib/__templates__/taro/src/presets/h5-container.tsx +15 -0
- package/lib/__templates__/taro/src/presets/h5-navbar.tsx +238 -0
- package/lib/__templates__/taro/src/presets/h5-styles.ts +220 -0
- package/lib/__templates__/taro/src/presets/index.tsx +18 -0
- package/lib/__templates__/taro/stylelint.config.mjs +4 -0
- package/lib/__templates__/taro/template.config.js +68 -0
- package/lib/__templates__/taro/tsconfig.json +29 -0
- package/lib/__templates__/taro/types/global.d.ts +32 -0
- package/lib/__templates__/templates.json +136 -36
- package/lib/__templates__/vite/.coze +4 -3
- package/lib/__templates__/vite/README.md +383 -26
- package/lib/__templates__/vite/_gitignore +1 -0
- package/lib/__templates__/vite/_npmrc +2 -1
- package/lib/__templates__/vite/eslint.config.mjs +14 -0
- package/lib/__templates__/vite/package.json +23 -3
- package/lib/__templates__/vite/pnpm-lock.yaml +2509 -293
- package/lib/__templates__/vite/scripts/build.sh +4 -1
- package/lib/__templates__/vite/scripts/dev.sh +16 -28
- package/lib/__templates__/vite/scripts/prepare.sh +9 -0
- package/lib/__templates__/vite/scripts/start.sh +9 -3
- package/lib/__templates__/vite/server/routes/index.ts +31 -0
- package/lib/__templates__/vite/server/server.ts +65 -0
- package/lib/__templates__/vite/server/vite.ts +67 -0
- package/lib/__templates__/vite/src/main.ts +17 -48
- package/lib/__templates__/vite/template.config.js +77 -7
- package/lib/__templates__/vite/tsconfig.json +4 -3
- package/lib/__templates__/vite/vite.config.ts +8 -3
- package/lib/cli.js +1545 -526
- package/package.json +17 -6
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_build.sh +0 -109
- package/lib/__templates__/expo/.cozeproj/scripts/deploy_run.sh +0 -257
- package/lib/__templates__/expo/app.json +0 -63
- package/lib/__templates__/expo/babel.config.js +0 -9
- package/lib/__templates__/expo/client/app/(tabs)/_layout.tsx +0 -43
- package/lib/__templates__/expo/client/app/(tabs)/home.tsx +0 -1
- package/lib/__templates__/expo/client/app/(tabs)/index.tsx +0 -7
- package/lib/__templates__/expo/client/hooks/useColorScheme.ts +0 -1
- package/lib/__templates__/expo/client/index.js +0 -12
- package/lib/__templates__/expo/client/screens/home/index.tsx +0 -54
- package/lib/__templates__/expo/client/screens/home/styles.ts +0 -332
- package/lib/__templates__/expo/metro.config.js +0 -53
- package/lib/__templates__/expo/src/index.ts +0 -12
- package/lib/__templates__/nextjs/.vscode/settings.json +0 -121
- package/lib/__templates__/nextjs/server.mjs +0 -50
- package/lib/__templates__/vite/.vscode/settings.json +0 -7
- /package/lib/__templates__/expo/{eslint-formatter-simple.mjs → client/eslint-formatter-simple.mjs} +0 -0
|
@@ -1,61 +1,418 @@
|
|
|
1
1
|
# <%= appName %>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
这是一个基于 Express + Vite + TypeScript + Tailwind CSS 的全栈 Web 应用项目,由扣子编程 CLI 创建。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**核心特性:**
|
|
6
|
+
- 🚀 前端:Vite + TypeScript + Tailwind CSS
|
|
7
|
+
- 🔧 后端:Express + TypeScript,提供 RESTful API
|
|
8
|
+
- 🔥 开发模式:Vite HMR + Express API,单进程启动
|
|
9
|
+
- 📦 生产模式:Express 静态服务 + API,高性能部署
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
13
|
+
### 启动开发服务器
|
|
8
14
|
|
|
9
15
|
```bash
|
|
10
|
-
|
|
16
|
+
coze dev
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
启动后,在浏览器中打开 [http://localhost:<%= port %>](http://localhost:<%= port %>) 查看应用。
|
|
20
|
+
|
|
21
|
+
开发服务器支持热更新(HMR),修改代码后页面会自动刷新。
|
|
22
|
+
|
|
23
|
+
### 构建生产版本
|
|
14
24
|
|
|
15
25
|
```bash
|
|
16
|
-
|
|
26
|
+
coze build
|
|
17
27
|
```
|
|
18
28
|
|
|
19
|
-
|
|
29
|
+
构建产物位于 `dist/` 目录,可直接部署到静态托管服务。
|
|
30
|
+
|
|
31
|
+
### 预览生产版本
|
|
20
32
|
|
|
21
33
|
```bash
|
|
22
|
-
coze
|
|
34
|
+
coze start
|
|
23
35
|
```
|
|
24
36
|
|
|
25
|
-
|
|
37
|
+
在本地启动一个静态服务器,预览生产构建的效果。
|
|
26
38
|
|
|
27
|
-
|
|
39
|
+
## 项目结构
|
|
28
40
|
|
|
29
|
-
```
|
|
30
|
-
|
|
41
|
+
```
|
|
42
|
+
├── server/ # 后端服务器目录
|
|
43
|
+
│ ├── index.ts # express 服务器入口
|
|
44
|
+
│ ├── routes/ # API 路由目录
|
|
45
|
+
│ │ └── index.ts # 路由定义
|
|
46
|
+
│ └── vite.ts # Vite 集成逻辑
|
|
47
|
+
├── src/ # 前端源码目录
|
|
48
|
+
│ ├── index.ts # 前端应用入口(初始化)
|
|
49
|
+
│ ├── main.ts # 前端主逻辑文件
|
|
50
|
+
│ └── index.css # 全局样式(包含 Tailwind 指令)
|
|
51
|
+
├── index.html # HTML 入口文件
|
|
52
|
+
├── vite.config.ts # Vite 配置
|
|
53
|
+
├── tailwind.config.ts # Tailwind CSS 配置
|
|
54
|
+
└── tsconfig.json # TypeScript 配置
|
|
31
55
|
```
|
|
32
56
|
|
|
33
|
-
|
|
57
|
+
**目录说明:**
|
|
34
58
|
|
|
35
|
-
|
|
36
|
-
|
|
59
|
+
- **`server/`** - 后端服务器代码
|
|
60
|
+
- `server.ts` - 服务器主入口,负责创建和启动 Express 应用
|
|
61
|
+
- `routes/` - API 路由模块,支持按功能拆分路由
|
|
62
|
+
- `vite.ts` - Vite 开发服务器和静态文件服务集成
|
|
63
|
+
|
|
64
|
+
- **`src/`** - 前端应用代码
|
|
65
|
+
- 所有前端相关代码都在这里
|
|
66
|
+
|
|
67
|
+
**工作原理:**
|
|
68
|
+
|
|
69
|
+
- **开发模式** (`coze dev`):
|
|
70
|
+
- 运行 `server/server.ts` 启动 Express 服务器
|
|
71
|
+
- Vite 以 middleware 模式集成到 Express
|
|
72
|
+
- 前端支持 HMR(热模块替换)
|
|
73
|
+
- 后端 API 和前端在同一进程,端口 <%= port %>
|
|
74
|
+
|
|
75
|
+
- **生产模式** (`coze start`):
|
|
76
|
+
- `coze build` 构建前端 → `dist/` 目录
|
|
77
|
+
- `coze build` 构建后端 → `dist-server/index.js` (CommonJS 格式)
|
|
78
|
+
- 运行 `dist-server/index.js` 启动生产服务器
|
|
79
|
+
- Express 服务静态文件 + API 路由
|
|
80
|
+
- 单一 Node.js 进程,轻量高效
|
|
81
|
+
|
|
82
|
+
## 核心开发规范
|
|
83
|
+
|
|
84
|
+
### 1. 后端 API 开发
|
|
85
|
+
|
|
86
|
+
**添加新的 API 路由**
|
|
87
|
+
|
|
88
|
+
在 `server/routes/index.ts` 中添加路由:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// GET 请求示例
|
|
92
|
+
router.get('/api/users', (req, res) => {
|
|
93
|
+
res.json({
|
|
94
|
+
users: [
|
|
95
|
+
{ id: 1, name: 'Alice' },
|
|
96
|
+
{ id: 2, name: 'Bob' },
|
|
97
|
+
],
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// POST 请求示例
|
|
102
|
+
router.post('/api/users', (req, res) => {
|
|
103
|
+
const userData = req.body;
|
|
104
|
+
// 处理业务逻辑
|
|
105
|
+
res.json({
|
|
106
|
+
success: true,
|
|
107
|
+
user: userData,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 动态路由参数
|
|
112
|
+
router.get('/api/users/:id', (req, res) => {
|
|
113
|
+
const userId = req.params.id;
|
|
114
|
+
res.json({
|
|
115
|
+
id: userId,
|
|
116
|
+
name: 'User ' + userId,
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**拆分路由模块**(推荐)
|
|
122
|
+
|
|
123
|
+
当路由变多时,可以按功能拆分:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// server/routes/users.ts
|
|
127
|
+
import { Router } from 'express';
|
|
128
|
+
|
|
129
|
+
const router = Router();
|
|
130
|
+
|
|
131
|
+
router.get('/api/users', (req, res) => {
|
|
132
|
+
// 用户列表逻辑
|
|
133
|
+
res.json({ users: [] });
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
router.post('/api/users', (req, res) => {
|
|
137
|
+
// 创建用户逻辑
|
|
138
|
+
res.json({ success: true });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
export default router;
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
然后在 `server/server.ts` 中注册:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
import usersRouter from './routes/users';
|
|
148
|
+
|
|
149
|
+
// 注册路由
|
|
150
|
+
app.use(usersRouter);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**前端调用 API**
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
// GET 请求
|
|
157
|
+
async function getUsers() {
|
|
158
|
+
const response = await fetch('/api/users');
|
|
159
|
+
const data = await response.json();
|
|
160
|
+
console.log(data);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// POST 请求
|
|
164
|
+
async function createUser(name: string) {
|
|
165
|
+
const response = await fetch('/api/users', {
|
|
166
|
+
method: 'POST',
|
|
167
|
+
headers: {
|
|
168
|
+
'Content-Type': 'application/json',
|
|
169
|
+
},
|
|
170
|
+
body: JSON.stringify({ name }),
|
|
171
|
+
});
|
|
172
|
+
const data = await response.json();
|
|
173
|
+
console.log(data);
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**API 最佳实践**
|
|
178
|
+
|
|
179
|
+
- ✅ 所有 API 路由以 `/api` 开头,避免与前端路由冲突
|
|
180
|
+
- ✅ 使用 RESTful 设计:GET 查询、POST 创建、PUT 更新、DELETE 删除
|
|
181
|
+
- ✅ 返回统一的响应格式:`{ success: boolean, data?: any, error?: string }`
|
|
182
|
+
- ✅ 添加错误处理和参数验证
|
|
183
|
+
|
|
184
|
+
### 2. 样式开发
|
|
185
|
+
|
|
186
|
+
**使用 Tailwind CSS**
|
|
187
|
+
|
|
188
|
+
本项目使用 Tailwind CSS 进行样式开发,支持亮色/暗色模式自动切换。
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
// 使用 Tailwind 工具类
|
|
192
|
+
app.innerHTML = `
|
|
193
|
+
<div class="flex items-center justify-center min-h-screen bg-white dark:bg-black">
|
|
194
|
+
<h1 class="text-4xl font-bold text-black dark:text-white">
|
|
195
|
+
Hello World
|
|
196
|
+
</h1>
|
|
197
|
+
</div>
|
|
198
|
+
`;
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**主题变量**
|
|
202
|
+
|
|
203
|
+
主题变量定义在 `src/index.css` 中,支持自动适配系统主题:
|
|
204
|
+
|
|
205
|
+
```css
|
|
206
|
+
:root {
|
|
207
|
+
--background: #ffffff;
|
|
208
|
+
--foreground: #171717;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
@media (prefers-color-scheme: dark) {
|
|
212
|
+
:root {
|
|
213
|
+
--background: #0a0a0a;
|
|
214
|
+
--foreground: #ededed;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
37
217
|
```
|
|
38
218
|
|
|
39
|
-
|
|
219
|
+
**常用 Tailwind 类名**
|
|
220
|
+
|
|
221
|
+
- 布局:`flex`, `grid`, `container`, `mx-auto`
|
|
222
|
+
- 间距:`p-4`, `m-4`, `gap-4`, `space-x-4`
|
|
223
|
+
- 颜色:`bg-white`, `text-black`, `dark:bg-black`, `dark:text-white`
|
|
224
|
+
- 排版:`text-lg`, `font-bold`, `leading-8`, `tracking-tight`
|
|
225
|
+
- 响应式:`sm:`, `md:`, `lg:`, `xl:`
|
|
226
|
+
|
|
227
|
+
### 2. 依赖管理
|
|
40
228
|
|
|
41
|
-
|
|
229
|
+
**必须使用 pnpm 管理依赖**
|
|
42
230
|
|
|
43
231
|
```bash
|
|
44
|
-
|
|
232
|
+
# ✅ 安装依赖
|
|
233
|
+
pnpm install
|
|
234
|
+
|
|
235
|
+
# ✅ 添加新依赖
|
|
236
|
+
pnpm add package-name
|
|
237
|
+
|
|
238
|
+
# ✅ 添加开发依赖
|
|
239
|
+
pnpm add -D package-name
|
|
240
|
+
|
|
241
|
+
# ❌ 禁止使用 npm 或 yarn
|
|
242
|
+
# npm install # 错误!
|
|
243
|
+
# yarn add # 错误!
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
项目已配置 `preinstall` 脚本,使用其他包管理器会报错。
|
|
247
|
+
|
|
248
|
+
### 3. TypeScript 开发
|
|
249
|
+
|
|
250
|
+
**类型安全**
|
|
251
|
+
|
|
252
|
+
充分利用 TypeScript 的类型系统,确保代码质量:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
// 定义接口
|
|
256
|
+
interface User {
|
|
257
|
+
id: number;
|
|
258
|
+
name: string;
|
|
259
|
+
email: string;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// 使用类型
|
|
263
|
+
function createUser(data: User): void {
|
|
264
|
+
console.log(`Creating user: ${data.name}`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// DOM 操作类型推断
|
|
268
|
+
const button = document.querySelector<HTMLButtonElement>('#my-button');
|
|
269
|
+
if (button) {
|
|
270
|
+
button.addEventListener('click', () => {
|
|
271
|
+
console.log('Button clicked');
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
**避免 any 类型**
|
|
277
|
+
|
|
278
|
+
尽量避免使用 `any`,使用 `unknown` 或具体类型:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
// ❌ 不推荐
|
|
282
|
+
function process(data: any) { }
|
|
283
|
+
|
|
284
|
+
// ✅ 推荐
|
|
285
|
+
function process(data: unknown) {
|
|
286
|
+
if (typeof data === 'string') {
|
|
287
|
+
console.log(data.toUpperCase());
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
## 常见开发场景
|
|
293
|
+
|
|
294
|
+
### 添加新页面
|
|
295
|
+
|
|
296
|
+
本项目是单页应用(SPA),如需多页面:
|
|
297
|
+
|
|
298
|
+
1. 在 `src/` 下创建新的 `.ts` 文件
|
|
299
|
+
2. 在 `vite.config.ts` 中配置多入口
|
|
300
|
+
3. 创建对应的 `.html` 文件
|
|
301
|
+
|
|
302
|
+
### DOM 操作
|
|
303
|
+
|
|
304
|
+
```typescript
|
|
305
|
+
// 获取元素
|
|
306
|
+
const app = document.getElementById('app');
|
|
307
|
+
const button = document.querySelector<HTMLButtonElement>('.my-button');
|
|
308
|
+
|
|
309
|
+
// 动态创建元素
|
|
310
|
+
const div = document.createElement('div');
|
|
311
|
+
div.className = 'flex items-center gap-4';
|
|
312
|
+
div.textContent = 'Hello World';
|
|
313
|
+
app?.appendChild(div);
|
|
314
|
+
|
|
315
|
+
// 事件监听
|
|
316
|
+
button?.addEventListener('click', (e) => {
|
|
317
|
+
console.log('Clicked', e);
|
|
318
|
+
});
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### 数据获取
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
// Fetch API
|
|
325
|
+
async function fetchData() {
|
|
326
|
+
try {
|
|
327
|
+
const response = await fetch('https://api.example.com/data');
|
|
328
|
+
const data = await response.json();
|
|
329
|
+
return data;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
console.error('Failed to fetch data:', error);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// 使用数据
|
|
336
|
+
fetchData().then(data => {
|
|
337
|
+
console.log(data);
|
|
338
|
+
});
|
|
45
339
|
```
|
|
46
340
|
|
|
47
|
-
|
|
341
|
+
### 环境变量
|
|
342
|
+
|
|
343
|
+
在 `.env` 文件中定义环境变量(需以 `VITE_` 开头):
|
|
48
344
|
|
|
49
345
|
```bash
|
|
50
|
-
|
|
346
|
+
VITE_API_URL=https://api.example.com
|
|
51
347
|
```
|
|
52
348
|
|
|
53
|
-
|
|
349
|
+
在代码中使用:
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
const apiUrl = import.meta.env.VITE_API_URL;
|
|
353
|
+
console.log(apiUrl); // https://api.example.com
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## 技术栈
|
|
357
|
+
|
|
358
|
+
**前端:**
|
|
359
|
+
- **构建工具**: Vite 7.x
|
|
360
|
+
- **语言**: TypeScript 5.x
|
|
361
|
+
- **样式**: Tailwind CSS 3.x
|
|
362
|
+
|
|
363
|
+
**后端:**
|
|
364
|
+
- **框架**: Express 4.x
|
|
365
|
+
- **内置中间件**: express.json(), express.urlencoded(), express.static()
|
|
366
|
+
|
|
367
|
+
**工具:**
|
|
368
|
+
- **包管理器**: pnpm 9+
|
|
369
|
+
- **运行时**: Node.js 18+
|
|
370
|
+
- **开发工具**: tsx (TypeScript 执行器)
|
|
54
371
|
|
|
55
|
-
|
|
372
|
+
## 参考文档
|
|
373
|
+
|
|
374
|
+
**前端:**
|
|
375
|
+
- [Vite 官方文档](https://cn.vitejs.dev/)
|
|
376
|
+
- [TypeScript 官方文档](https://www.typescriptlang.org/zh/docs/)
|
|
377
|
+
- [Tailwind CSS 文档](https://tailwindcss.com/docs)
|
|
378
|
+
|
|
379
|
+
**后端:**
|
|
380
|
+
- [Express 官方文档](https://expressjs.com/)
|
|
381
|
+
- [Express 中文文档](https://expressjs.com/zh-cn/)
|
|
382
|
+
|
|
383
|
+
## 重要提示
|
|
384
|
+
|
|
385
|
+
1. **必须使用 pnpm** 作为包管理器
|
|
386
|
+
2. **使用 TypeScript** 进行类型安全开发,避免使用 `any`
|
|
387
|
+
3. **使用 Tailwind CSS** 进行样式开发,支持响应式和暗色模式
|
|
388
|
+
4. **环境变量必须以 `VITE_` 开头** 才能在客户端代码中访问
|
|
389
|
+
5. **开发时使用 `coze dev`**,支持热更新和快速刷新
|
|
390
|
+
6. **API 路由以 `/api` 开头**,避免与前端路由冲突
|
|
391
|
+
7. **单进程架构**:开发和生产环境都是前后端在同一进程中运行
|
|
392
|
+
|
|
393
|
+
## 常见问题
|
|
394
|
+
|
|
395
|
+
**Q: 如何分离前后端端口?**
|
|
396
|
+
|
|
397
|
+
如果需要前后端分离部署,可以:
|
|
398
|
+
- 前端:使用 `npx vite` 单独启动(默认端口 5173)
|
|
399
|
+
- 后端:修改 `server.ts`,移除 Vite middleware,单独启动
|
|
400
|
+
|
|
401
|
+
**Q: 如何添加数据库?**
|
|
402
|
+
|
|
403
|
+
```bash
|
|
404
|
+
# 安装数据库客户端(以 PostgreSQL 为例)
|
|
405
|
+
pnpm add pg
|
|
406
|
+
pnpm add -D @types/pg
|
|
407
|
+
|
|
408
|
+
# 在 server.ts 中使用
|
|
409
|
+
import { Pool } from 'pg';
|
|
410
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
411
|
+
```
|
|
56
412
|
|
|
57
|
-
|
|
413
|
+
**Q: 如何部署?**
|
|
58
414
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
415
|
+
1. 运行 `coze build` 构建前后端
|
|
416
|
+
2. 将整个项目上传到服务器
|
|
417
|
+
3. 运行 `pnpm install --prod`
|
|
418
|
+
4. 运行 `coze start` 启动服务
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import eslint from '@eslint/js';
|
|
2
|
+
import tseslint from 'typescript-eslint';
|
|
3
|
+
import { defineConfig, globalIgnores } from 'eslint/config';
|
|
4
|
+
|
|
5
|
+
export default defineConfig([
|
|
6
|
+
eslint.configs.recommended,
|
|
7
|
+
...tseslint.configs.recommended,
|
|
8
|
+
globalIgnores([
|
|
9
|
+
'dist/**',
|
|
10
|
+
'dist-server/**',
|
|
11
|
+
'node_modules/**',
|
|
12
|
+
'scripts/**',
|
|
13
|
+
]),
|
|
14
|
+
]);
|
|
@@ -1,24 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "<%= appName %>",
|
|
3
3
|
"version": "1.0.0",
|
|
4
|
-
"description": "Vanilla TypeScript application with Vite (HTML + CSS + TS)",
|
|
4
|
+
"description": "Vanilla TypeScript application with Express + Vite (HTML + CSS + TS + Node API)",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "bash ./scripts/build.sh",
|
|
7
7
|
"dev": "bash ./scripts/dev.sh",
|
|
8
8
|
"preinstall": "npx only-allow pnpm",
|
|
9
9
|
"lint": "eslint",
|
|
10
|
-
"start": "bash ./scripts/start.sh"
|
|
10
|
+
"start": "bash ./scripts/start.sh",
|
|
11
|
+
"ts-check": "tsc -p tsconfig.json"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@supabase/supabase-js": "2.95.3",
|
|
15
|
+
"dotenv": "^17.2.3",
|
|
16
|
+
"express": "^4.21.2"
|
|
11
17
|
},
|
|
12
18
|
"devDependencies": {
|
|
19
|
+
"@types/express": "^5.0.0",
|
|
20
|
+
"@types/node": "^22.10.5",
|
|
13
21
|
"autoprefixer": "^10.4.20",
|
|
22
|
+
"coze-coding-dev-sdk": "^0.7.16",
|
|
23
|
+
"esbuild": "^0.24.2",
|
|
24
|
+
"eslint": "^9",
|
|
14
25
|
"only-allow": "^1.2.2",
|
|
15
26
|
"postcss": "^8.4.49",
|
|
16
27
|
"tailwindcss": "^3.4.17",
|
|
28
|
+
"tsup": "^8.3.5",
|
|
29
|
+
"tsx": "^4.19.2",
|
|
17
30
|
"typescript": "^5.6.0",
|
|
18
|
-
"
|
|
31
|
+
"typescript-eslint": "^8",
|
|
32
|
+
"vite": "^7.2.4"
|
|
19
33
|
},
|
|
20
34
|
"packageManager": "pnpm@9.0.0",
|
|
21
35
|
"engines": {
|
|
22
36
|
"pnpm": ">=9.0.0"
|
|
37
|
+
},
|
|
38
|
+
"pnpm": {
|
|
39
|
+
"overrides": {
|
|
40
|
+
"esbuild": "^0.27.2",
|
|
41
|
+
"is-generator-function": "1.0.10"
|
|
42
|
+
}
|
|
23
43
|
}
|
|
24
44
|
}
|