# Library

Qwik uses the [library mode of Vite](https://vitejs.dev/guide/build.html#library-mode) to create component libraries.

In order to create a component library for Qwik, you need to make sure of specific rules, this is because the Qwik Optimizer needs to recognize your library as a Qwik library.

The easiest way to create a new component library is to use the build-in `library` starter:

```shell
pnpm create qwik library my-library
```

This will create a new folder called `my-library` with the following structure:

```bash {2,11,14}
|-- README.md
|-- package.json
|-- src
|   |-- components
|   |   |-- counter
|   |   |   `-- counter.tsx
|   |   `-- logo
|   |       `-- logo.tsx
|   |-- entry.ssr.tsx
|   |-- index.ts
|   `-- root.tsx
|-- tsconfig.json
`-- vite.config.ts
```

The most important files of a library are a properly configured `package.json` and `vite.config.ts`.

## package.json

```json
{
  "name": "my-qwik-library-name",
  "version": "0.0.1",
  "description": "Create a reusable Qwik component library",
  "main": "./lib/index.qwik.mjs",
  "qwik": "./lib/index.qwik.mjs",
  "types": "./lib-types/index.d.ts",
  "exports": {
    ".": {
      "import": "./lib/index.qwik.mjs",
      "require": "./lib/index.qwik.cjs",
      "types": "./lib-types/index.d.ts"
    }
  },
  "files": [
    "lib",
    "lib-types"
  ],
  "peerDependencies": {
    "@qwik.dev/core": "^2.0.0",
    "@qwik.dev/router": "^2.0.0"
  },
  "peerDependenciesMeta": {
    "@qwik.dev/router": { "optional": true }
  },
  "type": "module",
}
```

Notice the `qwik` field, this is the entry point for the Qwik Optimizer, it will use this file to generate the `index.qwik.mjs` file.

List `@qwik.dev/core`, and `@qwik.dev/router` if you use it, in `peerDependencies`, starting at the version you build with: the build imports runtime helpers that older versions lack. The starter's `vite.config.ts` keeps peer dependencies out of the build, so the app provides a single copy of Qwik. A production server may load your library from `node_modules` as it is, and it shares Qwik's runtime state with the app.

The file must be called with the `.qwik.mjs` extension, otherwise the Qwik Optimizer will not recognize it.

## vite.config.ts

```ts {8-12}
import { defineConfig } from 'vite';
import { qwikVite } from '@qwik.dev/core/optimizer';

export default defineConfig(() => {
  return {
    build: {
      target: 'es2020',
      lib: {
        entry: './src/index.ts',
        formats: ['es', 'cjs'],
        fileName: (format) => `index.qwik.${format === 'es' ? 'mjs' : 'cjs'}`,
      },
    },
    plugins: [qwikVite()],
  };
});
```

Compared to a normal Vite configuration, libraries will use [the `lib` mode of Vite](https://vitejs.dev/guide/build.html#library-mode), which requires you to properly configure `build.lib`.

## `src/index.ts`

This is the entry point of your library, it will export all the components, and functions that you want to expose to the world.

```ts
// As an example, we will export the Logo and Counter components
export { Logo } from './components/logo/logo';
export { Counter } from './components/counter/counter';
```

## Libraries are also Apps

The library starter is also a standalone Qwik app (without routing, nor Qwik Router), this is the reason why you will find the `entry.ssr.tsx` and `root.tsx` files.

Do not worry about them, they won't be part of the final library, but they are useful during development and testing, so you can test your components in a real Qwik app.

## Build and publish

In order to publish your library, you have to first build it in `lib` mode and then publish to npm.

```shell
pnpm run build
```

This will create a `lib` folder with the following structure. Then you can publish it to npm.

```shell
npm publish
```

Before first publishing, you might want to update the `package.json` fields, like `name`, `version`, `description`, etc. For any other subsequent publish, you will need to update the `version` field.
