question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Building a library with dependent libraries defined via paths

See original GitHub issue

Type of Issue

[ x] Bug Report
[ ] Feature Request

Description

I have a project with monorepo structure. In general, it somethink like this

-apps -projects –lib1 –lib2 –lib3

And these libs has configured paths in tsconfig: @project/lib1 - projects/lib1, etc…

And libs can use other libs, for example lib1 can be used in lib2 and lib2 can be used in lib3 via path.

When I trying a build lib which depends on other lib (in example case lib1) via ng build --project=lib1 ng-packagr builds a strange dist directory structure. There is an example of ng-package.json of first lib.

{
  "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../dist/libs/lib1",
  "deleteDestPath": false,
  "workingDirectory": ".",
  "lib": {
    "entryFile": "index.ts",
  }
}

and in this case resulting sturcture in dist folder will be like this:

-libs
       -lib1
       | 
       |----esm5
       |      |----lib1
       |      |----lib2
       |      └----lib3
       |----esm2015
       |      |----lib1
       |      |----lib2
       |      └----lib3
       |----lib1
       |----lib2
       └----lib3

and ng-packagr calls rollup for dist/libs/lib1/ems2015/project-lib1.js, which calls an error, because this file is not exists.

Could not resolve entry (~/Documents/project/dist/libs/lib1/esm2015/project-lib1.js)
Error: Could not resolve entry (~/Documents/project/dist/libs/lib1/esm2015/project-lib1.js)
    at error (~/Documents/project/node_modules/rollup/dist/rollup.js:3766:30)
    at ~/Documents/project/node_modules/rollup/dist/rollup.js:18207:17

Is there a bug, or it is a configuration issue?

Do you have workaround for it?

How To Reproduce

Build project sturcture like described and call ng buld for project with dependencies.

Expected Behaviour

Successful build or my-code-related error.

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr: 5.0.1
@angular/*: 7.2.4
typescript: 3.1.5
rxjs: 7.2.0
node: v11.5.0
npm: 6.4.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:37 (6 by maintainers)

github_iconTop GitHub Comments

17reactions
freon27commented, Jun 28, 2019

I’ve also run into this problem and I have found a solution that appears to work (it lets me build my library at least) but I thought I’d share it and maybe someone can tell me if it would cause problems that I don’t see.

It seems too simple given all the discussion I’ve seen of this problem, so I’m assuming it’s already been discounted.

We have an Angular component library with multiple entry points so:

old setup


-lib
  |
  |-- component1
  |   |
  |   |- component1.ts
  |   |- component1.module.ts
  |   |- package.json
  |   |- public_api.ts 
  |
  |--component2
  |   |
  |   |- component2.ts
  |   |- component2.module.ts
  |   |- public_api.ts
  |   |- package.json
  |    
  |- package.json
  |- public_api.ts
  
  
component2/package.json:
{
  "ngPackage": {
    "lib": {
      "entryFile": “public_api.ts"
    }
  }
}
component2/public_api.ts:
export { Component2 } from ‘./component2.module';

This was working fine but when I tried to have component2 import component1 I ran into the rootDir error because component1 was not found in the component2 directory.

I have moved the component entry points up one level into the lib directory but leaving the package.json in the component directory.

This appears to cause rootDir to be set as the lib directory instead of the component directory and allow my library to build successfully.

new setup

-lib
  |
  |-- component1
  |   |
  |   |- component1.ts
  |   |- component1.module.ts
  |   |- package.json
  |     
  |
  |--component2
  |   |
  |   |- component2.ts
  |   |- component2.module.ts
  |   |- package.json
  |    
  |- package.json
  |- public_api.ts
  |- component1-public_api.ts
  |- component2-public_api.ts


component2/package.json:
{
  "ngPackage": {
    "lib": {
      "entryFile": “../component2-public_api.ts"
    }
  }
}
component2-public_api.ts:
export { Component2 } from ‘./component2/component2.module';

I hope that makes sense and I’d be really grateful if someone has some input on whether it’s a valid solution. Apologies for hijacking the thread but it seems relevant to the discussion.

7reactions
wmccrackencommented, Jun 19, 2019

I’m facing the same issue as @qballer . I have an angular monorepo with a number of libraries in it. These libraries import from each other based on paths defined in the root tsconfig.json file:

    "paths": {
      "@x/footer": ["libs/footer/src/index.ts"],
      "@x/assets": ["libs/assets/src/index.ts"],
      "@x/navbar": ["libs/navbar/src/index.ts"],
      "@x/sidebar": ["libs/sidebar/src/index.ts"],
      "@x/auth": ["libs/auth/src/index.ts"],
      "@x/helpers": ["libs/helpers/src/index.ts"],
      "@x/api": ["libs/api/src/index.ts"],
      "@x/grid": ["libs/grid/src/index.ts"]
    }

The auth lib for example makes use of @x/helpers and @x/api

Read more comments on GitHub >

github_iconTop Results From Across the Web

Run-Path Dependent Libraries - Apple Developer
A run-path dependent library is a dependent library whose complete install name is not known when the library is created (see How Dynamic...
Read more >
How to build a shared library (.so) without hardcoded full ...
RPATH is used to add shared library search paths, used at runtime. If you want a relative path to be used in RPATH,...
Read more >
Building And Using Static And Shared "C" Libraries
Compile a list of object files, then insert them all into a shared library file. However, there are two major differences:
Read more >
Building Java Libraries Sample - Gradle User Manual
This guide demonstrates how to create a Java library with Gradle using gradle init . You can follow the guide step-by-step to create...
Read more >
Creating libraries - Angular
Integrating with the CLI using code-generation schematicslink ... A library typically includes reusable code that defines components, services, and other Angular ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found