You need to enable JavaScript to run this app. 如何创建类Angular官方的多层级库及实现简洁导入方式
优惠活动
大模型
产品
解决方案
定价
更多

如何创建类Angular官方的多层级库及实现简洁导入方式

Building a Scoped Angular Monorepo Like the Official Angular Repo

Great question! The Angular team's repo structure is a custom monorepo setup for scoped packages, and it doesn't come straight out of the box with Angular CLI. Let's break down how to replicate it, answer your specific questions, and fix those import path issues.

How the Official Angular Repo Is Structured

First, let's clarify the Angular team's setup:

  • The root package.json has "name": "angular-srcs" because this is just the monorepo root identifier—it's not a published package. The actual published packages live in the packages/ directory.
  • They didn't use the default projects/ directory from Angular CLI; they manually renamed it to packages/ because the CLI's default structure is designed for standalone libraries, not nested scoped sub-packages like @angular/common/http.
  • Nested sub-packages (like @angular/common/http) are created manually, not via CLI commands. Each sub-package gets its own package.json with the scoped name, and they configure build/import paths manually (since the CLI doesn't support nested scoped libraries natively).

Step-by-Step to Build Your @myglobal/particular Monorepo

There's no single CLI command to generate this exact structure, but you can combine CLI commands with manual adjustments to get there:

1. Initialize the Monorepo & Adjust Directory Structure

  • Start with the standard monorepo setup:
    ng new myglobal --create-application=false
    cd myglobal
    
  • Rename the default projects/ directory to packages/ (this is manual—CLI doesn't have a command for this):
    mv projects packages
    

2. Configure the Root package.json

Update the root package.json to act as the monorepo root (not a published package):

{
  "name": "myglobal-srcs", // Like Angular's "angular-srcs"
  "private": true, // Critical—prevents accidental publishing of the root
  "workspaces": [ // For npm/yarn workspaces (optional but helpful)
    "packages/*",
    "packages/particular/*"
  ]
}

3. Create Your Scoped Libraries

For @myglobal/particular (main package):

Use the CLI to generate the library, but specify the custom project root to bypass the default projects/ directory:

ng generate library particular --project-root packages/particular

Then update packages/particular/package.json to use the scoped name:

{
  "name": "@myglobal/particular",
  "version": "0.0.1",
  // Keep peerDependencies, dependencies, and build entry points (main/module/typings) as generated
}

For nested sub-packages like @myglobal/particular/common:

  • Manually create the directory packages/particular/common
  • Initialize a new library structure here (you can copy the config from particular and adjust):
    • Create a package.json with "name": "@myglobal/particular/common"
    • Add an angular.json entry for this sub-package (pointing to its directory)
    • Write your library code in src/ as usual

4. Fix VS Code & Build Errors

The error you saw about package name pattern means your scoped name wasn't valid:

  • Ensure your scope (@myglobal) uses lowercase letters, numbers, and only -/_ characters (matches npm's package naming rules)
  • Double-check each sub-package's package.json name follows the pattern @scope/package/subpackage (e.g., @myglobal/particular/common)

Achieving Clean Import Paths & Proper Publishing

To get imports like import { ParticularComponent } from '@myglobal/particular' (instead of the long path):

  1. Build Configuration: Ensure each library's angular.json build output points to a dist/ subdirectory matching the package name (e.g., dist/packages/particular).
  2. Package Entry Points: Verify each package.json has correct main, module, typings, and exports fields pointing to the build artifacts. The Angular CLI sets these up automatically when you generate a library—you just need to keep them in sync.
  3. Publishing:
    • Build each library individually with ng build @myglobal/particular (and same for sub-packages)
    • Navigate to the build output directory (e.g., dist/packages/particular)
    • Run npm publish from there (you'll need to be logged into npm with an account that owns the @myglobal scope)

For bulk publishing across the monorepo, tools like Lerna or Nx can help manage versioning and publish all packages at once.

内容的提问来源于stack exchange,提问作者cknelle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:57:32