| title | Commands | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| description | Build a module with `tsdown`, and the `nuxt-module-build` commands and their options. | |||||||||
| links |
|
You build a module with tsdown, which reads the builder() plugin from your tsdown.config.ts. @nuxt/module-builder also installs the nuxt-module-build command, and the same command as nuxt-build-module, with two subcommands:
buildbuilds the module, with or without atsdown.config.ts.preparegenerates the.nuxtdirectory that types the module.
Name the subcommand every time: nuxt-module-build without one prints its usage.
The Nuxt CLI runs the same subcommands as nuxt build-module, so nuxt build-module build is the same as nuxt-module-build build. To see how a module's scripts combine these commands, read Develop and Publish.
npx tsdown [--outDir=<directory>] [--sourcemap] [--watch]tsdown builds the module for publishing with its tsdown.config.ts. Run it from the prepack script, so the package manager builds the module whenever it packs or publishes it:
{
"scripts": {
"prepack": "tsdown"
}
}tsdown needs a tsdown.config.ts that uses builder(). Without one, it doesn't build the module. It runs the same steps as build. tsdown's own options, such as --outDir and --sourcemap, take precedence over the config.
tsdown --watch builds the module, then rebuilds it when a source file changes. That includes the files that src/module.ts imports, and new files in the runtime directory:
npx tsdown --watchWatch mode keeps the output directory in place while its first build runs. A rebuild only writes the files whose content changed, and removes the files that the build doesn't emit any more. With vue-tsc 3.3.6 or later installed in the module, each rebuild frees the previous declaration program of its single-file components, apart from the sources it reuses. See Developing against dist.
npx nuxt-module-build build [ROOTDIR] [--cwd=<directory>] [--outDir=<directory>] [--sourcemap]build builds the module for publishing. It produces the same output, checks, and warnings as tsdown:
- With a
tsdown.config.ts, it builds with that config. - Without one, it builds
src/module.tsandsrc/runtime/, plus the entries in thebuildfield ofpackage.json. A module then needs no configuration file.
tsdown and build both run these steps:
- Check the paths that
package.jsonpublishes, with the fields ofpublishConfigin place of the top-level ones. As theprepackorprepublishOnlyscript, also check that the package manager that packs appliespublishConfig. See Checks on published paths. - Run
preparefirst, if.nuxt/tsconfig.jsonis missing from the module root. - Read the build configuration, and check that the module code loads with Node. See Module code.
- Empty the output directory.
- Bundle
src/module.tsand any other bundled entry with tsdown. - Transpile
src/runtime/and any other directory entry file by file. - Generate the declarations of every entry and runtime file, each with the project of the module's
tsconfig.jsonthat includes it. - Print the size of the output.
- Load the built module to write
dist/module.json, and writedist/types.d.mts. - Compare the paths in
package.jsonwith the files that the build produced.
The build fails when it can't generate a declaration correctly, and doesn't type check otherwise. TypeScript describes this. Build Output lists the files that the build writes.
| Argument | Description |
|---|---|
ROOTDIR="." |
The module root. |
| Option | Default | Description |
|---|---|---|
--cwd=<directory> |
The module root. Takes precedence over ROOTDIR. |
|
--outDir=<directory> |
The output directory, relative to the module root. Takes precedence over outDir in the tsdown config, which defaults to dist. The build empties it first, so it must be inside the module. |
|
--sourcemap |
Emit a source map next to every script. Takes precedence over sourcemap in the tsdown config. |
npx nuxt-module-build prepare [ROOTDIR] [--cwd=<directory>]prepare generates .nuxt in the module root. It runs nuxt prepare on the module root, as a Nuxt app with your module as its only module and with auto-imports disabled. It writes nothing to dist.
Your module's tsconfig.json references the tsconfigs that prepare generates, one for each part of the module. Your editor, your type check, and the declarations that the build generates then see the same types:
@nuxt/schema.- The
#app,#imports, and#buildaliases. - Anything your module adds, such as its templates and its server routes.
prepare also writes .nuxt/module-builder/types.d.mts. This file re-exports src/module.ts, and adds the same type augmentations as dist/types.d.mts. With source exports, the root export's types condition points at it, and the tsconfig for module code includes it.
In the app, server and shared tsconfigs, prepare resolves your module's own package name to declarations of module code, which it writes into .nuxt/module-builder/declarations. It writes those of the module code that the declarations of your bundled entries refer to. A declaration error there is a warning. Fix it as for an error that fails the build.
prepare loads src/module.ts with Node. It also reads the options of builder() from tsdown.config.ts, which it loads with Node too. Your module code therefore follows the rules in Module code. Until it does, prepare lists each problem and doesn't generate .nuxt.
prepare fails when the tsdown config sets cwd to another directory. tsdown's hooks and plugins don't run for prepare.
Run prepare again after you change anything that the module registers for types: its options, templates, type declarations, or server routes.
| Argument | Description |
|---|---|
ROOTDIR="." |
The module root. |
| Option | Default | Description |
|---|---|---|
--cwd=<directory> |
The module root. Takes precedence over ROOTDIR. |