2

I have a workspace that includes one app in the apps/sandbox folder and two libraries in libs folder.

I have these two libs:
@workspace/library library and @angular/nrwl to NPM

  1. libs/ui
  2. libs/cdk-ui

I need to publish ui library to NPM but it's uses the cdk-ui and I would like to publish only one and not both libraries as NPM deps.

How it would be possible to accomplish? I would like that after I type 'npm install @ccc/ui' both of the libraries to work as usual without any separate publishing.

If any additional explanations would be needed please tell.

UPD: I have encountered on this: https://github.com/nrwl/nx/issues/225#issuecomment-373668866

But it's lack of good explanations on how to do it. Does anyone actually did it?

hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • There's a difference between `npm install` and `npm i`. `npm i ui` will install the default peerDependencies too. The package.json will include `@ccc/ui`, the lockfile will include both `@ccc/ui` and `@ccc/cdk-ui`. – Pieterjan Mar 27 '22 at 15:39
  • I am publishing only UI library the CDK isn't a published library it's a shared-cdk like types, utils and helper methods, decorators and so on... I need it to be built as part of the ui library or am I mistaken by thinking it's possible? – hackp0int Mar 27 '22 at 15:42
  • @Pieterjan you are mistaken about the `npm install` https://stackoverflow.com/a/49564133/586439 – hackp0int Mar 27 '22 at 15:52
  • You're right. However I remember bumping my head into the table in the past because I published NPM packages (with peerDependencies) and when trying to install them I was getting the message `... requires a peer of ... but none is installed. You must install peer dependencies yourself`, which implied I had to run 5 commands before I could use the package... Odd... Did they reverse this behavior again back to (how it should be)? – Pieterjan Mar 27 '22 at 18:17
  • The problem is I'm unable to actually get the helper library working with another helper library. The second one isn't getting compiled as a dependency. And I don't want manually install it. – hackp0int Mar 27 '22 at 18:51

1 Answers1

0

You can extend your npm publish command by writing a custom script on your UI lib's package.json that runs the cdk/ui library build at the end of your ui build command which then copy your build library with "cpx" inside the first one.

And to achieve that they end up in same place and directory, you'll have to change the tsconfig.lib.json of "cdk/ui": specifically outDir of your cdk/ui (which should match the other one)

So basically in your UI's package.json you have something like this:

{
    "build-library": "ng build ui  && npm run build-cdk-ui",
    "publish": "npm run build-library && npm run publish-dist",
    "build-cdk-ui": "tsc -p yourFolderForCDK-ui/tsconfig.lib.json && cpx your-resulted-dist dist/the-dist-you-want-to-put"
}

which triggers ng build for UI (which creates a dist folder), and after that you do ng build cdk/ui, which puts that dist inside other dist folder with cpx library that you should install and then you publish that folder.

Can Geylan
  • 266
  • 1
  • 6
  • Thank you so much for your answer!!!! At least one person which answered me. I even discussed it with developers and they haven't gave me an answer, just said - tomorrow and that's it! I will set your answer as answered if you please create a git repo for example purposes. PLEASE!!! – hackp0int Apr 03 '22 at 11:31
  • Can you please show me a demo of it in work? – hackp0int Apr 04 '22 at 11:41