How to publish NX library with its dependencies bundled?
Asked Answered
C

1

5

This issue seems to be all over GitHub's NX repository, however I was not able to find any working solution there.

My workspace contains two buildable libraries: ui/avatar and ui/icon and one publishable library, called bar

The idea would be to use ui/avatar and ui/icon as internal monorepo libraries (e.g. these can be imported in the apps directly) but I also want to encapsulate these 2 buildable libs into bar and publish bar as a npm package so that external consumers can benefit from it.

Here's a general overview of the file structure:

📦libs
 ┣ 📂external
 ┃ ┗ 📂bar
 ┃ ┃ ┣ 📂src
 ┃ ┃ ┃ ┣ 📂lib
 ┃ ┃ ┃ ┃ ┗ 📜external-bar.module.ts
 ┃ ┃ ┣ 📜ng-package.json
 ┃ ┃ ┣ 📜package.json
 ┃ ┃ ┣ 📜project.json
 ┃ ┃ ┣ 📜tsconfig.json
 ┣ 📂ui
 ┃ ┣ 📂avatar
 ┃ ┃ ┣ 📂src
 ┃ ┃ ┃ ┣ 📂lib
 ┃ ┃ ┃ ┃ ┣ 📂avatar
 ┃ ┃ ┃ ┃ ┃ ┗ 📜avatar.component.ts
 ┃ ┃ ┃ ┃ ┗ 📜ui-avatar.module.ts
 ┃ ┃ ┃ ┣ 📜index.ts
 ┃ ┃ ┣ 📜ng-package.json
 ┃ ┃ ┣ 📜package.json
 ┃ ┃ ┣ 📜project.json
 ┃ ┃ ┣ 📜tsconfig.json
 ┃ ┗ 📂icon
 ┃ ┃ ┣ 📂src
 ┃ ┃ ┃ ┣ 📂lib
 ┃ ┃ ┃ ┃ ┣ 📂icon
 ┃ ┃ ┃ ┃ ┃ ┗ 📜icon.component.ts
 ┃ ┃ ┃ ┃ ┗ 📜ui-icon.module.ts
 ┃ ┃ ┃ ┣ 📜index.ts
 ┃ ┃ ┣ 📜ng-package.json
 ┃ ┃ ┣ 📜package.json
 ┃ ┃ ┣ 📜project.json
 ┃ ┃ ┗ 📜tsconfig.json

That initial part, works.
I'm able to import ui/avatar into bar and generate the @blue/bar package. The problem is that the generated package.json of @blue/bar looks as follows:

{
  "name": "@blue/bar",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^14.1.0",
    "@angular/core": "^14.1.0",
    "@blue/ui-avatar": "0.0.1" <-- this!!
  },
  "dependencies": {
    "tslib": "^2.3.0"
  },
  "module": "esm2020/blue-bar.mjs",
  "es2020": "esm2020/blue-bar.mjs",
  "esm2020": "esm2020/blue-bar.mjs",
  "typings": "index.d.ts",
  "exports": {
    "./package.json": {
      "default": "./package.json"
    },
    ".": {
      "types": "./index.d.ts",
      "es2020": "./esm2020/blue-bar.mjs",
      "esm2020": "./esm2020/blue-bar.mjs",
      "default": "./esm2020/blue-bar.mjs"
    }
  },
  "sideEffects": false
}

The generated package, expects ui/avatar to be a an existing npm package (and thus to exist in some registry) but what I really need is to bundle the ui/avatar as part of the @blue/bar package and not as a third party dependency.

I have tried to adjust the tsconfig.json file of the bar lib to somehow try to resolve the dependencies locally (using paths) but it doesn't seems to work.

  "compilerOptions": {
    "declarationMap": false,
    "paths": {
      "@blue/ui-avatar": ["libs/ui/avatar/src/index.ts"],
      "@blue/ui-icon": ["libs/ui/icon/src/index.ts"]
    }
  }

Any hints would be greately appreciated :)

Crucifer answered 31/8, 2022 at 5:26 Comment(4)
Looks like it's an open issue github.com/nrwl/nx/issues/4620Etui
@guzmanoj did you managed to solve that? I'm facing the same problemGather
I did actually. Need to find some time to explain the solution or even better publish the custom executor...Crucifer
@OrShalmayev npmjs.com/package/@altack/nx-bundlefyCrucifer
C
7

Eventually and considering that the issue remains unsolved, I decided to write a custom Nx executor to achieve the job. Will share it here so that maybe it could help others.

You can install the executor via npm from here https://www.npmjs.com/package/@altack/nx-bundlefy

Also, the source code is available in Github: https://github.com/altack/nx-bundlefy

Crucifer answered 16/5, 2023 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.