Skip to content

Commit

Permalink
docs(NA): breaking up packages into small pieces (elastic#135027)
Browse files Browse the repository at this point in the history
* docs(NA): update wording around embracing the monorepo

* docs(NA): best practises around breaking up packages

* Update best_practices.mdx

Co-authored-by: Spencer <[email protected]>
  • Loading branch information
mistic and Spencer authored Jun 24, 2022
1 parent f75976f commit d507305
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion dev_docs/contributing/best_practices.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,26 @@ When experimenting with code, it's completely fine to create a separate GitHub r

There are some exceptions where a separate repo makes sense. However, they are exceptions to the rule. A separate repo has proven beneficial when there's a dedicated team collaborating on a package which has multiple consumers, for example [EUI](https://github.com/elastic/eui).

It may be tempting to get caught up in the dream of writing the next package which is published to npm and downloaded millions of times a week. Knowing the quality of developers that are working on Kibana, this is a real possibility. However, knowing which packages will see mass adoption is impossible to predict. Instead of jumping directly to writing code in a separate repo and accepting all of the complications that come along with it, prefer keeping code inside the Kibana repo. A [Kibana package](https://github.com/elastic/kibana/tree/main/packages) can be used to publish a package to npm, while still keeping the code inside the Kibana repo. Move code to an external repo only when there is a good reason, for example to enable external contributions.
It may be tempting to get caught up in the dream of writing the next package which is published to npm and downloaded millions of times a week. Knowing the quality of developers that are working on Kibana, this is a real possibility. However, knowing which packages will see mass adoption is impossible to predict. Instead of jumping directly to writing code in a separate repo and accepting all the complications that come along with it, prefer keeping code inside the Kibana repo. A Kibana package follows the npm idioms and can be later converted into a npm package, moved into an external repo and be published into the npm if a good reason for it was found (for example to enable external contributions).

## Breaking up packages

We are currently working to fully leverage change-based tasks across the repository and this functionality works best when the dependency tree is made of small packages focused around a single responsibility. Having said that, we do not think that contributors should break up their packages into single functions from day one; having 10k packages that contain a single function would also be a problem. Instead, breaking up packages over time will be a common practice used to address bottlenecks in the dependency tree and will be a valuable tool if build performance is being compromised.

### When should a package be broken-up into smaller packages?
The goal when designing your package should be to mainly ensure that it has a single responsibility, and that anyone depending on your package would reasonably need everything exposed by it. This is
important for bundle sizes (the entire package will be loaded every time it is imported) and for change-based tasks (any time this package changes all dependent tasks will need to be run).
Packages should be broken into smaller packages if they have multiple responsibilities or serve as a collection of related things which usually won't be necessary at the same time.

### What is a package's "responsibility"? How do I know that my package has a single responsibility?
The "responsibilities" of a package are defined by the features, utilities, and components exposed by your package. If a developer tries to use your package, is it likely that they will use every
part of it or just some of it? If most people would only need part of your package, then we would argue that your package has multiple responsibilities. At this point it is probably time to break up
your package.

### How do we manage the transition from one package to many smaller packages?
When making a change like this, import statements that previously pointed to the now broken-up package need to be updated to point to the smaller packages, so we have written an ESLint rule
called `@kbn/imports/exports_moved_packages` which allows contributors to define the exports previously available from one package as now being available from another package and leverage
auto-fixing to migration all existing and new uses of this export to the proper package.

## Licensing

Expand Down

0 comments on commit d507305

Please sign in to comment.