Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/docs.
Something went wrong. Try again.
# Design and publish a reusable library
A library exposes behavior that other projects depend on. Its users need to know
which inputs it accepts, what it returns, what can fail, and which environments it
supports. Maintainers need to understand how those guarantees connect to the
implementation and release process.
This page is an overview. Separate package-architecture, API-design, and publishing
guides have not been added to this repository yet.
## Define the public contract
Start with one concrete use case and a complete example. Name the operation from
the caller's task. Document required input, defaults, returned values, errors,
side effects, and resource ownership. Use the same term for the same concept in
types, examples, error messages, and release notes.
For an asynchronous operation, distinguish requesting cancellation from stopping
the underlying work and releasing resources. State whether an input is reusable
or consumed once. A caller should not have to infer these rules from implementation
details.
Keep implementation helpers private unless a consumer needs a supported contract
for them. Test documented imports as a consumer would use them.
## Choose package formats from supported consumers
Do not add output formats only because another package publishes them. Record the
runtimes, versions, and module systems you intend to support, then test the built
package in those environments.
| Consumer requirement | Packaging decision to evaluate |
| --- | --- |
| ECMAScript module imports | ESM entrypoints and the corresponding type declarations |
| CommonJS `require()` | A compatible CommonJS entrypoint, if this consumer is supported |
| A browser script without a module loader | A separate browser distribution, if this is a supported use case |
For Node.js, [`package.json` and file extensions determine module interpretation](https://nodejs.org/api/packages.html).
The `exports` field defines public entrypoints. A dual ESM/CommonJS package needs
consumer checks for both paths; adding a second build…