Skip to content

Latest commit

 

History

History
126 lines (86 loc) · 7.27 KB

2.commands.md

File metadata and controls

126 lines (86 loc) · 7.27 KB
title Commands
description Build a module with `tsdown`, and the `nuxt-module-build` commands and their options.
links
label icon to size
Source
i-simple-icons-github
xs

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:

  • build builds the module, with or without a tsdown.config.ts.
  • prepare generates the .nuxt directory 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.

tsdown

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.

Watch mode

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 --watch

Watch 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.

build

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.ts and src/runtime/, plus the entries in the build field of package.json. A module then needs no configuration file.

Build steps

tsdown and build both run these steps:

  1. Check the paths that package.json publishes, with the fields of publishConfig in place of the top-level ones. As the prepack or prepublishOnly script, also check that the package manager that packs applies publishConfig. See Checks on published paths.
  2. Run prepare first, if .nuxt/tsconfig.json is missing from the module root.
  3. Read the build configuration, and check that the module code loads with Node. See Module code.
  4. Empty the output directory.
  5. Bundle src/module.ts and any other bundled entry with tsdown.
  6. Transpile src/runtime/ and any other directory entry file by file.
  7. Generate the declarations of every entry and runtime file, each with the project of the module's tsconfig.json that includes it.
  8. Print the size of the output.
  9. Load the built module to write dist/module.json, and write dist/types.d.mts.
  10. Compare the paths in package.json with 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.

Arguments

Argument Description
ROOTDIR="." The module root.

Options

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.

prepare

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 #build aliases.
  • 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.

How prepare loads your module

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.

Arguments

Argument Description
ROOTDIR="." The module root.

Options

Option Default Description
--cwd=<directory> The module root. Takes precedence over ROOTDIR.