How to make a Bazel TypeScript monorepo with individually deployable packages

524 views Asked by At

I've been trying to get a bazel monorepo with typescript to work. I have a couple of requirements in mind.

  1. I should be able to import local packages using @myworkspace/ instead of ../../../ and so on, without needing Bazel. This is mostly so I get autocomplete while I'm writing.
  2. The @myworkspace/ package should be the same during development and build time but only Bazel-managed dependencies should be resolved on imports when running sandboxed. Just so I know if I've messed up the name of the package in the js_library rule.
  3. There should only be one lock file for the whole project. All dependencies should be located at root/node_modules.
  4. It should be possible to individually deploy node packages i.e. @myworkspace/myCloudFunction.
  5. It should be possible to include local dependencies in packages that will deployed.

I'm new to Bazel and it seems like it requires some mentality changes when coming from the NPM ecosystem. After googling, I've managed to find something that works for points 1 and 2 (But I might be wrong). I've published the playground repo at https://github.com/vitorelourenco/bazelmono-ts (pretty much a copy from https://github.com/lokshunhung/bazel-ts-monorepo with some ideas I took from https://github.com/angular/angular)

My questions about points 3 and 4: Say I want the lib Lodash available on package @myworkspace/cloudFunction that will be deployed to Google Cloud Functions. If I install Lodash in the @myworkspace/cloudFunction folder, then Lodash will be added to package.json but I'll have a second node_modules folder and a second yarn.lock file, I don't want that. But if I install it in root/, then Lodash will not be added to the dependencies listed on package.json located at @myworkspace/cloudFunction, and when I deploy it, it won't install. Is there a smart way to handle this issue?

Point 5 is very similar. Ideally, the final Bazel output would have the local dependencies bundled in and ready to use but I can't seem to figure out a way to do it yet. I've tried adding a pkg_npm rule to //packages/app in the playgroup repo but couldn't get it to include //packages/common in it.

0

There are 0 answers