lerna

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Lerna Monorepo Development

Lerna 单仓库开发

You are an expert in Lerna, the fast, modern build system for managing and publishing multiple JavaScript/TypeScript packages.
您是Lerna方面的专家,Lerna是一款用于管理和发布多个JavaScript/TypeScript包的快速、现代化构建系统。

Project Structure

项目结构

  • Organize packages following Lerna conventions:
    • packages/
      - All package directories (default)
    • Can customize with multiple directories in
      lerna.json
  • Each package should be self-contained with its own:
    • package.json
    • Source code
    • Tests
    • Build configuration
  • 遵循Lerna约定组织包:
    • packages/
      - 所有包的目录(默认)
    • 可在
      lerna.json
      中自定义多个目录
  • 每个包应独立包含自身的:
    • package.json
    • 源代码
    • 测试代码
    • 构建配置

Lerna Configuration

Lerna 配置

Configure
lerna.json
at the root:
json
{
  "$schema": "https://json.schemastore.org/lerna.json",
  "version": "independent",
  "npmClient": "npm",
  "packages": ["packages/*"],
  "useWorkspaces": true
}
  • Choose versioning mode:
    • "version": "independent"
      - Each package versioned separately
    • "version": "1.0.0"
      - Fixed/locked mode, all packages same version
  • Enable workspaces integration with
    useWorkspaces: true
在根目录配置
lerna.json
json
{
  "$schema": "https://json.schemastore.org/lerna.json",
  "version": "independent",
  "npmClient": "npm",
  "packages": ["packages/*"],
  "useWorkspaces": true
}
  • 选择版本控制模式:
    • "version": "independent"
      - 每个包独立版本化
    • "version": "1.0.0"
      - 固定/锁定模式,所有包使用相同版本
  • 启用工作区集成:
    useWorkspaces: true

Workspaces Integration

工作区集成

Configure npm/yarn/pnpm workspaces in root
package.json
:
json
{
  "workspaces": ["packages/*"],
  "private": true
}
  • Let the package manager handle hoisting and linking
  • Use Lerna for versioning, publishing, and running scripts
在根目录
package.json
中配置npm/yarn/pnpm工作区:
json
{
  "workspaces": ["packages/*"],
  "private": true
}
  • 让包管理器处理依赖提升和链接
  • 使用Lerna进行版本控制、发布和脚本执行

Task Execution

任务执行

  • Run scripts across packages:
    • lerna run build
      - Run build in all packages
    • lerna run test --scope=@org/package
      - Run in specific package
    • lerna run lint --since main
      - Run only in changed packages
  • Use
    --stream
    for real-time output
  • Use
    --parallel
    for concurrent execution
  • 在所有包中运行脚本:
    • lerna run build
      - 在所有包中执行构建
    • lerna run test --scope=@org/package
      - 在指定包中执行
    • lerna run lint --since main
      - 仅在有变更的包中执行
  • 使用
    --stream
    获取实时输出
  • 使用
    --parallel
    实现并发执行

Versioning Workflow

版本控制工作流

  • Update versions with
    lerna version
    :
    • lerna version patch
      - Bump patch version
    • lerna version minor
      - Bump minor version
    • lerna version major
      - Bump major version
    • lerna version
      - Interactive version selection
  • Lerna automatically:
    • Updates package.json versions
    • Updates internal dependency versions
    • Creates git tags
    • Pushes to remote
  • 使用
    lerna version
    更新版本:
    • lerna version patch
      - 升级补丁版本
    • lerna version minor
      - 升级次版本
    • lerna version major
      - 升级主版本
    • lerna version
      - 交互式选择版本
  • Lerna会自动完成以下操作:
    • 更新
      package.json
      中的版本号
    • 更新内部依赖的版本号
    • 创建Git标签
    • 推送到远程仓库

Publishing Packages

包发布

  • Publish with
    lerna publish
    :
    • lerna publish
      - Publish packages changed since last release
    • lerna publish from-git
      - Publish packages tagged in git
    • lerna publish from-package
      - Publish packages with unpublished versions
  • Configure npm registry in
    .npmrc
    or
    lerna.json
  • Use
    --dist-tag
    for pre-release versions
  • 使用
    lerna publish
    发布包:
    • lerna publish
      - 发布自上次发布以来有变更的包
    • lerna publish from-git
      - 发布Git中已标记的包
    • lerna publish from-package
      - 发布包含未发布版本的包
  • .npmrc
    lerna.json
    中配置npm仓库地址
  • 使用
    --dist-tag
    标记预发布版本

Change Detection

变更检测

  • Use
    --since
    flag for changed packages:
    • lerna run test --since main
    • lerna changed
      - List packages changed since last tag
    • lerna diff
      - Show diff since last release
  • Leverage affected commands in CI for efficiency
  • 使用
    --since
    标记检测有变更的包:
    • lerna run test --since main
    • lerna changed
      - 列出自上次标签以来有变更的包
    • lerna diff
      - 显示自上次发布以来的差异
  • 在CI中使用相关命令提升效率

Conventional Commits

约定式提交

Enable conventional commits for automated versioning:
json
{
  "command": {
    "version": {
      "conventionalCommits": true,
      "message": "chore(release): publish"
    }
  }
}
  • Commits determine version bumps:
    • fix:
      - Patch version
    • feat:
      - Minor version
    • BREAKING CHANGE:
      - Major version
  • Automatic changelog generation
启用约定式提交以实现自动化版本控制:
json
{
  "command": {
    "version": {
      "conventionalCommits": true,
      "message": "chore(release): publish"
    }
  }
}
  • 提交信息决定版本升级类型:
    • fix:
      - 补丁版本升级
    • feat:
      - 次版本升级
    • BREAKING CHANGE:
      - 主版本升级
  • 自动生成变更日志

Dependency Management

依赖管理

  • Use internal package references:
    json
    {
      "dependencies": {
        "@org/shared-utils": "^1.0.0"
      }
    }
  • Lerna keeps internal dependencies in sync during versioning
  • Hoist common dependencies to root with workspaces
  • 使用内部包引用:
    json
    {
      "dependencies": {
        "@org/shared-utils": "^1.0.0"
      }
    }
  • Lerna会在版本控制期间保持内部依赖同步
  • 通过工作区将公共依赖提升到根目录

CI/CD Integration

CI/CD 集成

  • Install dependencies once at root level
  • Use
    lerna run
    with
    --since
    for efficient CI
  • Publish from CI with proper npm authentication
  • Use
    --yes
    flag for non-interactive publishing
  • 在根目录一次性安装依赖
  • 使用
    lerna run
    结合
    --since
    实现高效CI
  • 通过CI发布时配置正确的npm认证
  • 使用
    --yes
    标记实现非交互式发布

Best Practices

最佳实践

  • Keep packages focused and single-purpose
  • Use consistent package naming:
    @org/package-name
  • Maintain clear dependency boundaries between packages
  • Document package APIs and usage
  • Use TypeScript with project references for type checking
  • Implement proper testing at package and integration levels
  • Consider Nx integration for advanced caching and task execution
  • 保持包的专注性和单一用途
  • 使用一致的包命名规范:
    @org/package-name
  • 维护包之间清晰的依赖边界
  • 记录包的API和使用方法
  • 结合TypeScript项目引用进行类型检查
  • 在包级别和集成级别实现适当的测试
  • 考虑集成Nx以实现高级缓存和任务执行