Accessing Shared Assets and Code in Nx Monorepo

Discussão em 'Angular' iniciado por monkey, Outubro 24, 2024.

  1. monkey

    monkey Guest

    To illustrate my question, I have an example Nx project with the following structure:

    /nx.json
    /tsconfig.base.json
    /apps
    /my-app
    /project.json (type application)
    /tsconfig.json
    /src
    /app
    /components
    /comp1
    comp1.component.ts
    /libs
    /my-lib
    /tsconfig.json
    /project.json (type library)
    /src
    /index.ts
    /icons
    icon1.svg
    icon2.svg
    /models
    base-models.ts
    /angular
    /components
    comp2
    comp2.component.ts


    The goal is to share the assets and code in the my-lib library.

    Using Shared Assets

    Say I have two components that need to use icons from the shared library, as shown below:

    Component 1: apps/my-app/src/app/components/comp1/comp1.component.ts Component 2: libs/my-lib/src/angular/components/comp2/comp2.component.ts In the template of each component, I want to include icons, perhaps something like this:

    <ion-icon slot="start" src="assets/my-icons/icon1.svg"></ion-icon>


    Using Shared Code

    I also want to share code from the libs/my-lib/src/models/base-models.ts file, which is exported in libs/my-lib/src/index.ts. For example:

    import { BaseModels } from '@monkey/my-lib/models';

    Issues

    Shared Assets: I tried to configure my-app's project.json to copy the shared assets from the library using the following:

    {
    "glob": "**/*",
    "input": "libs/my-lib/src/assets",
    "output": "assets"
    }


    However, after successfully building the project, the assets did not get copied as expected. I noticed some files were in the dist/apps/my-app folder, but not the assets I tried to copy.

    Shared Code: I'm conscious that sharing code across apps and libraries might involve configuring the nx.json, tsconfig.base.json, my-app/tsconfig.json, and my-app/project.json, but I'm not entirely sure what steps I missed.

    Questions


    1. How should the two example components access the shared icon icon1.svg correctly using the src="..." path?


    2. How do I properly configure Nx to use shared code from base-models.ts across both the library and app?


    3. My understanding of the nx methodology is that everything that can should go in the shared space. But it's not clear how tsconfig.base.json & the various tsconfig.json files should be structured. In some documentation I've seen, the .base doesn't even exist. It's very confusing...

    Notes

    I suspect the solution involves configuring various tsconfig files and ensuring the assets are properly handled during the build process.

    Any advice on why the assets aren't getting copied correctly would be much appreciated!

    Continue reading...

Compartilhe esta Página