4.1 KiB
| title | description |
|---|---|
| JS library generator examples | 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.
npx nx g @nx/js:lib 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
{% tabs %}
{% tab label="Buildable with default compiler (tsc)" %}
Generate a buildable library using the @nx/js:tsc executor. This uses tsc as the compiler.
npx nx g @nx/js:lib mylib
{% /tab %}
{% tab label="Buildable with SWC compiler" %}
Generate a buildable library using SWC as the compiler. This will use the @nx/js:swc executor.
npx nx g @nx/js:lib 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.
npx nx g @nx/js:lib mylib --bundler=tsc
{% /tab %}
{% tab label="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.
npx nx g @nx/js:lib 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:
"build": {
"executor": "@nx/rollup:rollup",
"options": {
//...
"compiler": "babel"
}
}
{% /tab %}
{% tab label="Buildable, with Vite as a bundler" %}
Generate a buildable library using Vite as the bundler. This will use the @nx/vite:build executor.
npx nx g @nx/js:lib mylib --bundler=vite
{% /tab %}
{% tab label="Using ESBuild" %}
Generate a buildable library using ESBuild as the bundler. This will use the @nx/esbuild:esbuild executor.
npx nx g @nx/js:lib 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:
"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.
npx nx g lib mylib --publishable
{% /tab %}
{% tab label="Using directory flag" %}
Generate a library named mylib and put it under a directory named myapp (libs/myapp/mylib)
npx nx g lib mylib --directory=myapp
{% /tab %}
{% tab label="Non-buildable library" %}
Generate a non-buildable library.
npx nx g @nx/js:lib mylib --bundler=none
{% /tab %}
{% /tabs %}