|
| 1 | +# Creating a New Library |
| 2 | + |
| 3 | +This guide explains how to create a new library in the Bitwarden monorepo using Nx. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Node.js and npm installed |
| 8 | +- Bitwarden monorepo cloned and dependencies installed |
| 9 | +- The Nx cli installed, or accessible over npx |
| 10 | + |
| 11 | +## Creating a Library with Nx |
| 12 | + |
| 13 | +We use the `@nx/js` plugin to generate new libraries. Follow these steps to create a new library: |
| 14 | + |
| 15 | +1. From the root of the monorepo, run the generator command: |
| 16 | + |
| 17 | + nx g @nx/js:lib my-new-lib --directory=libs/my-new-lib |
| 18 | + |
| 19 | + Replace `my-new-lib` with the name of your library. |
| 20 | + |
| 21 | +2. The generator will ask you questions about the library configuration. Here are recommended settings: |
| 22 | + |
| 23 | + - **Which bundler would you like to use?** Select `none` (we use our own bundling setup) |
| 24 | + - **What should be the project name?** The same name as your library (e.g., `my-new-lib`) |
| 25 | + - **Would you like to add a package.json?** Select `Yes` |
| 26 | + - **Which unit test runner would you like to use?** Select `jest` (our standard test runner) |
| 27 | + |
| 28 | +3. Once generated, the library will have this structure: |
| 29 | + |
| 30 | +``` |
| 31 | +libs/ |
| 32 | + my-new-lib/ |
| 33 | + src/ |
| 34 | + index.ts |
| 35 | + my-new-lib.ts |
| 36 | + tsconfig.json |
| 37 | + project.json |
| 38 | + package.json |
| 39 | + jest.config.js |
| 40 | +``` |
| 41 | + |
| 42 | +4. Update the `package.json` with the appropriate Bitwarden naming convention: |
| 43 | + |
| 44 | +```json |
| 45 | +{ |
| 46 | + "name": "@bitwarden/my-new-lib", |
| 47 | + "version": "0.0.0" |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +5. Update the library's path in the root `tsconfig.base.json` file to make it available to other projects: |
| 52 | + |
| 53 | +```json |
| 54 | +{ |
| 55 | + "compilerOptions": { |
| 56 | + "paths": { |
| 57 | + "@bitwarden/my-new-lib": ["libs/my-new-lib/src"], |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Building and Testing the Library |
| 64 | + |
| 65 | +- Build the library: `nx build my-new-lib` |
| 66 | +- Test the library: `nx test my-new-lib` |
| 67 | +- Lint the library: `nx lint my-new-lib` |
| 68 | + |
| 69 | +## Importing Your Library in Other Projects |
| 70 | + |
| 71 | +Once your library is created and built, you can import it in other projects within the monorepo: |
| 72 | + |
| 73 | +```ts |
| 74 | +import { someFunction } from "@bitwarden/my-new-lib"; |
| 75 | +``` |
| 76 | + |
| 77 | +## Best Practices |
| 78 | + |
| 79 | +- Follow the Bitwarden code style guidelines |
| 80 | +- Document your library's public API |
| 81 | +- Write unit tests for your library's functionality |
| 82 | +- If your library has dependencies on other libraries, make sure to add them to the `project.json` file |
0 commit comments