Publicidad

"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experience", Oleksii Levzhynskyi

Fwdays
29 de Nov de 2021
Publicidad

Más contenido relacionado

Publicidad

Más de Fwdays(20)

Publicidad

"How to Use Bazel to Manage Monorepos: The Grammarly Front-End Team’s Experience", Oleksii Levzhynskyi

  1. 1
  2. ● Tech Lead, Software Engineer ● The Grammarly Editor ● Grammarly browser extension (past) ● @grammarly/focal contributor Oleksii Levzhynskyi
  3. 2. The limitations that we've faced Agenda 4 1. Why use monorepo? 3. Short intro to Bazel 4. Almost real-world example of webapp with Bazel 5. Results and conclusions
  4. https://app.grammarly.com
  5. @g/utils @g/textarea @g/editor @g/webapp Why use monorepo? 6
  6. Alarm!! Vulnerability in tests dependency!!! @g/utils @g/textarea @g/editor @g/webapp Why use monorepo? 7
  7. Alarm!! Vulnerability in tests dependency!!! @g/utils @g/textarea @g/editor @g/webapp Why use monorepo? 8
  8. 1 2 3 Why use monorepo? All changes in one MR One place for all configs No need to repeat configuration or project setup (CI/CD, pull request config, dev config, so on) No abandoned packages No long chains of MR to update one line
  9. Monorepo v1: yarn workspaces + wsrun 10
  10. Monorepo v1: CI Build Deploy to stagings Verify Reports (danger, custom e2e reporter) units linters e2e 11
  11. Monorepo v1: CI Build Deploy to stagings Verify Reports (danger, custom e2e reporter) units linters e2e integration performance a11y business metrics 12
  12. Monorepo v1: Dependency management dep1 (tsc) dep2 (rollup) dep12 (tsc) dep3 (webpack) webapp_1 webapp_2 13
  13. .segnahc elif rof gnihctaW 14
  14. 15
  15. Monorepo v1: No caching/smart rebuilds for affected changes 16 Build on CI was ~50 minutes even for “isolated” packages (Ex: scripts for release or e2e tests)
  16. New packages should not affect average build time (ideally) Build only things that matter build tools as a dependency Remote execution The new Architecture Not only JS
  17. 18 utils build:esm build:cjs specs licenses core-functions build:esm build:cjs specs licenses webapp build:esm build:cjs specs licenses app for modern browsers Build only things that matter!
  18. Custom scripts yarn workspaces/lerna Microsoft rush Existing tools Bazel
  19. Bazel is a mature framework with huge community and stable architecture 20
  20. bazelbuild/rules_nodejs 21
  21. Bazel intro
  22. Basics: commands 23 bazel build bazel run bazel test
  23. Basics: pure builds; the “unix philosophy” Sources Output_1 Build Output_1 web app Build 24
  24. Basics: macros and starlark language 25
  25. Project structure 26
  26. Build-specific configs - package.json - tsconfig.cjs.json - tsconfig.esm.json - webpack.config.js Integration with IDE - package.json - tsconfig.json BUILD.bazel as a source of truth 27 BUILD.bazel
  27. What about “configs”? 28
  28. Example project
  29. Example project: simplified version of OnlineEditor 1. [TODO] build app with webpack 2. [TODO] tests 3. [TODO] dev-server for app 30
  30. load("@npm//webpack:index.bzl", “webpack") Webapp (app.grammarly.com) 31
  31. Webpack build 32 packages/webapp/BUILD.bazel
  32. 33 bazel build //packages/webapp:webapp
  33. load("@npm//jest-cli:index.bzl", "jest_test") Tests with jest 34
  34. Tests with jest (official example) 35 tools/jest/index.bzl
  35. Tests with jest (official example) 36 tools/jest/index.bzl
  36. bazel test //packages/webapp:test 37
  37. 38 yarn bazel test //...
  38. Build tool as a dependency 39 tools/webpack/index.bzl packages/webapp/BUILD.bazel
  39. Example project: simplified version of OnlineEditor 1. [DONE] build app with webpack 2. [DONE] tests 3. [TODO] dev-server for app 40
  40. Dev-server 41 tools/webpack/index.bzl
  41. ibazel run //packages/webapp:serve 42
  42. ibazel run //packages/webapp:serve 43 + webpack plugin to avoid watch conflicts
  43. Need more speed! 44
  44. incremental build with webpack Super fast with Bazel “worker mode” protocol 45 webpack dev build serve files (es-dev-server or similar)
  45. Real world results: ○ time of the cold build on CI: wasn’t really changed ○ time of the hot build on CI: 2 minutes, was 10 minutes (5 times as fast!) ○ time of the hot start locally: 5 seconds, was 4 minutes (~50 times as fast!) ○ dependency builds automatically without additional code changes ○ no packages with broken scripts in packages.json 46
  46. Example project: simplified version of OnlineEditor 1. [DONE] build app with webpack 2. [DONE] tests 3. [DONE] dev-server for app 47
  47. 48 + typescript build for “utils” package https://github.com/oleksiilevzhynskyi/bazel-web-monorepo
  48. It’s scalable! It’s fast! It works! 49
  49. Not like other front-end build tools It’s a challenge to wrap some front-end tools into Bazel as they use “conventions over configuration” approach Challenges The learning curve is high
  50. Need caching When to use it? 51 Huge monorepos with complex dependency chain Some parts should be build on specific env
  51. Thank you 52
  52. 53 devua.club //uncomment BASIC Programming stream Grammarly Engineering Digest: Sign Up Here
  53. Join us at Grammarly 54
Publicidad