nx/packages/js/docs/library-examples.md
Leosvel Pérez Espinosa dc67660fec
fix(misc): update artifact generator option descriptions and cleanup leftovers (#29077)
- Update artifact generator schemas:
- Clarify `path` is the artifact file path relative to the current
working directory
  - Clarify `name` is the artifact symbol name
- Remove prompt for `name` and remove it from the important options
(won't be displayed by default in Nx Console generation UI, it will be
part of the collapsed options) given that most of the time, it's meant
to match the filename (last segment of the `path`)
- Remove some leftover options related to the name and path formats that
were previously missed
- Fix an issue with NestJS generators
- Fix an issue with Next `page` generator

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-11-28 09:44:44 -05:00

137 lines
4.1 KiB
Markdown

---
title: JS library generator examples
description: This page contains examples for the @nx/js:lib generator.
---
The `@nx/js:lib` generator will generate a library for you, and it will configure it according to the options you provide.
```bash
npx nx g @nx/js:lib libs/mylib
```
By default, the library that is generated when you use this executor without passing any options, like the example above, will be a buildable library, using the `@nx/js:tsc` executor as a builder.
You may configure the tools you want to use to build your library, or bundle it too, by passing the `--bundler` flag. The `--bundler` flag controls the compiler and/or the bundler that will be used to build your library. If you choose `tsc` or `swc`, the result will be a buildable library using either `tsc` or `swc` as the compiler. If you choose `rollup` or `vite`, the result will be a buildable library using `rollup` or `vite` as the bundler. In the case of `rollup`, it will default to the `tsc` compiler. If you choose `esbuild`, you may use the [`esbuildOptions` property](https://esbuild.github.io/api/) in your `project.json` under the `build` target options to specify whether you wish to bundle your library or not.
## Examples
{% tabs %}
{% tab label="Buildable with default compiler (tsc)" %}
Generate a buildable library using the `@nx/js:tsc` executor. This uses `tsc` as the compiler.
```bash
npx nx g @nx/js:lib libs/mylib
```
{% /tab %}
{% tab label="Buildable with SWC compiler" %}
Generate a buildable library using [SWC](https://swc.rs) as the compiler. This will use the `@nx/js:swc` executor.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=swc
```
{% /tab %}
{% tab label="Buildable with tsc" %}
Generate a buildable library using tsc as the compiler. This will use the `@nx/js:tsc` executor.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=tsc
```
{% /tab %}
{% tab label="Buildable, with Rollup as a bundler" %}
Generate a buildable library using [Rollup](https://rollupjs.org) as the bundler. This will use the `@nx/rollup:rollup` executor. It will also use [SWC](https://swc.rs) as the compiler.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=rollup
```
If you do not want to use `swc` as the compiler, and want to use the default `babel` compiler, you can do so in your `project.json` under the `build` target options, using the [`compiler` property](/nx-api/rollup/executors/rollup#compiler):
```jsonc {% fileName="libs/mylib/project.json" %}
"build": {
"executor": "@nx/rollup:rollup",
"options": {
//...
"compiler": "babel"
}
}
```
{% /tab %}
{% tab label="Buildable, with Vite as a bundler" %}
Generate a buildable library using [Vite](https://vitejs.dev/) as the bundler. This will use the `@nx/vite:build` executor.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=vite
```
{% /tab %}
{% tab label="Using ESBuild" %}
Generate a buildable library using [ESBuild](https://esbuild.github.io/) as the bundler. This will use the `@nx/esbuild:esbuild` executor.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=esbuild
```
If you want to specify whether you want to bundle your library or not, you can do so in your `project.json` under the `build` target options, using the [`esbuildOptions` property](https://esbuild.github.io/api/):
```jsonc {% fileName="libs/mylib/project.json" %}
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
//...
"esbuildOptions": {
"bundle": true
}
}
}
```
{% /tab %}
{% tab label="Minimal publishing target" %}
Generate a **publishable** library with a minimal publishing target. The result will be a buildable library using the `@nx/js:tsc` executor, using `tsc` as the compiler. You can change the compiler or the bundler by passing the `--bundler` flag.
```bash
npx nx g lib libs/mylib --publishable
```
{% /tab %}
{% tab label="In a nested directory" %}
Generate a library named `mylib` and put it under a directory named `nested` (`libs/nested/mylib`).
```shell
npx nx g lib libs/nested/mylib
```
{% /tab %}
{% tab label="Non-buildable library" %}
Generate a non-buildable library.
```bash
npx nx g @nx/js:lib libs/mylib --bundler=none
```
{% /tab %}
{% /tabs %}