Back to Nx

JS library generator examples

packages/js/docs/library-examples.md

22.7.13.7 KB
Original Source

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 in your project.json under the build target options to specify whether you wish to bundle your library or not.

Examples

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
Buildable with SWC compiler

Generate a buildable library using SWC as the compiler. This will use the @nx/js:swc executor.

bash
npx nx g @nx/js:lib libs/mylib --bundler=swc
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
Buildable, with Rollup as a bundler

Generate a buildable library using Rollup as the bundler. This will use the @nx/rollup:rollup executor. It will also use SWC 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:

jsonc
"build": {
  "executor": "@nx/rollup:rollup",
  "options": {
    //...
    "compiler": "babel"
  }
}
Buildable, with Vite as a bundler

Generate a buildable library using Vite as the bundler. This will use the @nx/vite:build executor.

bash
npx nx g @nx/js:lib libs/mylib --bundler=vite
Using ESBuild

Generate a buildable library using ESBuild 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:

jsonc
"build": {
  "executor": "@nx/esbuild:esbuild",
  "options": {
    //...
    "esbuildOptions": {
        "bundle": true
    }
  }
}
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
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
Non-buildable library

Generate a non-buildable library.

bash
npx nx g @nx/js:lib libs/mylib --bundler=none