angular-cli library create secondary entry point

后端 未结 3 1956
眼角桃花
眼角桃花 2021-02-13 18:26

I am trying to create what I believe is called a secondary entry point into my angular npm package. I want the following two entry points

@scope/data-service
@sc         


        
相关标签:
3条回答
  • 2021-02-13 18:37

    Thanks for the reply. Here is the solution I ended up with, it all revolved around setting up the index.ts and public_api.ts files correctly

    \---projects
        \---scope
            \---ngx-package
                |   karma.conf.js
                |   ng-package.json
                |   ng-package.prod.json
                |   package.json
                |   tsconfig.lib.json
                |   tsconfig.spec.json
                |   tslint.json
                |
                \---src
                    |   public_api.ts
                    |   test.ts
                    |
                    +---lib
                    |       package-client-config.ts
                    |       package-client.spec.ts
                    |       package-client.ts
                    |       package.module.ts
                    |
                    \---models
                        |   index.ts  (1)
                        |   package.json (2)
                        |   public_api.ts  (3)
                        |
                        \---src
                            |   public_api.ts  (4)
                            |
                            \---lib
                                |   model-a.ts
                                |   model-b.ts
                                |
                                \---hateoas
                                        hateoas.ts
    

    Ok so in the tree above notes the parens with numbers inside they correspond to the files below.

    1) /projects/scope/ngx-package/src/models/index.ts

    // export what ./public_api exports so we can reference models like
    // import { modelA } from './models'
    export * from './public_api';
    

    2) /projects/scope/ngx-package/src/models/package.json

    {
      "ngPackage": {}
    }
    

    3) /projects/scope/ngx-package/src/models/public_api.ts

    export * from './src/public_api';
    

    4) /projects/scope/ngx-package/src/models/src/public_api.ts

    export * from './lib/model-a';
    export * from './lib/model-b';
    export * from './lib/hateoas/hateoas';
    

    With this setup you only need to maintain your list of exports in one place. I tried a lot of other variations that didn't work this seemed to work without problem.

    0 讨论(0)
  • 2021-02-13 18:39

    I'm afraid that this is not an easy task with ng-packagr.

    For every "project" that you try to package, ng-packagr automatically detects all secondary packages.

    ng-packagr ignores tsconfig.lib.json files of secondary packages, it will use the tsconfig file provided with the primary package.

    It then loads a TS program for the primary and all secondaries, before compiling, with the tsconfig of the primary.

    This is done this way so the packager can then parse the code and create a dependency tree, which will tell it which package to render first, 2nd etc... YES, it also means that ng-packagr DOES NOT assume that a secondary package always depends on the primary, it might be the other way and it's valid...

    Now, up to this point everything should be ok, no errors etc... A TS program is created for all packages but without emitting anything so all is good.

    The error you see comes in the compilation phase, where the compiler try to emit files and throws. This is when ng-packagr logs "Compiling TypeScript sources through ngc"

    At this point, typescript is not happy with the reference to a file outside of the root, which is the case.

    One solution is to update the paths property in tsconfig to point to the output directory for every package that was built. So if package A was just compiled, we change/create paths record that points to the output library which will not be considered as a TS source... thus no error.

    This will work, I have tested it, but sadly it requires some work either in ng-packagr source code or, as I did it, using a custom angular devkit builder...

    With it you can replace the paths right after each compilation has finished, so next compilation will reference the built output and not the source code.

    Because ng-packagr build packages based on dependency graph we are safe to assume that this will work.

    0 讨论(0)
  • 2021-02-13 18:47

    Example Folder Layout for Secondary Entrypoints

    All you have to do is create a package.json file and put it where you want a secondary entry point to be created. One way this can be done is by mimicking the folder structure of the following example which has a testing entry point in addition to its main entry point.

    my_package
    ├── src
    |   ├── public_api.ts
    |   └── *.ts
    ├── ng-package.json
    ├── package.json
    └── testing
        ├── src
        |   ├── public_api.ts
        |   └── *.ts
        └── package.json
    

    The contents of my_package/testing/package.json can be as simple as:

    {
      "ngPackage": {}
    }
    

    No, that is not a typo. No name is required. No version is required. It's all handled for you by ng-packagr! When built, the primary entry point is imported by import {..} from '@my/library' and the secondary entry point with import {..} from '@my/library/testing'

    Source - https://github.com/ng-packagr/ng-packagr/blob/master/docs/secondary-entrypoints.md

    0 讨论(0)
提交回复
热议问题