packages/js/docs/library-examples.md
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 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.
Generate a buildable library using the @nx/js:tsc executor. This uses tsc as the compiler.
npx nx g @nx/js:lib libs/mylib
Generate a buildable library using SWC as the compiler. This will use the @nx/js:swc executor.
npx nx g @nx/js:lib libs/mylib --bundler=swc
Generate a buildable library using tsc as the compiler. This will use the @nx/js:tsc executor.
npx nx g @nx/js:lib libs/mylib --bundler=tsc
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 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:
"build": {
"executor": "@nx/rollup:rollup",
"options": {
//...
"compiler": "babel"
}
}
Generate a buildable library using Vite as the bundler. This will use the @nx/vite:build executor.
npx nx g @nx/js:lib libs/mylib --bundler=vite
Generate a buildable library using ESBuild as the bundler. This will use the @nx/esbuild:esbuild executor.
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:
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
//...
"esbuildOptions": {
"bundle": true
}
}
}
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 libs/mylib --publishable
Generate a library named mylib and put it under a directory named nested (libs/nested/mylib).
npx nx g lib libs/nested/mylib
Generate a non-buildable library.
npx nx g @nx/js:lib libs/mylib --bundler=none