0Pricing
Git Advanced: Monorepo, Submodules & Workflows · 课时

单体仓库的结构与布局

学习在单个 Git 仓库中组织项目、软件包和共享代码的最佳实践

单体仓库的结构与布局 是 CoddyKit 上的免费 Git Advanced: Monorepo, Submodules & Workflows 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Git Advanced: Monorepo, Submodules & Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Structure Matters

Welcome! In a monorepo, many projects live together in one place. A clear, thoughtful structure is super important for keeping things organized.

It helps developers quickly find what they need, promotes code sharing, and makes managing the entire codebase much easier as it grows.

The Monorepo Root

At the very top level of your monorepo, the root directory, you'll find files that apply to the entire repository. Think of them as global settings.

  • .git/: Git's internal tracking files.
  • .gitignore: Tells Git which files to ignore.
  • README.md: The main description of your monorepo.
  • package.json: (If using Node.js) Monorepo-level scripts or dependencies.
  • tools/: Global utility scripts.

Grouping Projects: `apps` and `packages`

To keep things tidy, projects are usually grouped into top-level directories based on their type or purpose. The most common ones are apps/ and packages/ (sometimes libs/).

  • apps/: For deployable applications like web apps, mobile apps, or API services.
  • packages/ (or libs/): For reusable libraries, components, or shared utilities that multiple apps might use.

This clear separation helps you understand a project's role at a glance.

Inside the `apps` Folder

Each folder within apps/ represents a distinct, standalone application. They usually have their own source code, configuration, and dependencies.

monorepo/
├── apps/
│   ├── web-app/         # Your frontend application
│   │   ├── src/
│   │   └── package.json
│   ├── api-service/     # Your backend API
│   │   ├── src/
│   │   └── Dockerfile
│   └── mobile-app/      # Your mobile application
│       ├── src/
│       └── ...
└── ...

This structure keeps each application isolated while being part of the larger monorepo.

Inside `packages` (or `libs`)

The packages/ (or libs/) directory is where you put all your reusable code. These are often smaller, focused libraries or UI components that can be shared across multiple applications.

monorepo/
├── packages/
│   ├── ui-components/   # Reusable UI library
│   │   ├── src/
│   │   └── package.json
│   ├── shared-utils/    # Common utility functions
│   │   └── src/
│   └── auth-lib/        # Authentication logic
│       └── src/
└── ...

This promotes code reuse and consistency across your projects.

Centralized Configuration

To ensure consistency, many monorepos centralize configuration files for tools like linters (for code style), formatters (for code layout), and build systems.

monorepo/
├── .eslintrc.js       # Linter configuration
├── prettier.config.js # Code formatter configuration
├── jest.config.js     # Test runner configuration
├── tsconfig.base.json # Base TypeScript config
└── ...

These files often live at the root or in a dedicated config/ folder, applying rules consistently to all projects.

Benefits of Good Structure

A well-defined monorepo structure offers significant advantages:

  • Discoverability: New team members can quickly understand where everything is.
  • Consistency: Enforces uniform project layouts and coding standards effortlessly.
  • Maintainability: Makes refactoring and managing dependencies much simpler.
  • Tooling: Streamlines setup for monorepo-aware build and development tools.

It's an investment that pays off in the long run!

A Simple Program Example

While monorepo structure is about organizing files and folders, here's a basic Java program to demonstrate how runnable code appears in CoddyKit. This helps you get comfortable with the editor!

public class MonorepoConcept {
  public static void main(String[] args) {
    System.out.println("A well-structured monorepo is great!");
  }
}

Naming Conventions

Clear and consistent naming is crucial in a monorepo. It helps avoid confusion and makes the codebase more readable.

  • Kebab-case: Use kebab-case for project and folder names (e.g., web-app, shared-utils).
  • Specific names: Avoid generic names like utils; be more specific (e.g., date-utils, api-client).
  • Prefixes: For larger organizations, consider prefixing shared packages (e.g., @myorg/ui-components).

Monorepo Organization Check

Consider a monorepo containing a web application, an API service, and a shared UI component library. What are common and effective ways to organize these projects?

Recap: Structured Monorepos

Fantastic work! Today, we learned the importance of structuring a monorepo. We covered organizing applications into apps/, shared libraries into packages/ (or libs/), and centralizing global configurations.

A well-defined layout makes your monorepo easier to navigate, more consistent, and simpler to maintain. Next, we'll dive into specific tools that help manage these complex structures!

常见问题解答

「单体仓库的结构与布局」课时是免费的吗?

是的 — 「单体仓库的结构与布局」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Git Advanced: Monorepo, Submodules & Workflows 课程的其余内容,请升级到 CoddyKit PRO。 Git Advanced: Monorepo, Submodules & Workflows 课程共包含 4 节课。

「单体仓库的结构与布局」这节课中我会学到什么?

学习在单个 Git 仓库中组织项目、软件包和共享代码的最佳实践 你通过在浏览器中直接运行的动手代码来练习 Git Advanced: Monorepo, Submodules & Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Git Advanced: Monorepo, Submodules & Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Git Advanced: Monorepo, Submodules & Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「单体仓库的结构与布局」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Git Advanced: Monorepo, Submodules & Workflows 课中编写并运行代码吗?

能。每节 Git Advanced: Monorepo, Submodules & Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 什么是单体仓库?
  2. 单体仓库的结构与布局
  3. 单体仓库工具入门
  4. 单体仓库与多仓库:选择合适的方法
← 返回 Git Advanced: Monorepo, Submodules & Workflows