Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
Create a module from the [module starter](https://github.com/nuxt/starter/tree/module):

```bash
npm create nuxt -- -t module my-module
pnpm create nuxt -t module my-module
cd my-module
pnpm dev
```

Building a module requires Node `^22.18.0 || ^24.11.0 || >=26.0.0`, with `tsdown` and `typescript` installed alongside `@nuxt/module-builder`. Apps that install a built module need Nuxt 3 or later.
Expand Down
21 changes: 13 additions & 8 deletions docs/1.project-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ my-module/
├── playground/ # a Nuxt app that installs the module
├── package.json
├── tsconfig.json # references the tsconfigs in .nuxt
├── tsconfig.test.json # type-checks tests and config files
└── tsdown.config.ts # the build configuration
```

Expand Down Expand Up @@ -138,17 +139,20 @@ export default defineNuxtConfig({
})
```

The playground has to resolve that name to the module directory. With pnpm, list `playground` in `pnpm-workspace.yaml` and use the `workspace:` protocol:
The playground has to resolve that name to the module directory. With pnpm, list `playground` in `pnpm-workspace.yaml`, and install the module with `link:..`:

```json [playground/package.json]
{
"name": "my-module-playground",
"private": true,
"type": "module",
"dependencies": {
"my-module": "workspace:*"
"my-module": "link:.."
}
}
```

With npm, list `playground` in the root `workspaces` and use `"my-module": "file:.."`.
`link:..` works with Yarn 4 and bun too. npm needs `file:..` instead. [Installing the module in the playground](/docs/api/module-builder/develop-and-publish#installing-the-module-in-the-playground) lists the setup for each package manager, and the `allowBuilds` that pnpm needs in `pnpm-workspace.yaml`.

The playground loads the module through the `exports` of `package.json`. With pnpm or Yarn 4, `exports` point at your sources, and `publishConfig.exports` point at `dist` for publishing. With npm, Yarn 3 or earlier, or bun, `exports` point at `dist`, and `tsdown --watch` rebuilds `dist` as you change your sources. [Develop and Publish](/docs/api/module-builder/develop-and-publish#preparing-a-checkout) describes both setups.

Expand All @@ -160,7 +164,7 @@ export default defineNuxtConfig({
})
```

It then loads `src/module.ts` whichever setup `package.json` uses, and doesn't resolve the package name. It only needs the dependency on `my-module` when the playground or a runtime file imports the module by name. Its type check doesn't read the module's `types` export, so it doesn't see the module's hooks and runtime config types: those of `ModuleHooks`, `ModuleRuntimeHooks`, `ModuleRuntimeConfig` and `ModulePublicRuntimeConfig`.
It then loads `src/module.ts` whichever setup `package.json` uses, and doesn't resolve the package name, so it works the same with every package manager. It only needs the dependency on `my-module` when the playground or a runtime file imports the module by name. Its type check doesn't read the module's `types` export, so it doesn't see the module's hooks and runtime config types: those of `ModuleHooks`, `ModuleRuntimeHooks`, `ModuleRuntimeConfig` and `ModulePublicRuntimeConfig`.

## `package.json`

Expand Down Expand Up @@ -193,7 +197,7 @@ This is a minimal `package.json` for a module that you publish with pnpm or Yarn
},
"scripts": {
"prepack": "tsdown",
"dev": "nuxt dev playground",
"dev": "nuxt-module-build prepare && nuxt dev playground",
"dev:prepare": "nuxt-module-build prepare && nuxt prepare playground"
},
"dependencies": {
Expand Down Expand Up @@ -235,22 +239,23 @@ To publish more entry points, such as `my-module/utils` or `my-module/components

## `tsconfig.json`

Your module's own `tsconfig.json` references the tsconfigs that `prepare` generates in the module's `.nuxt`, one for each part of the module:
Your module's own `tsconfig.json` references the tsconfigs that `prepare` generates in the module's `.nuxt`, one for each part of the module. It also references `tsconfig.test.json`, for `tsdown.config.ts` and the tests, which are in none of the parts:

```json [tsconfig.json]
{
"references": [
{ "path": "./.nuxt/tsconfig.node.json" },
{ "path": "./.nuxt/tsconfig.app.json" },
{ "path": "./.nuxt/tsconfig.server.json" },
{ "path": "./.nuxt/tsconfig.shared.json" }
{ "path": "./.nuxt/tsconfig.shared.json" },
{ "path": "./tsconfig.test.json" }
],
"files": [],
"exclude": ["dist", "node_modules", "playground"]
}
```

[TypeScript](/docs/api/module-builder/typescript) describes this setup, and how to change the options of one part.
[TypeScript](/docs/api/module-builder/typescript) describes this setup, and how to change the options of one part. [Tests and config files](/docs/api/module-builder/typescript#tests-and-config-files) shows `tsconfig.test.json`.

## `tsdown.config.ts`

Expand Down
72 changes: 52 additions & 20 deletions docs/3.develop-and-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ The [playground](/docs/api/module-builder/project-structure#playground) installs
| pnpm, Yarn 4 | Your sources, with the `dist` paths in `publishConfig` | The playground runs your sources. See [Source exports](#source-exports). |
| npm, Yarn 3 and earlier, bun | `dist` | `tsdown --watch` rebuilds `dist`. See [Developing against `dist`](#developing-against-dist). |

A playground that lists the module by relative path, as `modules: ['../src/module']`, loads `src/module.ts` in both setups, and its type check doesn't see the module's [type augmentations](/docs/api/module-builder/project-structure#srcmodulets). See [`playground/`](/docs/api/module-builder/project-structure#playground).
In both setups, the playground's `package.json` installs the module in a way that depends on the package manager. See [Installing the module in the playground](#installing-the-module-in-the-playground).

Run `dev:prepare` after you install dependencies. Run it again when the types that it generates are out of date, such as after the module registers a new template or server route. The module's own app, server and shared type checks see the types of module code as of the last `prepare`.

The build runs `prepare` on its own when `.nuxt` is missing. Keep `prepare` in `dev:prepare` anyway, so the module's types are in place before your editor or your type check needs them.

The playground needs `.nuxt` too: Vite reads the module's `tsconfig.json` when it transforms the module's TypeScript files, and that file references the tsconfigs in `.nuxt`. Until `prepare` has generated them, `nuxt dev playground` responds to every request with a 500 error, `[TSCONFIG_ERROR] Failed to load tsconfig '.nuxt/tsconfig.node.json'`. The `dev` script runs `nuxt-module-build prepare` before `nuxt dev playground`, so it also works in a fresh checkout.

### Source exports

pnpm and Yarn 4 publish the fields of `publishConfig` in place of the top-level ones. With either of them, point `exports` and `main` at `src`, and the same fields in `publishConfig` at `dist`:
Expand All @@ -48,7 +50,7 @@ pnpm and Yarn 4 publish the fields of `publishConfig` in place of the top-level
"main": "./dist/module.mjs"
},
"scripts": {
"dev": "nuxt dev playground",
"dev": "nuxt-module-build prepare && nuxt dev playground",
"dev:prepare": "nuxt-module-build prepare && nuxt prepare playground",
"prepack": "tsdown"
}
Expand All @@ -62,20 +64,7 @@ pnpm and Yarn 4 publish the fields of `publishConfig` in place of the top-level

`publishConfig` replaces each field as a whole, so `publishConfig.exports` repeats every subpath and condition of `exports`. [Exports](/docs/api/module-builder/exports#source-exports) shows an example with subpaths.

The playground has to resolve the module's name to the module directory itself. With pnpm, list `playground` in `pnpm-workspace.yaml`, and install the module with `workspace:*` or `link:..`:

```yaml [pnpm-workspace.yaml]
packages:
- playground
```

```json [playground/package.json]
{
"dependencies": {
"my-module": "workspace:*"
}
}
```
The playground has to resolve the module's name to the module directory itself, as [Installing the module in the playground](#installing-the-module-in-the-playground) describes.

The playground's type check includes your sources and the files that they import types from. [The playground's `tsconfig.json`](/docs/api/module-builder/typescript#with-source-exports) lists the errors that this can report.

Expand Down Expand Up @@ -136,7 +125,7 @@ npm, Yarn 3 and earlier, and bun publish some or all of these fields as written.
"dist"
],
"scripts": {
"dev": "nuxt dev playground",
"dev": "nuxt-module-build prepare && nuxt dev playground",
"dev:prepare": "nuxt-module-build prepare && tsdown && nuxt prepare playground",
"prepack": "tsdown"
}
Expand All @@ -149,7 +138,7 @@ npm, Yarn 3 and earlier, and bun publish some or all of these fields as written.
2. `tsdown` builds `dist`. See [`tsdown`](/docs/api/module-builder/commands#tsdown).
3. `nuxt prepare playground` generates the playground's `.nuxt`. It loads `dist/module.mjs` through `exports`.

With npm, list `playground` in the root `workspaces`, and install the module in the playground with `"my-module": "file:.."`. npm then links the module into the playground. With Yarn 1, use `"my-module": "link:.."`.
The playground installs the module as [Installing the module in the playground](#installing-the-module-in-the-playground) describes.

While the playground runs, run `tsdown --watch` next to it:

Expand All @@ -171,11 +160,54 @@ Watch mode keeps `dist` in place while its first build runs, so you can start bo
}
```

### Installing the module in the playground

The playground's `package.json` installs the module from the module directory, under the name that the playground's `nuxt.config` lists. The package manager decides how, in both setups:

| Package manager | Workspace | `my-module` in `playground/package.json` |
|---|---|---|
| pnpm | `playground` in `packages` of `pnpm-workspace.yaml` | `link:..` |
| Yarn 4 | `playground` in the root `workspaces` | `link:..` |
| bun | `playground` in the root `workspaces` | `link:..` |
| npm | `playground` in the root `workspaces` | `file:..` |
| Yarn 1 | None: run `yarn install` in `playground` as well | `link:..` |

```json [playground/package.json]
{
"name": "my-module-playground",
"private": true,
"type": "module",
"dependencies": {
"my-module": "link:.."
}
}
```

- `link:..` links the module directory, whatever the `name` in its `package.json`. `workspace:*` works with pnpm, Yarn 4 and bun too, but only while the dependency's name matches that `name`.
- bun installs a workspace only when its `package.json` has a `name`.
- npm doesn't support `link:`. Yarn copies a `file:..` dependency when it installs it, so the playground doesn't see later changes, or a `dist` built after the install.
- Yarn 1 enables workspaces only in a private package, so the root of a published module can't list them.
- With Yarn 3, none of these links the module directory.

With pnpm, also list the dependencies that have build scripts in `allowBuilds`:

```yaml [pnpm-workspace.yaml]
packages:
- playground

allowBuilds:
esbuild: false
```

pnpm 11 and later fail the install while a dependency with a build script is missing from `allowBuilds`. `pnpm approve-builds` adds the missing ones: `true` runs the script, and `false` skips it. pnpm 10 skips them with a warning.

A playground that lists the module by relative path, as `modules: ['../src/module']`, loads `src/module.ts` without the dependency, so it works the same with every package manager, in both setups. Its type check doesn't see the module's [type augmentations](/docs/api/module-builder/project-structure#srcmodulets). See [`playground/`](/docs/api/module-builder/project-structure#playground).

## Working in the playground

The playground is a Nuxt app that installs your module, so you can try the module as you change it:

- `npm run dev` starts the playground's development server with `nuxt dev playground`.
- The `dev` script runs `nuxt-module-build prepare`, then starts the playground's development server with `nuxt dev playground`.
- `nuxt build playground` builds the playground.
- Any other `nuxt` command works against the playground too, as `nuxt <command> playground`.

Expand Down Expand Up @@ -284,7 +316,7 @@ A `release` script can publish a new version in one step. Use the package manage

The script runs these steps:

1. Run the linter, the tests, and the build. The release stops if any of them fails.
1. Run the linter, the tests, and the build. The release stops if any of them fails, before `changelogen` commits and tags a new version.
2. Run `changelogen --release`, which bumps the version, updates `CHANGELOG.md` from your commit messages, and commits and tags the result.
3. Publish the package. `prepack` builds the module again, so `dist` has the new version.
4. Push the release commit and its tag with `git push --follow-tags`.
Expand Down
4 changes: 3 additions & 1 deletion docs/5.typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ A module has two TypeScript setups: one for the module itself, and one for its p
}
```

Type-check the module with `vue-tsc -b --noEmit`. Each part is checked with the types it has in an app that installs the module, and the build generates each file's declarations with the project that includes it, so your editor, your type check, and `dist` see the same types. `nuxt typecheck` in the module root checks it as an app instead, and regenerates `.nuxt` without the changes that `prepare` makes, so use it for the playground only, as `nuxt typecheck playground`.
`tsdown.config.ts` and the tests are in none of these parts. Add a fifth reference for them, to a `tsconfig.test.json`, as [Tests and config files](#tests-and-config-files) shows.

Type-check the module with `vue-tsc -b --noEmit`, with `vue-tsc` in the module's `devDependencies`. Each part is checked with the types it has in an app that installs the module, and the build generates each file's declarations with the project that includes it, so your editor, your type check, and `dist` see the same types. `nuxt typecheck` in the module root checks it as an app instead, and regenerates `.nuxt` without the changes that `prepare` makes, so use it for the playground only, as `nuxt typecheck playground`.

The `.nuxt` in the module root types the module as Nuxt sees it when the module is installed alone. It uses the module's default options, and disables auto-imports. It's separate from the playground's `.nuxt`, which reflects the playground's configuration and any other modules that it installs.

Expand Down
31 changes: 8 additions & 23 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,20 @@ Nuxt 3 has reached end of life, so these docs describe Nuxt 4 and later. They do

Create a module from the starter:

::code-group{sync="pm"}

```bash [npm]
npm create nuxt -- -t module my-module
```

```bash [yarn]
yarn create nuxt -t module my-module
```

```bash [pnpm]
```bash [Terminal]
pnpm create nuxt -t module my-module
```

```bash [bun]
bun create nuxt --template=module my-module
```

::
The starter uses [source exports](/docs/api/module-builder/develop-and-publish#source-exports) with pnpm, so `create nuxt` installs its dependencies with pnpm, whichever package manager runs it. pnpm has to be installed.

Then install its dependencies and prepare it for development:
Then start the playground:

```bash
```bash [Terminal]
cd my-module
npm install
npm run dev:prepare
pnpm dev
```

`dev:prepare` generates the `.nuxt` directories that type your module and its playground. To find out what each step does, read [Develop and Publish](/docs/api/module-builder/develop-and-publish).
`dev` runs `nuxt-module-build prepare`, which generates the `.nuxt` that types your module, and then `nuxt dev playground`. To generate the types of the module and the playground without starting the playground, such as before a type check, run `pnpm dev:prepare`. To find out what each script does, or to develop with another package manager, read [Develop and Publish](/docs/api/module-builder/develop-and-publish).

## Manual installation

Expand All @@ -67,7 +52,7 @@ To add `@nuxt/module-builder` to an existing module, follow these steps:
pnpm add -D --ignore-workspace-root-check @nuxt/module-builder@^2.0.0-alpha.0 tsdown@^0.23.7 typescript nuxt
```

`--ignore-workspace-root-check` lets `pnpm add` install at the root of a pnpm workspace, and has no effect in a single package.
`--ignore-workspace-root-check` lets `pnpm add` install at the root of a pnpm workspace, and has no effect in a single package. If pnpm's `minimumReleaseAge` or Yarn's `npmMinimalAgeGate` rejects a recent release, see [Install v2 and tsdown](/docs/api/module-builder/upgrade-v2#step-1-install-v2-and-tsdown).

2. Create a `tsdown.config.ts` in the module root that uses the `builder()` plugin:

Expand All @@ -85,7 +70,7 @@ To add `@nuxt/module-builder` to an existing module, follow these steps:
```json [package.json]
{
"scripts": {
"dev": "nuxt dev playground",
"dev": "nuxt-module-build prepare && nuxt dev playground",
"dev:prepare": "nuxt-module-build prepare && nuxt prepare playground",
"prepack": "tsdown"
}
Expand Down
4 changes: 4 additions & 0 deletions docs/upgrade-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pnpm add -D --ignore-workspace-root-check @nuxt/module-builder@^2.0.0-alpha.0 ts
- `--ignore-workspace-root-check` lets `pnpm add` install at the root of a pnpm workspace, and has no effect elsewhere. Yarn 1 takes `-W` instead. Other package managers need no flag.
- If your module is a package of a monorepo, such as `packages/my-module`, run the command in the module's directory. Or add both packages to the catalog that the module's `package.json` uses.
- If pnpm's `minimumReleaseAge` fails the install with `ERR_PNPM_NO_MATURE_MATCHING_VERSION`, add the `<package>@<version>` that the error names to `minimumReleaseAgeExclude` in `pnpm-workspace.yaml`. Then install again. Without an error, the same setting can install an older v2 release than the latest one.
- If Yarn's `npmMinimalAgeGate` fails the install with `YN0016`, add the package that the error names to `npmPreapprovedPackages` in `.yarnrc.yml`. Then install again.

Then change the other dependencies:

Expand Down Expand Up @@ -236,6 +237,8 @@ If you publish with pnpm or Yarn 4, point `exports`, `imports`, and `main` at yo

If you publish with npm, Yarn 3 or earlier, or bun, keep `package.json` pointing at `dist`. Step 7 builds `dist` in `dev:prepare`, and `tsdown --watch` rebuilds it while you develop. [Develop and Publish](./3.develop-and-publish.md#preparing-a-checkout) describes both setups.

If the playground lists the module by name, install the module in `playground/package.json` as [Installing the module in the playground](./3.develop-and-publish.md#installing-the-module-in-the-playground) shows.

### Step 7: Update the scripts

In each script that runs the builder, change only the builder's commands, and keep every other command where it is. That covers `prepack`, `dev:prepare`, and any other script or CI workflow that runs the builder under one of its names:
Expand All @@ -246,6 +249,7 @@ In each script that runs the builder, change only the builder's commands, and ke
- `nuxt-module-build prepare --stub` becomes `nuxt-module-build prepare`.
- A script that only ran the stub build, such as `"stub": "nuxt-module-build build --stub ."`, has nothing left to run. Delete it, and remove it from the scripts that run it, such as `pnpm stub &&`.
- In `dev:prepare`, run `nuxt-module-build prepare` first, before the build and every `nuxt prepare`. Add it if it isn't there.
- In `dev`, run `nuxt-module-build prepare` before `nuxt dev playground`. Without `.nuxt`, the playground responds to every request with a 500 error.
- If `package.json` points at `dist`, run `tsdown` in `dev:prepare` before the first `nuxt prepare`. Do the same if a subpath stays at `dist` and the playground or tests load it.
- The build empties `dist` first, so a command that writes into `dist` has to run after `tsdown`.
- If `package.json` points at your sources, run `tsdown` as a command of `prepack` itself, so that the [pack checks](#publishing-source-exports) run. A `prepack` that runs another script, such as `pnpm run build`, isn't checked: move that script's commands into `prepack`.
Expand Down
Loading
Loading