How to package only the dist folder in a monorepo

Viewed 5234

I am using lerna and yarn workspaces in my monorepo. The package structure looks like this:

root
`--- packages
     |--- pkg1
     |    |--- src
     |    |    `--- index.ts
     |    `--- dist
     |         `--- index.js
     `--- pkg2
          |--- src
          |    `--- index.ts
          `--- dist
               `--- index.js
  • src folder gets compiled into dist folder.
  • From each package, I would like to publish only the dist folder so that the imports work without the need of a main entry in package.json pointing inside the dist folder. This is easy to do with lerna: lerna publish --contents dist.

This works fine for external projects needing to use this repo, however it doesn't work within the repo itself. For example, if pkg2, depends on pkg1, it looks for pkg1's index.js file at pkg1/index.js while in reality the compiled version is sitting under pkg1/dist/index.js. How can I make this work?

P.S. I have seen this work in repositories like material-ui, but I can't explain how it works there!

2 Answers

Have a look at the "files" field of package.json file https://docs.npmjs.com/files/package.json#files

The "files" field is an array of files to include in your project. If you name a folder in the array, then it will also include the files inside that folder. (Unless they would be ignored by another rule.)

Finally got the solution:

You need to add publishConfig in the package.json of the dependency to be injected, refer following:

enter image description here

Cheers

Related