My App

turbo 生成 Monorepo 架构说明

turbo 生成 Monorepo 架构说明

生成 Monorepo 架构
npx create-turbo@latest
删除apps和packages目录里面的文件
rm -rf apps/* packages/* node_modules

fumadocs-app安装

安装参考文档: fumadocs-app

cd apps
npm create fumadocs-app

展示过程如下:

apps % npm create fumadocs-app

> npx
> "create-fumadocs-app"

  Create Fumadocs App

  Project name
  docs

  Choose a template
  Next.js: Fumadocs MDX

  Use `/src` directory?
  No

  Configure linter?
  ESLint

  Choose a search solution?
  Default

  Do you want to install packages automatically? (detected as npm)
  No

  Project Generated

  Done


Open the project
cd docs

Run Development Server
npm run dev

You can now open the project and start writing documents

修改运行next端口号

package.json
{
  //...
  "scripts": {
    //...
    "build": "next build",
    "dev": "next dev",
    "dev": "next dev -p 8070",
    "start": "next start"
    //...
  }
  //...
}
删除docs的.git
cd docs
rm -rf .git

nest安装

安装参考 nest安装

参考文档 --skip-install --skip-git

cd ..
# 如果没安装全局nest/cli则安装。安装了则无需再次安装
npm i -g @nestjs/cli
# 创建nest项目
nest new nest-app --skip-install --skip-git

修改nest的packages,由于turbo run dev,执行的都是dev所以这里需要增加一个dev启动

package.json
{
  "scripts": {
    // ...
    "dev": "nest start --watch",
    "start:dev": "nest start --watch"
    // ...
  }
}

避免端口号重复,修改nest的默认端口号3000为8080

src/main.ts
//...
async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  await app.listen(process.env.PORT ?? 3000)
  await app.listen(process.env.PORT ?? 8080)
}
//...

tsdown安装

安装参考文档 tsdown安装

cd ../packages
npm create tsdown@latest

展示过程如下:

packages % npm create tsdown@latest

> npx
> "create-tsdown"

  Creating a tsdown project...

  What is the name of your package?
  cg

  Which template do you want to use?
  Default

  Template cloned

  Done! Now run:
  cd cg
  npm install
  npm run build

For more information, visit: https://tsdown.dev/

修改cg/package.json的name属性为cg,version修改为0.0.1

package.json
{
  "name": "tsdown-starter",
  "name": "@repo/cg",
  "type": "module",
  "version": "0.0.0",
  "version": "0.0.1"
}

tsdown打包产物为dist目录,所以需要修改

turbo.json
{
  //...
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "inputs": ["$TURBO_DEFAULT$", ".env*"],
      "outputs": [".next/**", "!.next/cache/**"],
      "outputs": [".next/**", "!.next/cache/**", "dist/**"]
    }
    //...
  }
}
cd ..
npm install
apps/docs/src/app/layout.tsx
<html lang="en" className={inter.className} suppressHydrationWarning>
  <body className="flex flex-col min-h-screen">
    <RootProvider>{children}</RootProvider>
    <RootProvider {...({ children } as RootProviderProps)} />
  </body>
</html>

测试

npm run build

给nest-app安装@repo/cg

npm install @repo/cg -w nest-app

nest-app的package.json增加依赖

package.json
{
  "dependencies": {
    "@nestjs/common": "^11.0.1",
    "@nestjs/core": "^11.0.1",
    "@nestjs/platform-express": "^11.0.1",
    "@repo/cg": "^0.0.1",
    "reflect-metadata": "^0.2.2",
    "rxjs": "^7.8.1"
  }
}

测试调用方法在

dev/nest-app/src/main.ts
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { fn } from '@repo/cg'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)

  console.log(fn())

  await app.listen(process.env.PORT ?? 8080)
}
bootstrap()
npm run dev

控制台会输出Hello, tsdown!, 表示调用成功

提交commit初始化

git add .
git commit -m "初始化: turbo Monorepo 项目"

On this page