How do you bundle a nonpublishable library with a publishable library in an Nx monorepo?

36 Views Asked by At

I want to use an Nx monorepo to break a published package's implementation into multiple modules. My monorepo contains one publishable library, the public interface. This library depends on multiple buildable libraries, also in the monorepo. These buildable libraries are internals and should not be published independently. How can I include the internal libraries when publishing the public library?

For example, these commands create a simple monorepo, in which publishable package b depends on internal package a:

npx create-nx-workspace monorepo --preset ts --nxCloud skip
cd monorepo
nx generate @nx/js:library a --unitTestRunner none --bundler tsc
nx generate @nx/js:library b --unitTestRunner none --bundler tsc --publishable --importPath b
echo "export * from '@monorepo/a';" >> b/src/index.ts

nx build b indeed recognizes the dependency on a and builds a first.

However, npm-pack-ing the build output:

nx build b
cd dist/b
npm pack --dry-run

Shows that the internal dependencies would not be in the published bundle:

npm notice === Tarball Contents === 
npm notice 112B README.md       
npm notice 170B package.json    
npm notice 54B  src/index.d.ts  
npm notice 250B src/index.js    
npm notice 137B src/index.js.map
npm notice 37B  src/lib/b.d.ts  
npm notice 174B src/lib/b.js    
npm notice 167B src/lib/b.js.map

Also, dist/b/src/index.js references a by its monorepo-internal identifier (@monorepo/a):

tslib_1.__exportStar(require("@monorepo/a"), exports);

It feels like there's an obvious step I'm missing - perhaps a bundler configuration.

0

There are 0 best solutions below