Alex Rickabaugh
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Angular in Monorepos A "monorepo" is a single codebase (e.g. a single Git repository) that contains source code for multiple applications or libraries, often developed by different teams. Monorepos have a number of advantages as a project structure: 1. Since all code is built at the current HEAD of the repository, traditional complexity associated with versioning of APIs and libraries, or dependency management is gone. 2. Continuous Integration systems can verify that a change to a library will not break any dependent apps, without complex integration environments. 3. Large-scale refactoring changes can be performed as a single commit across multiple apps and libraries. Angular supports monorepo project structures with the right configuration. :::info If you use the Angular CLI to generate applications and libraries in a monorepo, the builds will all be configured correctly and you don't have to care about anything listed in this document. ::: :::warning This document discusses monorepo configuration with Angular in the context of Angular v9 and the "Ivy" renderer. It might not be applicable to previous versions of Angular. ::: ## Different types of Angular libraries There are two broad types of Angular libraries which could be present in a monorepo, "publishable" and "private". ### Publishable libraries Publishable libraries are intended to be distributed on NPM or a private package repository, and must conform to the Angular Package Format (APF) specification for libraries. One of the main constraints of the APF is that the library must export all of its public-facing types (such as NgModules or Components) via one or more "entrypoints". Deep imports into APF libraries are not allowed. If the library is built using the Angular CLI, it will be a publishable library in the Angular Package Format. If not using the CLI, the `ng-packagr` tool can be used to prepare a library in the APF for publishing. :::info This document is concerned with proper configuration of dependencies within the monorepo itself. It does not cover the configuration of the CLI or `ng-packagr` to package and publish specific libraries. For this, see the CLI or `ng-packagr` docs. ::: ### Private libraries Private (non-publishable) libraries are never intended for distribution outside of the repository in which they live. Private libraries are instead depended on by one or more applications within the monorepo itself. They can serve a number of purposes: 1. As a way to share code between multiple apps. 2. To split an application build up into multiple pieces, in order to speed up or parallelize the overall build. 3. To allow for a more granular development & testing cycle by only working on or testing a smaller part of the app. Unlike publishable libraries, private libraries have no special requirement to export their API via entrypoints. Deep imports into private libraries are legal, and may even be the desired approach. ## Composition of an Angular monorepo There are several aspects of configuration required to build Angular projects within a monorepo. ### Editor configuration For all but the largest monorepos, it's often desirable to open the entire repository in an IDE, with seamless code navigation between individual projects. This can be accomplished by configuring a single, top-level `tsconfig.json`, which the IDE will use when loading the project. In this tsconfig, TypeScript options common to the entire monorepo (strictness settings, etc) can be specified. Most importantly, path mappings should be configured which map each library's module specifier (the name used to import the library, e.g. `'@angular/cdk'`) to the library's `.ts` files. For example, the Angular monorepo contains a number of publishable libraries, including `@angular/core` and `@angular/common`, in the following layout: ``` packages/ core/ index.ts src/ ... common/ index.ts src/ ... ``` The code for `@angular/core` lives in `packages/core`, and the code for `@angular/common` lives in `packages/common`. A top-level `tsconfig.json` for this repo might look like: ```jsonld= { "compilerOptions": { "baseUrl": ".", "outDir": "dist", "paths": { "@angular/core": ["./packages/core"], "@angular/core/*": ["./packages/core/*"], "@angular/common": ["./packages/common"], "@angular/common/*": ["./packages/common/*"], }, ... } } ``` For each package, two path mappings are needed. The first maps the top-level package, the second maps files within it. Alternatively, a `tsconfig.json` for this repo might specify a single mapping for the entire `@angular` namespace. This is possible because the Angular repo follows the convention that the code for library `@angular/X` is under the directory `packages/X`. This would look like: ```jsonld "compilerOptions": { "paths": { "@angular/*": ["./packages/*"], }, } ``` No top-level mapping is needed because it is impossible to import `'@angular'` by itself. ### Build configuration :::info As mentioned above, this document is concerned with the build relationship between different projects in the monorepo. It does not cover packaging of publishable libraries via `ng-packagr`. ::: When it comes time to build a library or application, the editor configuration is not sufficient. Each project within the monorepo requires a separate tsconfig which specifies the _build_ options for that particular project. Often a project will have more than one configuration, such as a development build config and a production build config. :::warning These tsconfigs should not be named `tsconfig.json` as they will then be picked up by the editor, incorrectly overriding the top-level configuration. Instead, `tsconfig.build.json` or a similar name can be used. ::: The build tsconfig has several responsibilities: * Configuring TypeScript options for the build. * Declaring the inputs to the compilation. * Configuring the output of build artifacts. * Configuring path mappings for dependencies. Often it's desirable for each build configuration to extend the top-level editor tsconfig. In addition to saving lots of repetition, this setup ensures the editor and the compiler agree on the set of build options in use, and thus show similar errors to the user. #### Inputs configuration Specify inputs is done with the `files` option in tsconfig: ```jsonld= // packages/core/tsconfig.build.json { "extends": "../../tsconfig.json", "files": { "./index.ts" } } ``` This should always be configured to limit the set of TS files being passed into the compilation, and only include files which belong to the particular project being compiled. #### Output configuration It's highly recommended (but not necessary) to emit output artifacts into a "parallel" filesystem tree - one that matches the repository's own directory layout. Often this directory is called `dist`. Using a combination of the `outDir` and `rootDir` options in tsconfig, the `@angular/core` library located in `packages/core` can be configured to output to `dist/packages/core`: ```jsonld= // packages/core/tsconfig.build.json { "extends": "../../tsconfig.json", "compilerOptions": { "declaration": true, "outDir": "../../dist/packages/core", "rootDir": ".", }, "files": { "./index.ts" } } ``` `outDir` here configures the output directory for the built library. The `packages/core` directory structure is mirrored into the `dist/` output directory. The `rootDir` setting guarantees that regardless of how this build is invoked, `packages/core` will always end up being compiled to `dist/packages/core`. #### Path mappings of dependencies If the build tsconfig for a project extends the top-level editor tsconfig, it will include the path mappings defined there for all libraries. However, this is a problem: these path mappings map each library to its _source_ files. This is correct for the IDE, but when actually building a project Angular needs to receive its dependencies in their _compiled_ form. For example, `@angular/common` depends on `@angular/core`, so the `tsconfig.build.json` for `@angular/common` needs to map the `@angular/core` path to its compiled form in `dist`. ```jsonld= // packages/common/tsconfig.build.json { "extends": "../../tsconfig.json", "compilerOptions": { "declaration": true, "outDir": "../../dist/packages/common", "rootDir": ".", "paths": { "@angular/core": ["../../dist/packages/core"], "@angular/core/*": ["../../dist/packages/core/*"], } }, "files": { "./index.ts" } } ``` As before, two path mappings are needed: one for the top level and one for any subpaths. Unlike the editor configuration, this build tsconfig's path mappings point to the `dist` version of `@angular/core`. As a result, it's a prerequisite to build `@angular/core` before `@angular/common`, since the latter build depends on the output of the former. #### Bazel A build system like Bazel can really streamline this kind of setup. Bazel will automatically construct the needed tsconfigs, avoiding the need to manage this configuration yourself. Additionally, it understands the dependency structure of the projects in the monorepo, and will automatically build whatever projects need to be built in the correct order. ## Private libraries and import paths An interesting issue arises when an application compilation is split into multiple libraries.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully