From 974c4293bec70e1f80deddc48af24ba1400b5787 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 22 Nov 2026 23:48:13 +0000 Subject: [PATCH 01/19] docs: list relative imports of module code that miss its augmentations --- docs/5.typescript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/5.typescript.md b/docs/5.typescript.md index b90bda25..397cff17 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -137,7 +137,7 @@ Each part is type-checked on its own, with the files that it imports and the typ - **`default` refers to itself** (TS7022 on the default export, TS7023 on a function in it, such as `moduleDependencies`). The module's own `.nuxt/types/modules.d.ts` types its options key and its entry in `ModuleDependencies` from `typeof import('../../src/module').default`. A `moduleDependencies` function without a return type is inferred from that same export. Give it one: `moduleDependencies(nuxt): ModuleDependencies`, with `ModuleDependencies` from `@nuxt/schema`. - **An ambient module declared twice** (TS2554, TS2748, or a wrong type for an export). A `.d.ts` in the module and a type template that both declare the same ambient module, such as `declare module '#my-module/bindings'`, both apply to each part, and the two declarations merge. Keep one of them. - **Errors in code that relies on `.nuxt`'s declarations**. Each part includes `.nuxt/nuxt.d.ts` and the declarations it references, such as the module's runtime config, typed routes and type templates. Code that relies on them is checked against them. Fix what they report. -- **Module code that a runtime file imports by relative path** (TS2339 on `nuxt.options`, TS2307 for `#app`). A runtime file that imports from `src/module.ts` or another file of module code by relative path, such as `import type { ImageProvider } from '../../module'`, brings that file into the type check of its own part. The server and shared parts don't have the module's augmentations, so `nuxt.options.` fails there, and so do the imports that module code makes. Import the type through the module's own package name instead, as `import type { ImageProvider } from 'my-module'`, or from a file that only declares types. +- **Module code that a runtime file imports by relative path** (TS2339 on `nuxt.options` or on a type that the module augments, TS7053, TS2307 for `#app`). A runtime file that imports from `src/module.ts` or another file of module code by relative path, such as `import type { ImageProvider } from '../../module'`, brings that file into the type check of its own part. A type file that imports module code, such as `import type { ModuleOptions } from '../module'`, does the same. The server and shared parts don't have the module's augmentations: neither those of `src/module.ts`, nor a `.d.ts` module augmentation such as `declare module './types'`, which goes to the app part and to module code only. So `nuxt.options.` and the augmented properties fail there, as `Property 'ipx' does not exist on type 'ModuleOptions'`, and so do the imports that module code makes. Import the type through the module's own package name instead, as `import type { ImageProvider } from 'my-module'`, or from a file that only declares types and imports no module code. - **Vite's module types not found** (TS2307 for an import such as `./worker.ts?worker&url` or `./styles.css?inline`, TS2882 for `import './styles.css'`). No part has the types of `vite/client`. Add `vite` to the module's `devDependencies`, and a `.d.ts` in the module root, such as `env.d.ts`, with `/// `. It applies to every part, as described in [The generated tsconfigs](#the-generated-tsconfigs). - **Errors in tests under `test/nuxt`** (TS2593 for `describe` and `it`, and other errors in tests). The app part includes `test/nuxt` and `tests/nuxt`, so these tests are checked with the app's types and without a test runner's globals. Import `describe` and `it` from `vitest`. Or add the directory to the root's `exclude`, such as `"exclude": ["dist", "node_modules", "playground", "test"]`, run `nuxt-module-build prepare` again, and type-check the tests as described in [Tests and config files](#tests-and-config-files). - **Errors in a template that the module generates** (such as TS7006 in a `.ts` template). A template with a `.ts` file name that a runtime file imports, through an alias or `#build`, is type-checked in that file's part, with its options. Type the code that the template generates, such as the parameters of its functions. From 3455a1ddde6ef70da5d8f1b9650461d9ed8eb519 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Nov 2026 00:08:27 +0000 Subject: [PATCH 02/19] docs: describe which paths point at sources and when to add tsconfig.test.json --- docs/3.develop-and-publish.md | 4 ++-- docs/5.typescript.md | 11 ++++++----- docs/7.exports.md | 15 +++++++++++++++ src/diagnostics.ts | 2 +- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index c24ca7e7..eb405c5b 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -245,11 +245,11 @@ To check the package before you publish it, follow these steps: npx publint ./my-module-1.0.0.tgz ``` -3. List the imports in the declarations that don't resolve from the package alone, with [Are the Types Wrong?](https://arethetypeswrong.github.io). Write its report to a file, and read the file with `jq`: +3. List the imports in the declarations that don't resolve from the package alone, with [Are the Types Wrong?](https://arethetypeswrong.github.io), and the number of files that each appears in. Write its report to a file, and read the file with `jq`. The first command exits with 1 whenever the report lists a problem: ```bash [Terminal] npx @arethetypeswrong/cli ./my-module-1.0.0.tgz --profile esm-only --format json > attw.json - jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.resolutionOption) \(.moduleSpecifier) \(.fileName)"' attw.json | sort -u + jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c ``` Each one has to be an alias that only a Nuxt app provides, such as `#app`, `#imports`, `#build/my-module/options`, or an alias that the module adds to the app, or, under `node10` for a module without `node10` consumers, one of the package's own `imports`. A module that supports `node10` consumers imports those files by relative path in the code that the declarations come from. Fix any other, such as a relative import of a file that the package doesn't contain. An app's `nuxt.config.ts` context reads the module's options type without the app's aliases, and types an alias that doesn't resolve as `any`, so move a type that the options type takes from such a file into one that doesn't import the aliases. diff --git a/docs/5.typescript.md b/docs/5.typescript.md index 397cff17..696ac0f8 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -22,7 +22,7 @@ A module has two TypeScript setups: one for the module itself, and one for its p } ``` -`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. +`tsdown.config.ts` and the tests are in none of these parts. To type check them, add a fifth reference, to a `tsconfig.test.json`, as [Tests and config files](#tests-and-config-files) shows. Otherwise, add them to `exclude`. 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`. @@ -100,7 +100,7 @@ Its `include` replaces the one that it extends, so it lists the server's declara ### Tests and config files -Other tests, fixtures, and config files in the module root such as `vitest.config.ts` are in none of the parts. Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no referenced tsconfig includes. Type-check tests and config files with a `tsconfig.test.json` that extends the node part, as they run in Node, and add it to the root's `references`: +Other tests, fixtures, and config files in the module root such as `vitest.config.ts` are in none of the parts. Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no referenced tsconfig includes. To type check them, use a `tsconfig.test.json` that extends the node part, as they run in Node, and add it to the root's `references`. Otherwise, add them to the root's `exclude`. ```json [tsconfig.test.json] { @@ -115,7 +115,8 @@ Other tests, fixtures, and config files in the module root such as `vitest.confi ``` - `erasableSyntaxOnly: false` turns off the option that the node part sets, as tests run through a transform. The build checks that `tsdown.config.ts` loads with Node. -- `include` replaces the one it extends, so it lists the declarations that module code is checked with. With a `tests` directory, write `./tests` and `./tests/nuxt`. +- `include` replaces the one it extends, so it lists the declarations that module code is checked with, then the tests and config files. With a `tests` directory, write `./tests` and `./tests/nuxt`. Leave out what the root's `exclude` leaves out, such as `./test/bundle/*/dist`. +- Types that only the tests bring in have to be in this program too. A setup file that `exclude` leaves out, such as a `test/nuxt/setup.ts` that adds a matcher like `toHaveNoViolations` to `expect`, goes in `files`, as `"files": ["./test/nuxt/setup.ts"]`, which `exclude` doesn't apply to. Without it, each use of the matcher fails with TS2339. - Tests that import module code or runtime files type-check those files with these options too. Tests that use `@nuxt/test-utils/e2e`, or mount components with `@vue/test-utils`, belong here. - `exclude` leaves out `test/nuxt`, which the app part checks with the app's types. Tests that import from `#imports` or use `mountSuspended` run in the Nuxt environment, so they go in `test/nuxt`. When the root's `exclude` leaves out `test`, the app part doesn't include `test/nuxt`, so remove `./test/nuxt` from this `exclude`. - `exclude` also leaves out the fixtures. Files that a test imports from `test/fixtures` are checked all the same. @@ -178,7 +179,7 @@ import { resolveOptions } from './options.ts' import { registerComponents } from './components/index.ts' ``` -Type-only imports don't stop the module from loading, because Node never loads them. Write their extensions too. A type check that resolves imports like Node requires them, such as the playground's check of its `nuxt.config.ts` context when the playground sets `future.compatibilityVersion: 5`. +Type-only imports don't stop the module from loading, because Node never loads them, and the module's own type check resolves them without an extension. Only the playground's type check requires their extensions, with [source exports](#with-source-exports), on Nuxt 5 or with `future.compatibilityVersion: 5`. Relative imports in other [bundled entries](/docs/api/module-builder/configuration#bundled-entries) carry their extension as well. Runtime files that module code doesn't import by value can leave out the extension, because the build adds it to their output. @@ -294,7 +295,7 @@ The root export's `types` condition points at `.nuxt/module-builder/types.d.mts` The type check can report these errors: - **`#app`, `#imports`, or `#build/*` not found (TS2307, TS2664) in the `nuxt.config.ts` context.** Module code imports types from a runtime file that uses these aliases. Move those types into a file that doesn't import the aliases, such as `src/runtime/types.ts`. -- **Relative imports without extensions (TS2835, TS2834) in the `nuxt.config.ts` context.** This context resolves imports like Node when the playground sets `future.compatibilityVersion: 5`. Add extensions to type-only relative imports as well. +- **Relative imports without extensions (TS2835, TS2834) in the `nuxt.config.ts` context.** This context resolves imports like Node on Nuxt 5, or when the playground sets `future.compatibilityVersion: 5`. Add extensions to type-only relative imports as well. - **Globals that the module declares in its own `.d.ts` not found (TS2304).** The playground doesn't include that file. Reference it with `/// ` from the files that use the globals. - **The module's own aliases not found (TS2307).** Move each alias from the module's tsconfig `paths` into the `imports` of `package.json`, with its `dist` paths in `publishConfig.imports`. TypeScript resolves those from the module's files in any project. - **A virtual module that the module adds not found (TS2307).** Declare its types with `addTypeTemplate(template, { nuxt: true, node: true })` from `@nuxt/kit`, so both of the playground's contexts include them. diff --git a/docs/7.exports.md b/docs/7.exports.md index 8e3a13a7..1c7c86f6 100644 --- a/docs/7.exports.md +++ b/docs/7.exports.md @@ -179,6 +179,21 @@ With [source exports](/docs/api/module-builder/develop-and-publish#source-export The build checks the paths in `publishConfig.exports`, not the source paths in `exports`. See [Checks on published paths](/docs/api/module-builder/develop-and-publish#checks-on-published-paths). +### Which paths point at sources + +A subpath points at its source when only Nuxt loads it: an app, through the module, its `nuxt.config`, or a runtime file. It points at `dist` when Node or a tool that can't read TypeScript loads it, such as a plain Vite app's `vite.config.ts`, an ESLint config, or a CLI. Build the module before that code runs. + +Each path of `exports` mirrors its path in `publishConfig.exports`: + +- **A wildcard subpath** with an extension in its `dist` target gets the source's extension: `"./runtime/*": "./dist/runtime/*.js"` becomes `"./src/runtime/*.ts"`, for its `types` condition too. TypeScript doesn't swap the extension of an `exports` target. +- **A wildcard subpath for `.vue` files**, whose `dist` target keeps the extension of the import, such as `"./dist/runtime/components/*"`, points at `"./src/runtime/components/*"`, for its `types` condition too. +- **A wildcard subpath for every file in `dist`**, such as `"./*": "./dist/*"`, stays at `dist`, as imports of it name the files in `dist`. +- **A subpath with only a `types` condition** that points at a file outside `dist`, such as `./client.d.ts`, stays as it is, in both fields. +- **A subpath for the module entry**, such as `"./module"`, points where the root export does. +- **An entry of `imports`** points at the source of its target. `./dist/types.d.mts` has none: point it at `./.nuxt/module-builder/types.d.mts`, as the root export's `types` condition does. +- **`main` and `module`** point at `./src/module.ts`, with the `dist` paths in `publishConfig`. +- **Other fields, such as `style`,** stay at `dist`. pnpm and Yarn 4 don't apply `publishConfig` to them. A `style` condition in `exports` follows `exports`, and points at the source stylesheet. + ## Importing your own exports Runtime code and module code can import the module's own package name, such as `my-module/utils`. The build never bundles these imports, so they resolve to the published files in the app that installs the module. diff --git a/src/diagnostics.ts b/src/diagnostics.ts index 488a5a8f..918a6a69 100644 --- a/src/diagnostics.ts +++ b/src/diagnostics.ts @@ -283,7 +283,7 @@ export const warningCodes = { }, NUXT_MB_C0303: { why: (p: { groups: Array<[path: string, files: number]> }): string => `No project that \`tsconfig.json\` references includes these TypeScript files, so \`vue-tsc -b\` does not type check them: ${p.groups.map(([path, files]) => path.endsWith('/') ? `\`${path}\` (${files} ${files === 1 ? 'file' : 'files'})` : `\`${path}\``).join(', ')}.`, - fix: 'Reference a tsconfig that includes them, such as a `tsconfig.test.json` for tests and config files, or add them to the `exclude` of `tsconfig.json`.', + fix: 'To type check them, reference a tsconfig that includes them, such as a `tsconfig.test.json`. Otherwise, add them to the `exclude` of `tsconfig.json`.', docs: '#tests-and-config-files', }, NUXT_MB_C0305: { From 5f802201c404ee2886d915be83e66ecc6e96477e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Nov 2026 00:08:27 +0000 Subject: [PATCH 03/19] docs: make each upgrade step decidable --- docs/upgrade-v2.md | 145 ++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 80 deletions(-) diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index fdce10cb..1f3616ee 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -7,7 +7,7 @@ This guide moves a Nuxt module from v1 to v2 of `@nuxt/module-builder`. Follow t ## What changes in v2 -v2 builds your module with [tsdown](https://tsdown.dev) instead of unbuild. The layout, with `src/module.ts` and `src/runtime/`, stays the same, and so does `dist`. Most other changes follow from one fact: Nuxt 5 loads a module's sources with Node as written. +v2 builds your module with [tsdown](https://tsdown.dev) instead of unbuild. The layout of `src` and `dist` stays the same. Most other changes follow from one fact: Nuxt 5 loads a module's sources with Node as written. - A `tsdown.config.ts` with the `builder()` plugin replaces `build.config.ts`, and `tsdown` replaces `nuxt-module-build build`. - `src/module.ts` and the files that it imports have to load with Node, so relative imports carry their extension. @@ -19,9 +19,9 @@ v2 builds your module with [tsdown](https://tsdown.dev) instead of unbuild. The ## Before you start - Use Node `^22.18.0 || ^24.11.0 || >=26.0.0`, locally and in CI. -- Find out which package manager publishes your module. Step 6 depends on it. -- The steps use `pnpm`. Use your module's own package manager instead. Run commands with its `exec`, such as `pnpm exec tsdown`, and scripts with its `run`, such as `pnpm run dev:prepare`. -- The builder's command is `nuxt-module-build`, with the alias `nuxt-build-module`. The Nuxt CLI also runs it as `nuxt build-module` and `nuxt module-build`, or the same with `nuxi`. The steps write `nuxt-module-build`, and apply to every name. +- Find out which package manager packs your module when you publish it: the one whose `pack` or `publish` command your release script or workflow runs. A release tool, such as release-it, uses npm unless its config or docs name another. Step 6 depends on it. +- The steps use `pnpm`. Use your module's own package manager instead. Run commands with its `exec`, such as `pnpm exec tsdown`, and scripts with its `run`, such as `pnpm run dev:prepare`. With Yarn 1, write `yarn add -D` for `pnpm add -D`, and `yarn pack --filename my-module-1.0.0.tgz` for `pnpm pack`. +- The steps write the builder's command as `nuxt-module-build`. They apply to its other names too: `nuxt-build-module`, and `nuxt build-module` or `nuxt module-build`, with `nuxt` or `nuxi`. ## Steps @@ -33,15 +33,16 @@ Install v2 together with `tsdown`, which is a peer dependency: pnpm add -D --ignore-workspace-root-check @nuxt/module-builder@^2.0.0-alpha.0 tsdown@^0.23.7 ``` -- `--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. +- `--ignore-workspace-root-check` lets `pnpm add` install at the root of a pnpm workspace, and has no effect elsewhere. Yarn 1 takes `-W` there instead. - 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 `@` 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. +- If pnpm fails with `ERR_PNPM_CATALOG_VERSION_MISMATCH`, set the version of the package that it names in the catalog of `pnpm-workspace.yaml` to the one in the command. Then run `pnpm install`. Then change the other dependencies: - If `unbuild` is in `devDependencies`, run `pnpm remove unbuild`. Remove `mkdist` and `jiti` too, if only the builder used them. -- If your module's own code imports `unbuild`, `mkdist`, `jiti`, `defu`, or `magic-regexp`, add that package to its dependencies. v2 doesn't install them any more. +- If a file other than `build.config.ts` imports `unbuild`, `mkdist`, `jiti`, `defu`, or `magic-regexp`, add that package to its dependencies. v2 doesn't install them any more. - If `src/runtime` contains `.scss` or `.sass` files, run `pnpm add -D --ignore-workspace-root-check sass`. v2 compiles them with your module's own `sass` or `sass-embedded`. ### Step 2: Move the build options into `tsdown.config.ts` @@ -67,9 +68,15 @@ If your module has a `build.config.ts`, or a `build` or `unbuild` field in `pack | `sourcemap` | `sourcemap` | | `hooks['build:before']` | tsdown's `hooks['build:prepare']`, where `ctx.options.cwd` is the module root | | `hooks['build:done']` | tsdown's `hooks['build:done']` | -| `hooks['rollup:done']` | a plugin that [emits the extra files](./4.configuration.md#extra-files-in-the-output), or `package.json` paths that point at the files the build emits | +| `hooks['rollup:done']` that renames files | `package.json` paths that point at the files the build emits | +| `hooks['rollup:done']` that adds files | a plugin that [emits the extra files](./4.configuration.md#extra-files-in-the-output), unless only a `require` condition, which step 3 removes, used them | +| `hooks['rollup:options']` | the tsdown option that it sets, such as `treeshake: false` | | `rollup.inlineDependencies` | a list: tsdown's `deps.onlyBundle`. `true`: `deps.onlyBundle: false`. `false`: nothing, drop it | -| `hooks['mkdist:*']`, `declaration`, `failOnWarn`, `name`, and `parallel` | nothing: drop them | +| `failOnWarn` | tsdown's `failOnWarn` | +| `declaration` | nothing: the build always writes declarations | +| `rollup.emitCJS: false`, `rollup.cjsBridge`, `rollup.output.exports` | nothing: they only change CommonJS output | +| an entry's `esbuild` JSX options | nothing: [JSX](./6.output.md#jsx) stays for the app to compile | +| `hooks['mkdist:*']`, `name`, `parallel` | nothing: drop them | | any other option | see [Unsupported build options](#unsupported-build-options) | For example, this `build.config.ts`: @@ -106,19 +113,20 @@ export default defineConfig({ ``` - If both `build.config.ts` and a field of `package.json` hold options, move the options of both. List each external once. -- The build never bundles the packages in `dependencies`, `peerDependencies`, and `optionalDependencies`, their subpaths, Node builtins, `@nuxt/kit`, `@nuxt/schema`, `nuxt`, `vue`, `#app`, `#build/*`, and `#imports`. Drop the externals that are among these, and those that no bundled entry imports. Keep the rest in `builder({ externals })`. +- The build never bundles the packages in `dependencies`, `peerDependencies`, and `optionalDependencies`, their subpaths, Node builtins, `@nuxt/kit`, `@nuxt/schema`, `nuxt`, `vue`, `#app`, `#build/*`, and `#imports`. Drop the externals that are among these, and those that no bundled entry imports by value. The build never bundles a type-only import. Keep the rest in `builder({ externals })`. - The build fails when it bundles any other package from `node_modules`, `devDependencies` included. See [Bundled packages](#bundled-packages). - `src/module` is always built, so you can leave it out of `entries`. Drop `addRelativeDeclarationExtensions` from directory entries. - Relative imports in `tsdown.config.ts` carry their extension, because `prepare` loads the file with Node. -Then delete `build.config.ts`, and remove the `build` or `unbuild` field. With a `tsdown.config.ts`, the build fails while either of them is still there. [Configuration](./4.configuration.md) describes every option. +Then delete `build.config.ts`, and remove the `build` or `unbuild` field. With a `tsdown.config.ts`, the build fails while either of them is still there. ### Step 3: Update the paths in `package.json` v2 emits ES modules only, and writes the module entry's declarations as `.d.mts` files. In `exports`, `main`, `types`, and `typesVersions`: -- Replace `.d.ts` with `.d.mts` in the paths of `types.d.ts` and `module.d.ts`. -- Replace `module.cjs` with `module.mjs`, and remove the other `.cjs` paths and `require` conditions. +- Replace `.d.ts` with `.d.mts` in the paths of `types.d.ts`, `module.d.ts`, and the declarations of each other bundled entry, such as `utils.d.ts`. Other `.d.ts` paths stay as they are. +- Replace `module.cjs` with `module.mjs`, and remove the other `.cjs` paths and `require` conditions. Keep the other conditions, such as `default`. +- Give the root export a `types` condition, `./dist/types.d.mts`, as below. A root export that is a string becomes an object. ```json [package.json] { @@ -132,18 +140,17 @@ v2 emits ES modules only, and writes the module entry's declarations as `.d.mts` } ``` -Then decide whether your module supports `node10` consumers: projects that import it and type check with `"moduleResolution": "node"` or `"node10"`, or with `"module": "commonjs"` and no `moduleResolution`. They read `types` and `typesVersions` instead of `exports`. An app on Nuxt 4 isn't one. +Unless your module's maintainers know of a project that imports the module and type checks with `"moduleResolution": "node"`, remove the top-level `types` field and `typesVersions`. Such a `node10` consumer reads them instead of `exports`, and a Nuxt app isn't one. -- If your module doesn't support them, remove the top-level `types` field and `typesVersions`. Subpath exports then don't resolve under `node10`, which step 10 leaves out of its check. -- If it does, set `types` to `./dist/types.d.mts`. Write every `typesVersions` path with its full file name, such as `./dist/utils.d.mts` rather than `dist/utils`, because `node10` doesn't look for `.d.mts` files. A wildcard such as `"*": ["./dist/*"]` also maps the path in `types`, so check the root import under `node10` too. +If you keep them, set `types` to `./dist/types.d.mts`. In `typesVersions`, write each key without `./`, such as `"runtime"` rather than `"./runtime"`, and each path with its full file name, such as `./dist/utils.d.mts` rather than `dist/utils`. A wildcard such as `"*": ["./dist/*"]` also maps the path in `types`, so check the root import under `node10` too. After each build, a warning lists any other path in these fields that the build didn't produce. Add the entry that produces it in step 2, or remove the path. ### Step 4: Make module code load with Node -`nuxt-module-build prepare` and the build load module code with Node, as Nuxt 5 does. Module code is `src/module.ts`, every file that it imports by value (runtime files included), and `tsdown.config.ts`. With the source exports of step 6, it also includes each bundled entry that `exports` or `imports` points at, and the files that it imports by value. In these files: +`nuxt-module-build prepare` and the build load module code with Node, as Nuxt 5 does. Module code is `src/module.ts`, every file that it imports by value (runtime files included), and `tsdown.config.ts`. With the source exports of step 6, it also includes each bundled entry that `exports` or `imports` points at, and the files that it imports by value. `prepare` lists each import and each piece of syntax in these files that Node can't load. Change only what it lists: -- Add the file's extension to every relative import, type-only imports included: `./utils` becomes `./utils.ts`, and `./dir` becomes `./dir/index.ts`. +- Add the file's extension to a relative import: `./utils` becomes `./utils.ts`, and `./dir` becomes `./dir/index.ts`. - Replace imports through tsconfig `paths` aliases with relative imports, or with entries in the `imports` field of `package.json`. - Rewrite `enum`, `namespace` with values, parameter properties, `value` assertions, `import x = require()`, `export =`, and decorators. - Import JSON with an import attribute, and read properties from its default export. @@ -162,7 +169,7 @@ import { resolveOptions } from './options.ts' import { addComponents } from './components/index.ts' ``` -[Module code](./5.typescript.md#module-code) shows how to rewrite each of these. +[Module code](./5.typescript.md#module-code) shows how to rewrite each of these. Extensions on type-only relative imports are optional. Only the playground's type check requires them, with source exports, on Nuxt 5 or with `future.compatibilityVersion: 5`. Before you run `prepare`: @@ -173,7 +180,7 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod ### Step 5: Switch to the module's own tsconfigs -`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Remove its `extends` and `include`, and keep its `compilerOptions` and `exclude`: +`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Remove its `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example: ```json [tsconfig.json] { @@ -188,29 +195,33 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod } ``` -`prepare` sets `erasableSyntaxOnly` in the generated tsconfig for module code, so the type check reports the syntax that Node can't run. Then adjust the setup to your module: +`prepare` sets `erasableSyntaxOnly` for module code, so the type check reports the syntax that Node can't run. Adjust the setup to your module: -- `prepare` copies the `compilerOptions` of `tsconfig.json` into every part. Move `types` that only tests use, such as `"vitest/globals"`, into a `tsconfig.test.json` (see below). For options of module code only, see [Options of one part](./5.typescript.md#options-of-one-part). +- `prepare` copies the `compilerOptions` of `tsconfig.json` into every part. Move `types` that only tests use, such as `"vitest/globals"`, out of them. For options of module code only, see [Options of one part](./5.typescript.md#options-of-one-part). - `prepare` doesn't copy `paths` or `baseUrl`. Move each alias from `paths` into the `imports` field of `package.json`. - The generated tsconfigs cover `src` and the module's `.d.ts` files. Move a `.d.ts` that is in an excluded directory, such as `test/`, into `src`. -- Tests outside `test/nuxt`, and TypeScript config files in the module root such as `tsdown.config.ts`, are in no part. To type check them, add a [`tsconfig.test.json`](./5.typescript.md#tests-and-config-files) to `references`. Leave out of its `include` what your old `exclude` left out, such as `test`. -- If your module is in a monorepo, put `tsconfig.json` in the module's directory. Copy into its `compilerOptions` the entries of the repository root's `types` that `src` relies on. +- If your module is in a monorepo, put `tsconfig.json` in the module's directory. Copy into its `compilerOptions` the repository root's `types`, apart from those of a test runner, such as `"vitest/globals"`. + +Tests outside `test/nuxt`, and TypeScript config files in the module root such as `tsdown.config.ts`, are in no part. Your old type check covered a file if a script ran `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` on `tsconfig.json`, and that tsconfig neither listed the file in `exclude` nor left it out of an `include`. + +- If it covered tests or config files, add the [`tsconfig.test.json`](./5.typescript.md#tests-and-config-files) of that page to `references`. Add to its `include` the other directories that the old check covered, such as `./scripts`, and to its `exclude` what the old `exclude` left out. Add the types that only the tests bring in, as that page shows. +- Otherwise, add them to the `exclude` of `tsconfig.json`: the directories and files that `prepare` lists in `NUXT_MB_C0303` below. Then change the type check: - If `vue-tsc` isn't in `devDependencies`, run `pnpm add -D --ignore-workspace-root-check vue-tsc`. -- In the type-check script, replace the module's own `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` without a directory, with `vue-tsc -b --noEmit`. Keep the rest of the script: `vue-tsc --noEmit && nuxt typecheck playground` becomes `vue-tsc -b --noEmit && nuxt typecheck playground`. -- If your module has no type-check script, add `"test:types": "vue-tsc -b --noEmit"`. +- In each script that runs `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` without a directory, replace that command with `vue-tsc -b --noEmit`. Keep the rest of the script: `vitest run && vue-tsc --noEmit` becomes `vitest run && vue-tsc -b --noEmit`. +- If no script runs one of these commands, add `"test:types": "vue-tsc -b --noEmit"`. -Run `pnpm exec nuxt-module-build prepare`, then `pnpm exec vue-tsc -b --noEmit`. Don't run the type-check script yet, as it can still run a stub build that step 7 removes. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) lists the errors that this can report, with their fixes. +Run `pnpm exec nuxt-module-build prepare`. If you add what it lists to `exclude`, run it again. Then run `pnpm exec vue-tsc -b --noEmit`. Don't run the type-check script yet, as it can still run a stub build that step 7 removes. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) lists the errors that this can report, with their fixes. To keep a single `tsconfig.json` that extends `./.nuxt/tsconfig.json` instead, see [Extending .nuxt/tsconfig.json](./5.typescript.md#extending-nuxttsconfigjson). ### Step 6: Choose how the playground loads the module -The playground installs the module by its package name, so it loads whatever `package.json` points at. In v1, a stub build made `dist` load your sources. In v2, the package manager that publishes the module decides the setup. +The playground installs the module by its package name, so it loads whatever `package.json` points at. The package manager that publishes the module decides what that is. -If you publish with pnpm or Yarn 4, point `exports`, `imports`, and `main` at your sources, and the same fields in `publishConfig` at `dist`. Both publish the fields of `publishConfig` in place of the top-level ones: +If you publish with pnpm or Yarn 4, point `exports`, `imports`, `main`, and `module` at your sources, and the same fields in `publishConfig` at `dist`. Both publish the fields of `publishConfig` in place of the top-level ones: ```json [package.json] { @@ -230,29 +241,30 @@ If you publish with pnpm or Yarn 4, point `exports`, `imports`, and `main` at yo ``` - `publishConfig.exports` and `publishConfig.imports` replace the top-level fields as a whole, so repeat every subpath and condition. -- The root export's `types` condition points at `.nuxt/module-builder/types.d.mts`, which `prepare` writes. A subpath points at its source. +- The root export's `types` condition points at `.nuxt/module-builder/types.d.mts`, which `prepare` writes. So does an `imports` entry for `./dist/types.d.mts`. +- Point each other subpath at its source, unless Node or a tool that can't read TypeScript loads it, such as a plain Vite app's `vite.config.ts`, an ESLint config, or a CLI. Keep such a subpath at `dist`. When nothing in the repository shows what loads a subpath, point it at its source. +- [Which paths point at sources](./7.exports.md#which-paths-point-at-sources) shows how to write wildcard subpaths, subpaths with only `types`, and fields such as `style`. - If you kept `types` in step 3, move it into `publishConfig`. Keep `typesVersions` at the top level, pointing at `dist`. -- If code outside Nuxt loads a subpath with Node, keep that subpath pointing at `dist`. A Vite plugin for a plain Vite app is one example. Step 4 doesn't check its source. Run `tsdown` before anything loads that subpath, as in step 7. -- Point [code that reaches into `dist`](./3.develop-and-publish.md#code-that-reaches-into-dist) by path, such as a test that imports `../dist/runtime/...`, at `src`. +- Point [code that reaches into `dist`](./3.develop-and-publish.md#code-that-reaches-into-dist) by path, such as a test that imports `../dist/runtime/...`, at `src`. Code that reads a built file's contents keeps reading `dist`. 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. +If the playground lists the module by name, and `playground/node_modules` doesn't link the module directory yet, 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: +In each script that runs the builder, change only the builder's commands, and keep every other command where it is. That covers every script and CI workflow that runs the builder: -- `nuxt-module-build build` becomes `tsdown`. `tsdown` takes no module directory, so `nuxt-module-build build .` becomes `tsdown` too. `nuxt-module-build` without a subcommand only prints its usage, so it becomes `tsdown` as well. +- `nuxt-module-build build`, `nuxt-module-build build .`, and `nuxt-module-build` without a subcommand become `tsdown`, which takes no module directory. - For a module in another directory, run `cd packages/my-module && tsdown`, or keep `nuxt-module-build build packages/my-module`. -- `nuxt-module-build build --stub`: if `package.json` points at your sources, remove it. If it points at `dist`, replace it with `tsdown`. +- `nuxt-module-build build --stub`: if `package.json` points at your sources, remove it. If it points at `dist`, replace it with `tsdown`, after `nuxt-module-build prepare`. - `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. +- 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 &&`. A script that runs other commands too keeps them. +- In `dev:prepare`, run `nuxt-module-build prepare` first, before the build and every `nuxt prepare`. Add it if it isn't there. It's the one command that moves. +- In each script that runs `nuxt dev` on a playground, run `nuxt-module-build prepare` before it. Without `.nuxt`, the playground responds to every request with a 500 error. A script that runs another package's script, such as `pnpm -C playground-v5 dev`, stays as it is. +- If `package.json` points at `dist`, or a subpath stays at `dist` and a command in `dev:prepare` or the tests load it, run `tsdown` in `dev:prepare` right after `nuxt-module-build prepare`. - 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`. +- If `package.json` points at your sources, run `tsdown` as a command of `prepack` itself, so that the [pack checks](#publishing-source-exports) run. Add a `prepack` if the module's `package.json` has none, as in a monorepo package that the root builds. A `prepack` that runs another script, such as `pnpm run build`, isn't checked: move that script's commands into `prepack`, and make the other script run `prepack`, as `"build": "pnpm run prepack"`. For example, these v1 scripts also minify a file into `dist` and prepare a docs app: @@ -265,7 +277,7 @@ For example, these v1 scripts also minify a file into `dist` and prepare a docs } ``` -If `package.json` points at your sources, they become: +If `package.json` points at your sources, and nothing in `dev:prepare` loads `dist`, they become: ```json [package.json] { @@ -276,7 +288,7 @@ If `package.json` points at your sources, they become: } ``` -If `package.json` points at `dist`, `dev:prepare` builds `dist` after `prepare` instead: `nuxt-module-build prepare && tsdown && nuxt prepare playground && nuxt prepare docs`. +Otherwise, `dev:prepare` builds `dist` after `prepare`: `nuxt-module-build prepare && tsdown && nuxt prepare playground && nuxt prepare docs`. ### Step 8: Build @@ -296,11 +308,11 @@ Run your module's type-check script, and fix what it reports. [Errors from check 1. Run `pnpm pack`. It writes a tarball, such as `my-module-1.0.0.tgz`. 2. Run `pnpm dlx publint my-module-1.0.0.tgz`. -3. List the imports in its declarations that don't resolve from the package alone. Write the report to a file first, as attw can exit before a pipe reads all of it: +3. List the imports in its declarations that don't resolve from the package alone, with the number of files that each appears in. Write the report to a file first, as attw can exit before a pipe reads all of it. The first command exits with 1 whenever the report lists a problem: ```bash pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --format json > attw.json - jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.resolutionOption) \(.moduleSpecifier) \(.fileName)"' attw.json | sort -u + jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c ``` Check that each one is an alias that only a Nuxt app provides, such as `#app`, `#imports`, or `#build/my-module`. Aliases that your module, or a module it installs, adds to the app count too. Fix any other. @@ -335,14 +347,14 @@ Each message that requires a change starts with a code, such as `NUXT_MB_D0001`, - Add `plugins: [builder()]` to the config, as in [step 2](#step-2-move-the-build-options-into-tsdownconfigts). `tsdown` and `nuxt-module-build build` fail with a tsdown config that doesn't use it. - Call `builder()` in the config that builds the module. One `builder()` plugin that two configs share fails the build, and so does an array with `builder()` in more than one config. -- `builder()` takes `entries`, `externals`, and `replace`. Set every other option on the tsdown config itself. +- `builder()` takes `entries`, `externals`, `replace`, and `declarations`. Set every other option on the tsdown config itself. - `prepare` reads the module in the directory of the tsdown config, so the config can't set `cwd` to another directory. See [Configuration](./4.configuration.md). ### Options `builder()` sets -`builder()` sets some tsdown options itself, such as `entry`, `format`, `dts`, and `clean`. Remove them from the tsdown config. The message names each one with its replacement: for example, list entries in `builder({ entries })`, and externals in `builder({ externals })` or `deps.neverBundle`. +`builder()` sets some tsdown options itself, such as `entry`, `format`, `dts`, and `clean`. Remove them from the tsdown config. The message names the replacement of each, such as `builder({ entries })` for `entry`. The build also fails when another plugin changes one of these options, or the file names of the bundled entries through `outputOptions.entryFileNames`. Configure that plugin to leave them unchanged, or remove it. [Options that `builder()` sets](./4.configuration.md#options-that-builder-sets) lists them all. @@ -366,7 +378,7 @@ See [Externals](./4.configuration.md#externals). ### Output directory -The build empties the output directory before it writes to it, and `tsdown --watch` removes the files that it doesn't emit any more. So `outDir` and `--outDir` have to name a directory inside the module, such as `dist`. A directory outside the module, such as `../dist`, or the module root itself, fails the build. +The build empties the output directory, and `tsdown --watch` removes the files that it no longer emits. So `outDir` and `--outDir` have to name a directory inside the module, such as `dist`, other than the module root. ### Stub builds @@ -410,40 +422,13 @@ When `prepare` reports this, as a warning, run `nuxt-module-build prepare` again ### Isolated declarations -With `isolatedDeclarations: true` in the tsconfig that includes `src/module.ts`, the build generates the module entry's declarations with oxc. This is faster than TypeScript, but requires an explicit type on every export. TypeScript only accepts `isolatedDeclarations` with `declaration` and without `allowJs`, which the generated tsconfigs set, so set all three in a `tsconfig.module.json` that you reference in place of `./.nuxt/tsconfig.node.json`: - -```json [tsconfig.module.json] -{ - "extends": "./.nuxt/tsconfig.node.json", - "compilerOptions": { - "tsBuildInfoFile": "./.nuxt/tsconfig.module.tsbuildinfo", - "allowJs": false, - "declaration": true, - "isolatedDeclarations": true - } -} -``` - -`vue-tsc -b --noEmit` then reports every export without an explicit type in the files that module code imports, runtime files included, also through `import type`. A `.js` or `.mjs` file that module code imports needs a declaration file next to it, such as `utils.d.mts` for `utils.mjs`. Write the module entry as follows: - -```ts [src/module.ts] -import type { NuxtModule } from '@nuxt/schema' - -const module: NuxtModule = defineNuxtModule({ /* ... */ }) - -export default module -``` - -Leaving it off is fine: the build then generates these declarations with TypeScript, alongside the runtime files, and reuses them while their inputs are unchanged. See [`isolatedDeclarations`](./5.typescript.md#isolateddeclarations). +With `declarations.isolated: true`, or `isolatedDeclarations: true` in the tsconfig that includes `src/module.ts`, the build generates the module entry's declarations with oxc, which requires an explicit type on every export of module code. Give each export that the message lists a type, as [`isolatedDeclarations`](./5.typescript.md#isolateddeclarations) shows, or remove the option. Without it, the build generates these declarations with TypeScript. ### Publishing source exports -Every build reads the path fields of `package.json`, with `publishConfig` applied. It fails when one of them points at a TypeScript source, or at a file that `files` leaves out. Point the field at `dist`, in `publishConfig` if `package.json` points at your sources. To publish a file other than a TypeScript source as it is, list it in `files`. - -When `tsdown` runs as the `prepack` or `prepublishOnly` script, the build also checks the package manager that packs. It has to apply `publishConfig` to each of these fields: +Every build reads the path fields of `package.json`, with `publishConfig` applied. It fails when one of them points at a TypeScript source, or at a file that `files` leaves out. Point the field at `dist`, in `publishConfig` if `package.json` points at your sources. To publish another file as it is, list it in `files`. -- Pack and publish with pnpm or Yarn 4, as `pnpm publish` or `yarn npm publish`. A script that runs `npm publish` fails, even when `pnpm run` starts it. -- Set each field that points at your sources in `publishConfig`, apart from `typesVersions`, which points at `dist`. +When `tsdown` runs as the `prepack` or `prepublishOnly` script, the package manager that packs has to apply `publishConfig` to each field that points at your sources. Publish with `pnpm publish` or `yarn npm publish`, not `npm publish`, and set each of those fields in `publishConfig`. See [Checks on published paths](./3.develop-and-publish.md#checks-on-published-paths) and [Checks when you pack](./3.develop-and-publish.md#checks-when-you-pack). @@ -461,7 +446,7 @@ Remove `paths` from that tsconfig, whether it is `tsconfig.json` extending `./.n Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no tsconfig in `references` includes, with the number of files in each directory. `vue-tsc -b` doesn't type check them. These are usually tests outside `test/nuxt`, fixtures, and config files in the module root. -Reference a `tsconfig.test.json` that includes them, as in [Tests and config files](./5.typescript.md#tests-and-config-files), or add them to the `exclude` of `tsconfig.json`. +To type check them, reference a `tsconfig.test.json` that includes them, as in [Tests and config files](./5.typescript.md#tests-and-config-files). Otherwise, add them to the `exclude` of `tsconfig.json`. [Step 5](#step-5-switch-to-the-modules-own-tsconfigs) says which to choose when you upgrade. ### `build.config.ts` and the `build` field @@ -474,7 +459,7 @@ Until then: ### Unsupported build options -Without a `tsdown.config.ts`, the build reads `build.config.ts` and the `build` field of `package.json`. It fails with a list of every option it can't apply, and the file that each comes from. [Step 2](#step-2-move-the-build-options-into-tsdownconfigts) moves the options that v2 supports into `tsdown.config.ts`. To keep `build.config.ts` for now, remove each listed option instead. +Without a `tsdown.config.ts`, the build reads `build.config.ts` and the `build` field of `package.json`, and fails with a list of every option it can't apply. [Step 2](#step-2-move-the-build-options-into-tsdownconfigts) moves the options that v2 supports into `tsdown.config.ts`. To keep `build.config.ts` for now, remove each listed option instead. #### Hooks @@ -505,7 +490,7 @@ These options fail the build too: ## Other changes -These changes need no action in most modules. Check them if a runtime file behaves differently after the upgrade: +Check these changes if a runtime file behaves differently after the upgrade: - Runtime files are transpiled one at a time with oxc. Type-only imports and TypeScript directive comments are removed. See [Runtime files](./6.output.md#runtime-files). - `.tsx` and `.jsx` runtime files keep their JSX for the app to compile, unless they run in Nitro or Node. See [JSX](./6.output.md#jsx). From 60176a518142f1ef3a95f3b3ebf8224e3c28af7f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Nov 2026 01:46:36 +0000 Subject: [PATCH 04/19] docs: cover node10 self-imports, attw --pack, generic exports and node-loaded subpaths --- docs/3.develop-and-publish.md | 16 ++++++++++++---- docs/5.typescript.md | 10 ++++++++++ docs/7.exports.md | 14 +++++++++++--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index eb405c5b..ac53c857 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -68,7 +68,7 @@ The playground has to resolve the module's name to the module directory itself, 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. -Keep `typesVersions` at the top level, pointing at `dist`, with pnpm and with Yarn 4. TypeScript only reads it for a package whose `exports` it doesn't read, as under `node10` module resolution, which a playground doesn't use. pnpm also applies `publishConfig.typesVersions`, but Yarn 4 packs `typesVersions` as written. +A module that keeps `typesVersions` for `node10` consumers leaves it at the top level, pointing at `dist`, with pnpm and with Yarn 4. TypeScript only reads it for a package whose `exports` it doesn't read, as under `node10` module resolution, which a playground doesn't use. pnpm also applies `publishConfig.typesVersions`, but Yarn 4 packs `typesVersions` as written. #### Code that reaches into `dist` @@ -249,10 +249,10 @@ To check the package before you publish it, follow these steps: ```bash [Terminal] npx @arethetypeswrong/cli ./my-module-1.0.0.tgz --profile esm-only --format json > attw.json - jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c + jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError" and .resolutionOption != "node10") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c ``` - Each one has to be an alias that only a Nuxt app provides, such as `#app`, `#imports`, `#build/my-module/options`, or an alias that the module adds to the app, or, under `node10` for a module without `node10` consumers, one of the package's own `imports`. A module that supports `node10` consumers imports those files by relative path in the code that the declarations come from. Fix any other, such as a relative import of a file that the package doesn't contain. An app's `nuxt.config.ts` context reads the module's options type without the app's aliases, and types an alias that doesn't resolve as `any`, so move a type that the options type takes from such a file into one that doesn't import the aliases. + Each one has to be an alias that only a Nuxt app provides, such as `#app`, `#imports`, `#build/my-module/options`, or an alias that the module adds to the app. The command leaves out `node10`, which the `esm-only` profile doesn't check either. Under `node10`, a module without `typesVersions` also can't resolve its own `imports` and subpaths, such as `my-module/utils` in `dist/runtime/types.d.ts`. A module that supports `node10` consumers imports those files by relative path in the code that the declarations come from. Fix any other, such as a relative import of a file that the package doesn't contain. An app's `nuxt.config.ts` context reads the module's options type without the app's aliases, and types an alias that doesn't resolve as `any`, so move a type that the options type takes from such a file into one that doesn't import the aliases. 4. Run Are the Types Wrong? with those imports ignored: @@ -268,7 +268,15 @@ For a module that is correct, Are the Types Wrong? reports three kinds of proble - `NoResolution`, under `node10`, for each subpath export of a module without `typesVersions`. - `InternalResolutionError`, for an import in a declaration file that only a Nuxt app resolves. It appears under every resolution for aliases such as `#imports` and `#build/*`, and under `node10` for the package's own `imports`. The `esm-only` profile doesn't leave it out, so step 4 ignores the rule. -`attw --pack .` packs with npm, which fails on a package with `devEngines`, so pack the module with its own package manager first. +`attw --pack .` packs with npm, which fails on a package with `devEngines`, and on source exports with `NUXT_MB_C0104`. Pack the module with its own package manager first, as in this script: + +```json [package.json] +{ + "scripts": { + "test:attw": "pnpm pack --out node_modules/.cache/attw.tgz && attw node_modules/.cache/attw.tgz --profile esm-only --ignore-rules internal-resolution-error" + } +} +``` tsdown's own `attw` option packs the module with its package manager and checks the tarball after each build. It needs `@arethetypeswrong/core` in the module's `devDependencies`: diff --git a/docs/5.typescript.md b/docs/5.typescript.md index 696ac0f8..be80c1d2 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -341,6 +341,16 @@ export const handler = defineCachedHandler(...) export const handler: ReturnType = defineCachedHandler(...) ``` +When the export is a function, generic or not, annotate its return type rather than the variable, so it keeps its own type parameters: + +```ts +// before +export const defineMyHandler = (handler: T) => defineCachedHandler(handler) + +// after +export const defineMyHandler = (handler: T): ReturnType => defineCachedHandler(handler) +``` + Otherwise, import a type from a package that your module depends on directly: ```ts diff --git a/docs/7.exports.md b/docs/7.exports.md index 1c7c86f6..ab4b6680 100644 --- a/docs/7.exports.md +++ b/docs/7.exports.md @@ -185,15 +185,23 @@ A subpath points at its source when only Nuxt loads it: an app, through the modu Each path of `exports` mirrors its path in `publishConfig.exports`: -- **A wildcard subpath** with an extension in its `dist` target gets the source's extension: `"./runtime/*": "./dist/runtime/*.js"` becomes `"./src/runtime/*.ts"`, for its `types` condition too. TypeScript doesn't swap the extension of an `exports` target. -- **A wildcard subpath for `.vue` files**, whose `dist` target keeps the extension of the import, such as `"./dist/runtime/components/*"`, points at `"./src/runtime/components/*"`, for its `types` condition too. -- **A wildcard subpath for every file in `dist`**, such as `"./*": "./dist/*"`, stays at `dist`, as imports of it name the files in `dist`. +- **A wildcard subpath with an extension in its `dist` target** gets the source's extension: `"./runtime/*": "./dist/runtime/*.js"` becomes `"./src/runtime/*.ts"`, for its `types` condition too. TypeScript doesn't swap the extension of an `exports` target. +- **A wildcard subpath without an extension in its `dist` target**, such as `"./dist/runtime/components/*"`, takes the import's own extension. It points at the same path under `src` when every file it reaches keeps its name in `dist`, such as `.vue` and `.css` files, for its `types` condition too. When it also reaches scripts, which `dist` names `.js`, such as `"./*": "./dist/*"` or a `"./runtime/*"` for components and composables, it stays at `dist`. +- **A subpath written as a string** stays a string: `"./registry": "./dist/registry.mjs"` becomes `"./registry": "./src/registry.ts"`. - **A subpath with only a `types` condition** that points at a file outside `dist`, such as `./client.d.ts`, stays as it is, in both fields. - **A subpath for the module entry**, such as `"./module"`, points where the root export does. - **An entry of `imports`** points at the source of its target. `./dist/types.d.mts` has none: point it at `./.nuxt/module-builder/types.d.mts`, as the root export's `types` condition does. - **`main` and `module`** point at `./src/module.ts`, with the `dist` paths in `publishConfig`. - **Other fields, such as `style`,** stay at `dist`. pnpm and Yarn 4 don't apply `publishConfig` to them. A `style` condition in `exports` follows `exports`, and points at the source stylesheet. +### Subpaths that Node loads from source + +`prepare` checks module code, not the other entries that a subpath points at. When Node loads such a subpath from source, as a `vite.config.ts` or a script does, the entry and the files it imports follow the [module code rules](/docs/api/module-builder/typescript#module-code), such as JSON imports with an import attribute. To check one, import it with Node from the module root: + +```bash [Terminal] +node --input-type=module -e "await import('my-module/utils')" +``` + ## Importing your own exports Runtime code and module code can import the module's own package name, such as `my-module/utils`. The build never bundles these imports, so they resolve to the published files in the app that installs the module. From d7cb43be6d60c684f7b34bb2754d3aac0745eae8 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Nov 2026 01:46:36 +0000 Subject: [PATCH 05/19] docs: make the upgrade defaults instructions and run the type check after the build --- docs/upgrade-v2.md | 74 +++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 1f3616ee..cec2cff6 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -14,14 +14,13 @@ v2 builds your module with [tsdown](https://tsdown.dev) instead of unbuild. The - Stub builds are removed. With pnpm or Yarn 4, `package.json` points at your sources while you develop. With other package managers, `tsdown --watch` rebuilds `dist`. - The output is ES modules only, with `.d.mts` declarations for the module entry and no `module.cjs`. - `nuxt-module-build prepare` generates a tsconfig for each part of the module, and `vue-tsc -b` checks each part with the types it has in an app. -- The build doesn't type check, so your own type check runs as a separate step. ## Before you start - Use Node `^22.18.0 || ^24.11.0 || >=26.0.0`, locally and in CI. - Find out which package manager packs your module when you publish it: the one whose `pack` or `publish` command your release script or workflow runs. A release tool, such as release-it, uses npm unless its config or docs name another. Step 6 depends on it. -- The steps use `pnpm`. Use your module's own package manager instead. Run commands with its `exec`, such as `pnpm exec tsdown`, and scripts with its `run`, such as `pnpm run dev:prepare`. With Yarn 1, write `yarn add -D` for `pnpm add -D`, and `yarn pack --filename my-module-1.0.0.tgz` for `pnpm pack`. -- The steps write the builder's command as `nuxt-module-build`. They apply to its other names too: `nuxt-build-module`, and `nuxt build-module` or `nuxt module-build`, with `nuxt` or `nuxi`. +- The steps use `pnpm`. Use your module's own package manager instead. Run commands with its `exec`, such as `pnpm exec tsdown`, and scripts with its `run`, such as `pnpm run dev:prepare`. With Yarn 1, write `yarn add -D`, `yarn remove`, `yarn tsdown`, and `npx publint` for `pnpm add -D`, `pnpm remove`, `pnpm exec tsdown`, and `pnpm dlx publint`, and `yarn pack --filename my-module-1.0.0.tgz` for `pnpm pack`. +- The steps write the builder's command as `nuxt-module-build`, and apply to its other names, such as `nuxt-build-module` and `nuxi module-build`. ## Steps @@ -115,7 +114,7 @@ export default defineConfig({ - If both `build.config.ts` and a field of `package.json` hold options, move the options of both. List each external once. - The build never bundles the packages in `dependencies`, `peerDependencies`, and `optionalDependencies`, their subpaths, Node builtins, `@nuxt/kit`, `@nuxt/schema`, `nuxt`, `vue`, `#app`, `#build/*`, and `#imports`. Drop the externals that are among these, and those that no bundled entry imports by value. The build never bundles a type-only import. Keep the rest in `builder({ externals })`. - The build fails when it bundles any other package from `node_modules`, `devDependencies` included. See [Bundled packages](#bundled-packages). -- `src/module` is always built, so you can leave it out of `entries`. Drop `addRelativeDeclarationExtensions` from directory entries. +- `src/module` is always built: leave it out of `entries`. Drop `addRelativeDeclarationExtensions` from directory entries. With no externals left, leave out `externals`. - Relative imports in `tsdown.config.ts` carry their extension, because `prepare` loads the file with Node. Then delete `build.config.ts`, and remove the `build` or `unbuild` field. With a `tsdown.config.ts`, the build fails while either of them is still there. @@ -125,8 +124,8 @@ Then delete `build.config.ts`, and remove the `build` or `unbuild` field. With a v2 emits ES modules only, and writes the module entry's declarations as `.d.mts` files. In `exports`, `main`, `types`, and `typesVersions`: - Replace `.d.ts` with `.d.mts` in the paths of `types.d.ts`, `module.d.ts`, and the declarations of each other bundled entry, such as `utils.d.ts`. Other `.d.ts` paths stay as they are. -- Replace `module.cjs` with `module.mjs`, and remove the other `.cjs` paths and `require` conditions. Keep the other conditions, such as `default`. -- Give the root export a `types` condition, `./dist/types.d.mts`, as below. A root export that is a string becomes an object. +- Replace `module.cjs`, or a name that a removed `rollup:done` hook gave it such as `module.js`, with `module.mjs`. Remove the other `.cjs` paths and `require` conditions. Keep the other conditions, such as `default`. +- Point the root export's `types` condition at `./dist/types.d.mts`, in place of any other path such as `./dist/module.d.mts`, as below. A root export that is a string becomes an object. ```json [package.json] { @@ -140,9 +139,9 @@ v2 emits ES modules only, and writes the module entry's declarations as `.d.mts` } ``` -Unless your module's maintainers know of a project that imports the module and type checks with `"moduleResolution": "node"`, remove the top-level `types` field and `typesVersions`. Such a `node10` consumer reads them instead of `exports`, and a Nuxt app isn't one. +Remove the top-level `types` field and `typesVersions`. Only projects that type check with `"moduleResolution": "node"` (`node10`) read them in place of `exports`, and a Nuxt app isn't one. attw then reports `NoResolution` under `node10` for each subpath, and an `InternalResolutionError` under `node10` for each import of the package's own subpaths in its declarations. Step 10 leaves both out. -If you keep them, set `types` to `./dist/types.d.mts`. In `typesVersions`, write each key without `./`, such as `"runtime"` rather than `"./runtime"`, and each path with its full file name, such as `./dist/utils.d.mts` rather than `dist/utils`. A wildcard such as `"*": ["./dist/*"]` also maps the path in `types`, so check the root import under `node10` too. +If your module's maintainers know of a `node10` consumer, keep them instead. Set `types` to `./dist/types.d.mts`. In `typesVersions`, write each key without `./`, such as `"runtime"` rather than `"./runtime"`, and each path with its full file name, such as `./dist/utils.d.mts` rather than `dist/utils`. After each build, a warning lists any other path in these fields that the build didn't produce. Add the entry that produces it in step 2, or remove the path. @@ -169,7 +168,7 @@ import { resolveOptions } from './options.ts' import { addComponents } from './components/index.ts' ``` -[Module code](./5.typescript.md#module-code) shows how to rewrite each of these. Extensions on type-only relative imports are optional. Only the playground's type check requires them, with source exports, on Nuxt 5 or with `future.compatibilityVersion: 5`. +[Module code](./5.typescript.md#module-code) shows how to rewrite each of these. Leave type-only relative imports as they are, unless step 6 says otherwise. Before you run `prepare`: @@ -180,7 +179,7 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod ### Step 5: Switch to the module's own tsconfigs -`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Remove its `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example: +`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Remove its `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example, and add no `exclude` if it has none: ```json [tsconfig.json] { @@ -195,25 +194,27 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod } ``` -`prepare` sets `erasableSyntaxOnly` for module code, so the type check reports the syntax that Node can't run. Adjust the setup to your module: +Then adjust the setup to your module: - `prepare` copies the `compilerOptions` of `tsconfig.json` into every part. Move `types` that only tests use, such as `"vitest/globals"`, out of them. For options of module code only, see [Options of one part](./5.typescript.md#options-of-one-part). - `prepare` doesn't copy `paths` or `baseUrl`. Move each alias from `paths` into the `imports` field of `package.json`. - The generated tsconfigs cover `src` and the module's `.d.ts` files. Move a `.d.ts` that is in an excluded directory, such as `test/`, into `src`. - If your module is in a monorepo, put `tsconfig.json` in the module's directory. Copy into its `compilerOptions` the repository root's `types`, apart from those of a test runner, such as `"vitest/globals"`. -Tests outside `test/nuxt`, and TypeScript config files in the module root such as `tsdown.config.ts`, are in no part. Your old type check covered a file if a script ran `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` on `tsconfig.json`, and that tsconfig neither listed the file in `exclude` nor left it out of an `include`. +The old type check is the command in a script of the module's own `package.json` that type checked the module root: `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` (`nuxi typecheck`) without a directory. Commands that run in another directory, such as after `cd playground`, or on one, such as `nuxt typecheck playground`, aren't it. -- If it covered tests or config files, add the [`tsconfig.test.json`](./5.typescript.md#tests-and-config-files) of that page to `references`. Add to its `include` the other directories that the old check covered, such as `./scripts`, and to its `exclude` what the old `exclude` left out. Add the types that only the tests bring in, as that page shows. -- Otherwise, add them to the `exclude` of `tsconfig.json`: the directories and files that `prepare` lists in `NUXT_MB_C0303` below. +Then run `pnpm exec nuxt-module-build prepare`. Its `NUXT_MB_C0303` note lists the tests and config files that no part includes. The old type check covered a path if the old `tsconfig.json` included it: its `include`, or that of the tsconfig it extends, such as `.nuxt/tsconfig.json`, which includes the whole root, contains the path, and its `exclude` doesn't. With `"files": []` and no `include`, it included nothing. -Then change the type check: +- If the old check covered none of the listed paths, or there was no old check, add them all to the `exclude` of `tsconfig.json`. +- Otherwise, add a [`tsconfig.test.json`](./5.typescript.md#tests-and-config-files) to `references`. Its `include` lists `./.nuxt/nuxt.d.ts`, `./.nuxt/nuxt.node.d.ts`, and each covered path. Its `exclude` lists `./test/nuxt` and `./test/fixtures` if a covered path contains them, and the old `exclude` entries inside the covered paths. Its `files` lists each setup file that registers matchers or globals for the tests and that this `exclude` leaves out, such as `./test/nuxt/setup.ts`. Add the paths that the old check didn't cover to the `exclude` of `tsconfig.json`. + +Run `prepare` again. Then change the type check: - If `vue-tsc` isn't in `devDependencies`, run `pnpm add -D --ignore-workspace-root-check vue-tsc`. -- In each script that runs `vue-tsc --noEmit`, `tsc --noEmit`, or `nuxt typecheck` without a directory, replace that command with `vue-tsc -b --noEmit`. Keep the rest of the script: `vitest run && vue-tsc --noEmit` becomes `vitest run && vue-tsc -b --noEmit`. -- If no script runs one of these commands, add `"test:types": "vue-tsc -b --noEmit"`. +- Replace the old type check with `vue-tsc -b --noEmit`, and keep the rest of its script: `vitest run && vue-tsc --noEmit && cd playground && vue-tsc --noEmit` becomes `vitest run && vue-tsc -b --noEmit && cd playground && vue-tsc --noEmit`. +- If there is no old type check, add `"test:types": "vue-tsc -b --noEmit"`. -Run `pnpm exec nuxt-module-build prepare`. If you add what it lists to `exclude`, run it again. Then run `pnpm exec vue-tsc -b --noEmit`. Don't run the type-check script yet, as it can still run a stub build that step 7 removes. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) lists the errors that this can report, with their fixes. +Don't run the type check yet: step 9 runs it after the build, which writes the declarations of subpaths that point at `dist`. To keep a single `tsconfig.json` that extends `./.nuxt/tsconfig.json` instead, see [Extending .nuxt/tsconfig.json](./5.typescript.md#extending-nuxttsconfigjson). @@ -240,20 +241,21 @@ If you publish with pnpm or Yarn 4, point `exports`, `imports`, `main`, and `mod } ``` -- `publishConfig.exports` and `publishConfig.imports` replace the top-level fields as a whole, so repeat every subpath and condition. +- `publishConfig.exports` and `publishConfig.imports` replace the top-level fields as a whole, so repeat every subpath, entry, and condition, changed or not. A subpath written as a string stays a string. - The root export's `types` condition points at `.nuxt/module-builder/types.d.mts`, which `prepare` writes. So does an `imports` entry for `./dist/types.d.mts`. -- Point each other subpath at its source, unless Node or a tool that can't read TypeScript loads it, such as a plain Vite app's `vite.config.ts`, an ESLint config, or a CLI. Keep such a subpath at `dist`. When nothing in the repository shows what loads a subpath, point it at its source. +- Point each other subpath at its source, unless Node or a tool that can't read TypeScript loads it, such as a plain Vite app's `vite.config.ts`, an ESLint config, or a CLI. Keep such a subpath at `dist`. When nothing in the repository shows what loads a subpath, point it at its source, and check that [Node can load it](./7.exports.md#subpaths-that-node-loads-from-source). - [Which paths point at sources](./7.exports.md#which-paths-point-at-sources) shows how to write wildcard subpaths, subpaths with only `types`, and fields such as `style`. -- If you kept `types` in step 3, move it into `publishConfig`. Keep `typesVersions` at the top level, pointing at `dist`. -- Point [code that reaches into `dist`](./3.develop-and-publish.md#code-that-reaches-into-dist) by path, such as a test that imports `../dist/runtime/...`, at `src`. Code that reads a built file's contents keeps reading `dist`. +- If you kept `types` and `typesVersions` in step 3, move `types` into `publishConfig`, and leave `typesVersions` at the top level, pointing at `dist`. +- Change each import in tests and the playground that reaches into `dist`, such as `../dist/runtime/providers/ipx`, to the same path under `src`, without an extension: `../src/runtime/providers/ipx`. Code that reads a built file's contents keeps reading `dist`, as [Code that reaches into `dist`](./3.develop-and-publish.md#code-that-reaches-into-dist) shows. +- If the playground uses Nuxt 5 or sets `future.compatibilityVersion: 5`, its type check also needs extensions on the type-only relative imports that module code reaches. After step 8, run `vue-tsc -b --noEmit` in the playground, add the extension to each import that it reports with TS2835 or TS2834, and repeat until it reports none. 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, and `playground/node_modules` doesn't link the module directory yet, 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. +If the playground's `nuxt.config` lists the module by its package name, and `playground/node_modules` doesn't link the module directory yet, 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 every script and CI workflow that runs the builder: +In each script and CI workflow that runs the builder, change only the builder's commands, and keep every other command where it is: - `nuxt-module-build build`, `nuxt-module-build build .`, and `nuxt-module-build` without a subcommand become `tsdown`, which takes no module directory. - For a module in another directory, run `cd packages/my-module && tsdown`, or keep `nuxt-module-build build packages/my-module`. @@ -261,7 +263,8 @@ 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 &&`. A script that runs other commands too keeps them. - In `dev:prepare`, run `nuxt-module-build prepare` first, before the build and every `nuxt prepare`. Add it if it isn't there. It's the one command that moves. -- In each script that runs `nuxt dev` on a playground, run `nuxt-module-build prepare` before it. Without `.nuxt`, the playground responds to every request with a 500 error. A script that runs another package's script, such as `pnpm -C playground-v5 dev`, stays as it is. +- In each script of the module's `package.json` that runs `nuxt dev` or `nuxi dev` on an app, such as a playground, `docs`, or an example, run `nuxt-module-build prepare` before it, unless the script runs `dev:prepare` first. Without `.nuxt`, the app responds to every request with a 500 error. A script that runs another package's script, such as `pnpm -C playground-v5 dev`, and the scripts in other `package.json` files, stay as they are. +- If `package.json` points at your sources and a script runs `attw --pack`, which packs with npm and fails with `NUXT_MB_C0104`, pack with your package manager and run attw on the tarball, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows. - If `package.json` points at `dist`, or a subpath stays at `dist` and a command in `dev:prepare` or the tests load it, run `tsdown` in `dev:prepare` right after `nuxt-module-build prepare`. - 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. Add a `prepack` if the module's `package.json` has none, as in a monorepo package that the root builds. A `prepack` that runs another script, such as `pnpm run build`, isn't checked: move that script's commands into `prepack`, and make the other script run `prepack`, as `"build": "pnpm run prepack"`. @@ -312,10 +315,10 @@ Run your module's type-check script, and fix what it reports. [Errors from check ```bash pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --format json > attw.json - jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c + jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError" and .resolutionOption != "node10") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c ``` - Check that each one is an alias that only a Nuxt app provides, such as `#app`, `#imports`, or `#build/my-module`. Aliases that your module, or a module it installs, adds to the app count too. Fix any other. + Check that each one is an alias that only a Nuxt app provides, such as `#app`, `#imports`, or `#build/my-module`. Aliases that your module, or a module it installs, adds to the app count too. Fix any other. The command leaves out `node10`, as the `esm-only` profile does. 4. Run attw with those imports ignored: @@ -335,7 +338,7 @@ Run your module's type-check script, and fix what it reports. [Errors from check If you vendor types, check the affected imports again in a separate TypeScript config with `skipLibCheck: false`. Extend the relevant generated Nuxt config, such as `.nuxt/tsconfig.app.json`, to keep aliases like `#app`. Look for errors in your package's declarations. -[Checking the package](./3.develop-and-publish.md#checking-the-package) explains what attw reports for a module that is correct, and how to run step 4 on every build with tsdown's `attw` option. Finally, run your module's tests and its playground. +[Checking the package](./3.develop-and-publish.md#checking-the-package) explains what attw reports, and tsdown's `attw` option. Finally, run your module's tests and its playground. ## Build messages @@ -382,12 +385,7 @@ The build empties the output directory, and `tsdown --watch` removes the files t ### Stub builds -`nuxt-module-build build --stub` and `nuxt-module-build prepare --stub` are removed. - -- With pnpm or Yarn 4, point `package.json` at your sources, as in [step 6](#step-6-choose-how-the-playground-loads-the-module), and remove the stub build from your scripts. -- With npm, Yarn 3 or earlier, or bun, replace the stub build with `tsdown`, after `nuxt-module-build prepare`. Run `tsdown --watch` next to `nuxt dev playground`. - -[Step 7](#step-7-update-the-scripts) lists every change to the scripts. +`nuxt-module-build build --stub` and `nuxt-module-build prepare --stub` are removed. [Step 6](#step-6-choose-how-the-playground-loads-the-module) says how the playground loads your module instead, and [step 7](#step-7-update-the-scripts) what replaces the stub build in your scripts. With `package.json` pointing at `dist`, run `tsdown --watch` next to `nuxt dev playground`. ### Preparing `.nuxt` @@ -408,17 +406,13 @@ The build doesn't type check otherwise, so your own type check reports every oth ### Portability errors -An export whose inferred type refers to a package that your module can't import by name can't be declared. Give each export that the message lists an explicit type annotation. When the value comes from a function that your module imports, annotate it with that function's return type: +An export whose inferred type refers to a package that your module can't import by name can't be declared. Give each export that the message lists an explicit type annotation, such as the return type of the function that produces its value: ```ts -// before -export const handler = defineCachedHandler(...) - -// after export const handler: ReturnType = defineCachedHandler(...) ``` -When `prepare` reports this, as a warning, run `nuxt-module-build prepare` again after the fix. [Errors that fail the build](./5.typescript.md#errors-that-fail-the-build) has more examples. +For an export that is a function, annotate the function's return type instead. When `prepare` reports this, as a warning, run it again after the fix. [Errors that fail the build](./5.typescript.md#errors-that-fail-the-build) has more examples. ### Isolated declarations From 17bf9098900ff8e197b4742672595ab7ead3d0d1 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 15:38:25 +0000 Subject: [PATCH 06/19] docs: keep the upgrade guide under 5,000 words --- docs/upgrade-v2.md | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index cec2cff6..f0995679 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -13,13 +13,12 @@ v2 builds your module with [tsdown](https://tsdown.dev) instead of unbuild. The - `src/module.ts` and the files that it imports have to load with Node, so relative imports carry their extension. - Stub builds are removed. With pnpm or Yarn 4, `package.json` points at your sources while you develop. With other package managers, `tsdown --watch` rebuilds `dist`. - The output is ES modules only, with `.d.mts` declarations for the module entry and no `module.cjs`. -- `nuxt-module-build prepare` generates a tsconfig for each part of the module, and `vue-tsc -b` checks each part with the types it has in an app. ## Before you start - Use Node `^22.18.0 || ^24.11.0 || >=26.0.0`, locally and in CI. - Find out which package manager packs your module when you publish it: the one whose `pack` or `publish` command your release script or workflow runs. A release tool, such as release-it, uses npm unless its config or docs name another. Step 6 depends on it. -- The steps use `pnpm`. Use your module's own package manager instead. Run commands with its `exec`, such as `pnpm exec tsdown`, and scripts with its `run`, such as `pnpm run dev:prepare`. With Yarn 1, write `yarn add -D`, `yarn remove`, `yarn tsdown`, and `npx publint` for `pnpm add -D`, `pnpm remove`, `pnpm exec tsdown`, and `pnpm dlx publint`, and `yarn pack --filename my-module-1.0.0.tgz` for `pnpm pack`. +- The steps use `pnpm`. Use your module's own package manager instead, with its own `exec` and `run`. With Yarn 1, write `yarn add -D`, `yarn remove`, `yarn tsdown`, and `npx publint` for `pnpm add -D`, `pnpm remove`, `pnpm exec tsdown`, and `pnpm dlx publint`, and `yarn pack --filename my-module-1.0.0.tgz` for `pnpm pack`. - The steps write the builder's command as `nuxt-module-build`, and apply to its other names, such as `nuxt-build-module` and `nuxi module-build`. ## Steps @@ -34,7 +33,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` there instead. - 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 `@` 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 pnpm's `minimumReleaseAge` fails the install with `ERR_PNPM_NO_MATURE_MATCHING_VERSION`, add the `@` that the error names to `minimumReleaseAgeExclude` in `pnpm-workspace.yaml`. Then install again. - If Yarn's `npmMinimalAgeGate` fails the install with `YN0016`, add the package that the error names to `npmPreapprovedPackages` in `.yarnrc.yml`. Then install again. - If pnpm fails with `ERR_PNPM_CATALOG_VERSION_MISMATCH`, set the version of the package that it names in the catalog of `pnpm-workspace.yaml` to the one in the command. Then run `pnpm install`. @@ -175,7 +174,7 @@ Before you run `prepare`: - If module code imports files that a build hook generates, commit them, or generate them in `dev:prepare` before `nuxt-module-build prepare`. tsdown's hooks don't run for `prepare`. - If your module is in a monorepo and has no `tsconfig.json` of its own, create the `tsconfig.json` of step 5 now. Otherwise, `prepare` reads the repository root's tsconfig, which can extend a `.nuxt` that doesn't exist yet. -Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Node, it lists every problem with its file, line, and fix, and doesn't generate `.nuxt`. Fix them all, and run it again to generate `.nuxt`. +Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Node, it lists every problem with its file, line, and fix, and doesn't generate `.nuxt`. Fix them all, and run it again. ### Step 5: Switch to the module's own tsconfigs @@ -249,7 +248,7 @@ If you publish with pnpm or Yarn 4, point `exports`, `imports`, `main`, and `mod - Change each import in tests and the playground that reaches into `dist`, such as `../dist/runtime/providers/ipx`, to the same path under `src`, without an extension: `../src/runtime/providers/ipx`. Code that reads a built file's contents keeps reading `dist`, as [Code that reaches into `dist`](./3.develop-and-publish.md#code-that-reaches-into-dist) shows. - If the playground uses Nuxt 5 or sets `future.compatibilityVersion: 5`, its type check also needs extensions on the type-only relative imports that module code reaches. After step 8, run `vue-tsc -b --noEmit` in the playground, add the extension to each import that it reports with TS2835 or TS2834, and repeat until it reports none. -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 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. If the playground's `nuxt.config` lists the module by its package name, and `playground/node_modules` doesn't link the module directory yet, 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. @@ -269,29 +268,7 @@ In each script and CI workflow that runs the builder, change only the builder's - 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. Add a `prepack` if the module's `package.json` has none, as in a monorepo package that the root builds. A `prepack` that runs another script, such as `pnpm run build`, isn't checked: move that script's commands into `prepack`, and make the other script run `prepack`, as `"build": "pnpm run prepack"`. -For example, these v1 scripts also minify a file into `dist` and prepare a docs app: - -```json [package.json] -{ - "scripts": { - "prepack": "nuxt-module-build build && esbuild --minify src/script.js --outfile=dist/script.min.js", - "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground && nuxt prepare docs" - } -} -``` - -If `package.json` points at your sources, and nothing in `dev:prepare` loads `dist`, they become: - -```json [package.json] -{ - "scripts": { - "prepack": "tsdown && esbuild --minify src/script.js --outfile=dist/script.min.js", - "dev:prepare": "nuxt-module-build prepare && nuxt prepare playground && nuxt prepare docs" - } -} -``` - -Otherwise, `dev:prepare` builds `dist` after `prepare`: `nuxt-module-build prepare && tsdown && nuxt prepare playground && nuxt prepare docs`. +For example, `nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground` becomes `nuxt-module-build prepare && nuxt prepare playground` when `package.json` points at your sources and nothing in it loads `dist`, and `nuxt-module-build prepare && tsdown && nuxt prepare playground` otherwise. ### Step 8: Build @@ -305,7 +282,7 @@ Otherwise, `dev:prepare` builds `dist` after `prepare`: `nuxt-module-build prepa Run your module's type-check script, and fix what it reports. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) lists the errors that it can report. - The build doesn't type check, so run the script as a step of its own in CI, after `dev:prepare`. -- The build types aliases such as `#build/*` as `any`. Annotate each export whose type is inferred through one, as [Other type errors](./5.typescript.md#other-type-errors) shows. Otherwise, `dist` declares it as `any`. +- The build types aliases such as `#build/*` as `any`. Annotate each export whose type is inferred through one, as [Other type errors](./5.typescript.md#other-type-errors) shows. ### Step 10: Check the package @@ -365,7 +342,7 @@ The build also fails when another plugin changes one of these options, or the fi `builder()` takes no `hooks`. Use tsdown's own `hooks` in the config: `build:prepare` in place of `build:before`, and `build:done`. To add a file to `dist`, emit it from a plugin. See [Hooks](./4.configuration.md#hooks). -tsdown's hooks and plugins don't run for `nuxt-module-build prepare`, or for a playground that loads your sources. If module code imports generated files, commit them, or generate them in `dev:prepare` before `nuxt-module-build prepare`. +tsdown's hooks and plugins don't run for `nuxt-module-build prepare`, or for a playground that loads your sources. Commit the files they generate that module code imports, as in step 4. ### Bundled packages @@ -395,7 +372,7 @@ The `.nuxt` that the build prepares can leave out the declarations that the modu ### Declaration errors -The build generates declarations with TypeScript, using the tsconfig project that includes each file. It fails when a declaration would be missing or wrong: +The build fails when a declaration would be missing or wrong: - An export whose inferred type can't be named (TS2742, TS2883, or TS4023) needs a type annotation. See [Portability errors](#portability-errors). - If `.nuxt` is out of date, run `nuxt-module-build prepare` and build again. @@ -416,7 +393,7 @@ For an export that is a function, annotate the function's return type instead. W ### Isolated declarations -With `declarations.isolated: true`, or `isolatedDeclarations: true` in the tsconfig that includes `src/module.ts`, the build generates the module entry's declarations with oxc, which requires an explicit type on every export of module code. Give each export that the message lists a type, as [`isolatedDeclarations`](./5.typescript.md#isolateddeclarations) shows, or remove the option. Without it, the build generates these declarations with TypeScript. +With `declarations.isolated: true`, or `isolatedDeclarations: true` in the tsconfig that includes `src/module.ts`, oxc generates the module entry's declarations, which requires an explicit type on every export of module code. Give each export that the message lists a type, as [`isolatedDeclarations`](./5.typescript.md#isolateddeclarations) shows, or remove the option. ### Publishing source exports @@ -438,7 +415,7 @@ Remove `paths` from that tsconfig, whether it is `tsconfig.json` extending `./.n ### Tests and config files -Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no tsconfig in `references` includes, with the number of files in each directory. `vue-tsc -b` doesn't type check them. These are usually tests outside `test/nuxt`, fixtures, and config files in the module root. +Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no tsconfig in `references` includes, which `vue-tsc -b` doesn't type check, such as tests and config files. To type check them, reference a `tsconfig.test.json` that includes them, as in [Tests and config files](./5.typescript.md#tests-and-config-files). Otherwise, add them to the `exclude` of `tsconfig.json`. [Step 5](#step-5-switch-to-the-modules-own-tsconfigs) says which to choose when you upgrade. @@ -469,7 +446,7 @@ The build emits ES modules only, so `rollup.emitCJS: true` fails the build. Remo #### mkdist options -Runtime files are transpiled with oxc, one file at a time. The `esbuild`, `pattern`, and `format` options of an entry fail the build, so remove them. The build applies `ext` (`mjs` or `js`), and ignores `declaration` and `addRelativeDeclarationExtensions`. Postcss plugins from `mkdist:entry:options` aren't applied. See [Runtime files](./6.output.md#runtime-files) for how stylesheets are processed. +Runtime files are transpiled with oxc, one file at a time. The `esbuild`, `pattern`, and `format` options of an entry fail the build, so remove them. The build applies `ext` (`mjs` or `js`), and ignores `declaration` and `addRelativeDeclarationExtensions`. #### Other options From 139a1912c04303b68a59126cff24aa76f4308020 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 16:16:09 +0000 Subject: [PATCH 07/19] docs: describe the module code the app and server parts type check --- docs/1.project-structure.md | 2 +- docs/5.typescript.md | 15 ++++++++++----- docs/upgrade-v2.md | 34 +++++++++------------------------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/docs/1.project-structure.md b/docs/1.project-structure.md index 93c6dd2c..26b8b274 100644 --- a/docs/1.project-structure.md +++ b/docs/1.project-structure.md @@ -100,7 +100,7 @@ Write paths into the runtime directory without an extension, as in the `addPlugi The runtime directory holds the code that the module adds to the app: components, composables, plugins, server routes, stylesheets, and any other files. The module registers them from `setup()`, with `@nuxt/kit` utilities such as `addPlugin`, `addComponent`, `addImports`, and `addServerHandler`. -Put code for the app in `src/runtime/app/`, and code for the server in `src/runtime/server/`. In an app that installs the module, Nuxt type-checks `runtime/server/` in the server context, and the rest of `runtime/` in the app context. Your module's own `tsconfig.json` type-checks all of `src/runtime/` with one configuration. See [Augment Types](/docs/guide/modules/recipes-advanced#augment-types) in the Module Author Guide. +Put code for the app in `src/runtime/app/`, and code for the server in `src/runtime/server/`. In an app that installs the module, Nuxt type-checks `runtime/server/` in the server context, and the rest of `runtime/` in the app context. Your module's own `tsconfig.json` checks them in the same contexts, as described in [TypeScript](/docs/api/module-builder/typescript#the-modules-tsconfigjson). See [Augment Types](/docs/guide/modules/recipes-advanced#augment-types) in the Module Author Guide. The build transpiles each file on its own into `dist/runtime/`, and keeps its name and its imports. [Build Output](/docs/api/module-builder/output#runtime-files) describes how each kind of file is emitted. diff --git a/docs/5.typescript.md b/docs/5.typescript.md index be80c1d2..527651de 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -128,7 +128,11 @@ A fixture that is a Nuxt app relies on the `.nuxt` that Nuxt generates for it, w ### Errors from checking each part -Each part is type-checked on its own, with the files that it imports and the types that the part has in an app. These are the errors that this can report, with their fixes: +Each part is type-checked on its own, with the files that it imports and the types that the part has in an app. + +That includes module code that a runtime file imports other than through the module's own package name: by a relative import, such as `'../../routing'`, or through an alias that the module adds or `~`, such as `#my-module-types` for `src/types.ts` or `~/src/types`. A [directory entry](/docs/api/module-builder/configuration#directory-entries) is in the app part as a whole, so module code in that directory is too, such as a module that `src/module.ts` installs. These files are type-checked with that part's types, so a dependency on `#app` or on DOM globals fails in the server part. Keep such code out of what runtime files import and out of directory entries, or move the types that both use into a file that imports neither module code nor `#app`, such as `src/runtime/types.ts`. + +These are the errors that checking each part can report, with their fixes: - **Missing `types` in module code** (TS2304, TS2503, such as `Cannot find namespace 'YT'`). Runtime files that module code imports are type-checked with module code too. The root's `types` apply to module code, but a `types` list in `tsconfig.module.json` replaces them, so repeat them there, such as `"types": ["youtube", "google.maps"]`. - **An augmentation that only another file brings in** (TS2322, TS2339, TS2353). Module code that relies on a type augmentation that only a test or a config file loads needs it in its own tsconfig. For example, a Vite plugin that returns `test` from its `config` hook needs Vitest's augmentation of the Vite config: add `/// ` to that file. Or add `"vitest/config"` to a `types` list in `tsconfig.module.json`, which replaces the root's `types`, so repeat those in it too. @@ -138,16 +142,17 @@ Each part is type-checked on its own, with the files that it imports and the typ - **`default` refers to itself** (TS7022 on the default export, TS7023 on a function in it, such as `moduleDependencies`). The module's own `.nuxt/types/modules.d.ts` types its options key and its entry in `ModuleDependencies` from `typeof import('../../src/module').default`. A `moduleDependencies` function without a return type is inferred from that same export. Give it one: `moduleDependencies(nuxt): ModuleDependencies`, with `ModuleDependencies` from `@nuxt/schema`. - **An ambient module declared twice** (TS2554, TS2748, or a wrong type for an export). A `.d.ts` in the module and a type template that both declare the same ambient module, such as `declare module '#my-module/bindings'`, both apply to each part, and the two declarations merge. Keep one of them. - **Errors in code that relies on `.nuxt`'s declarations**. Each part includes `.nuxt/nuxt.d.ts` and the declarations it references, such as the module's runtime config, typed routes and type templates. Code that relies on them is checked against them. Fix what they report. +- **Types of module code out of date** (TS2353, such as `'x' does not exist in type 'ModuleOptions'`, on a type that a runtime file imports through the module's own package name). The app, server and shared parts read the declarations of module code that `prepare` last wrote. Run `nuxt-module-build prepare` again after you change module code's types, such as `ModuleOptions`. - **Module code that a runtime file imports by relative path** (TS2339 on `nuxt.options` or on a type that the module augments, TS7053, TS2307 for `#app`). A runtime file that imports from `src/module.ts` or another file of module code by relative path, such as `import type { ImageProvider } from '../../module'`, brings that file into the type check of its own part. A type file that imports module code, such as `import type { ModuleOptions } from '../module'`, does the same. The server and shared parts don't have the module's augmentations: neither those of `src/module.ts`, nor a `.d.ts` module augmentation such as `declare module './types'`, which goes to the app part and to module code only. So `nuxt.options.` and the augmented properties fail there, as `Property 'ipx' does not exist on type 'ModuleOptions'`, and so do the imports that module code makes. Import the type through the module's own package name instead, as `import type { ImageProvider } from 'my-module'`, or from a file that only declares types and imports no module code. -- **Vite's module types not found** (TS2307 for an import such as `./worker.ts?worker&url` or `./styles.css?inline`, TS2882 for `import './styles.css'`). No part has the types of `vite/client`. Add `vite` to the module's `devDependencies`, and a `.d.ts` in the module root, such as `env.d.ts`, with `/// `. It applies to every part, as described in [The generated tsconfigs](#the-generated-tsconfigs). +- **Vite's module types not found** (TS2307 for an import such as `./worker.ts?worker&url`, `some-package/dist/worker.mjs?worker&url` or `./styles.css?inline`, TS2882 for `import './styles.css'`). No part has the types of `vite/client`. Add `vite` to the module's `devDependencies`, and `"vite/client"` to the root's `types`, or a `.d.ts` in the module root, such as `env.d.ts`, with `/// `. Either one applies to every part, as [The generated tsconfigs](#the-generated-tsconfigs) describes. - **Errors in tests under `test/nuxt`** (TS2593 for `describe` and `it`, and other errors in tests). The app part includes `test/nuxt` and `tests/nuxt`, so these tests are checked with the app's types and without a test runner's globals. Import `describe` and `it` from `vitest`. Or add the directory to the root's `exclude`, such as `"exclude": ["dist", "node_modules", "playground", "test"]`, run `nuxt-module-build prepare` again, and type-check the tests as described in [Tests and config files](#tests-and-config-files). -- **Errors in a template that the module generates** (such as TS7006 in a `.ts` template). A template with a `.ts` file name that a runtime file imports, through an alias or `#build`, is type-checked in that file's part, with its options. Type the code that the template generates, such as the parameters of its functions. -- **Excessive stack depth** (TS2321). TypeScript stops comparing two deeply nested types once the comparison is too deep, and which comparisons get that far depends on the types in the program. Give the value an explicit type, so that its inferred type isn't compared. +- **Errors in a template that the module generates** (such as TS7006 in a `.ts` file in `.nuxt`, like `.nuxt/my-module/components.ts`). A template with a `.ts` file name that a runtime file imports, through an alias or `#build`, is type-checked in that file's part, with its options. The code in it is the module's own, and a `tsconfig.json` that extends `.nuxt/tsconfig.json` reports the same errors. Type the code that the template generates, such as the parameters of its functions. +- **Excessive stack depth** (TS2321, such as on a `$fetch` call with typed server routes). TypeScript stops comparing two deeply nested types once the comparison is too deep, and which comparisons get that far depends on all the types in the program. So one part can report it where the other parts, and a single program, don't. Give the call an explicit type, such as `$fetch(url, options)`, so that its inferred type isn't compared. - **An unused `@ts-expect-error`** (TS2578). The part that checks the file reports no error on the line after the directive. Remove the directive. If another check of the same file still reports the error, such as the playground's check with source exports, use `// @ts-ignore` on that line instead. ### Extending `.nuxt/tsconfig.json` -`prepare` also writes `.nuxt/tsconfig.json`, for a `tsconfig.json` that extends it. It type-checks the whole module root as one program, with the app's types, so it doesn't report the [errors from checking each part](#errors-from-checking-each-part). In it, the module's own package name resolves through `package.json`, rather than to declarations of module code. Set `erasableSyntaxOnly` in its own `compilerOptions`, and type-check it with `vue-tsc --noEmit`: +`prepare` also writes `.nuxt/tsconfig.json`, for a `tsconfig.json` that extends it. It type-checks the whole module root as one program, with the app's types, so it doesn't report most of the [errors from checking each part](#errors-from-checking-each-part). In it, the module's own package name resolves through `package.json`, rather than to declarations of module code. Set `erasableSyntaxOnly` in its own `compilerOptions`, and type-check it with `vue-tsc --noEmit`: ```json [tsconfig.json] { diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index f0995679..024bed96 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -77,21 +77,7 @@ If your module has a `build.config.ts`, or a `build` or `unbuild` field in `pack | `hooks['mkdist:*']`, `name`, `parallel` | nothing: drop them | | any other option | see [Unsupported build options](#unsupported-build-options) | -For example, this `build.config.ts`: - -```ts [build.config.ts] -import { defineBuildConfig } from 'unbuild' - -export default defineBuildConfig({ - entries: ['./src/utils'], - externals: ['shiki'], - hooks: { - 'build:before': ctx => generateTypes(ctx.options.rootDir), - }, -}) -``` - -becomes this `tsdown.config.ts`: +A `build.config.ts` with `entries`, `externals`, and a `build:before` hook that generates types becomes: ```ts [tsdown.config.ts] import { builder } from '@nuxt/module-builder/tsdown' @@ -114,7 +100,6 @@ export default defineConfig({ - The build never bundles the packages in `dependencies`, `peerDependencies`, and `optionalDependencies`, their subpaths, Node builtins, `@nuxt/kit`, `@nuxt/schema`, `nuxt`, `vue`, `#app`, `#build/*`, and `#imports`. Drop the externals that are among these, and those that no bundled entry imports by value. The build never bundles a type-only import. Keep the rest in `builder({ externals })`. - The build fails when it bundles any other package from `node_modules`, `devDependencies` included. See [Bundled packages](#bundled-packages). - `src/module` is always built: leave it out of `entries`. Drop `addRelativeDeclarationExtensions` from directory entries. With no externals left, leave out `externals`. -- Relative imports in `tsdown.config.ts` carry their extension, because `prepare` loads the file with Node. Then delete `build.config.ts`, and remove the `build` or `unbuild` field. With a `tsdown.config.ts`, the build fails while either of them is still there. @@ -207,7 +192,7 @@ Then run `pnpm exec nuxt-module-build prepare`. Its `NUXT_MB_C0303` note lists t - If the old check covered none of the listed paths, or there was no old check, add them all to the `exclude` of `tsconfig.json`. - Otherwise, add a [`tsconfig.test.json`](./5.typescript.md#tests-and-config-files) to `references`. Its `include` lists `./.nuxt/nuxt.d.ts`, `./.nuxt/nuxt.node.d.ts`, and each covered path. Its `exclude` lists `./test/nuxt` and `./test/fixtures` if a covered path contains them, and the old `exclude` entries inside the covered paths. Its `files` lists each setup file that registers matchers or globals for the tests and that this `exclude` leaves out, such as `./test/nuxt/setup.ts`. Add the paths that the old check didn't cover to the `exclude` of `tsconfig.json`. -Run `prepare` again. Then change the type check: +Run `prepare` again, and again after each later change to the module's types, such as to `ModuleOptions`: until then, the app, server and shared parts read the options that the last `prepare` wrote, and the type check reports errors such as TS2353, `'x' does not exist in type 'ModuleOptions'`. Then change the type check: - If `vue-tsc` isn't in `devDependencies`, run `pnpm add -D --ignore-workspace-root-check vue-tsc`. - Replace the old type check with `vue-tsc -b --noEmit`, and keep the rest of its script: `vitest run && vue-tsc --noEmit && cd playground && vue-tsc --noEmit` becomes `vitest run && vue-tsc -b --noEmit && cd playground && vue-tsc --noEmit`. @@ -268,8 +253,6 @@ In each script and CI workflow that runs the builder, change only the builder's - 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. Add a `prepack` if the module's `package.json` has none, as in a monorepo package that the root builds. A `prepack` that runs another script, such as `pnpm run build`, isn't checked: move that script's commands into `prepack`, and make the other script run `prepack`, as `"build": "pnpm run prepack"`. -For example, `nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground` becomes `nuxt-module-build prepare && nuxt prepare playground` when `package.json` points at your sources and nothing in it loads `dist`, and `nuxt-module-build prepare && tsdown && nuxt prepare playground` otherwise. - ### Step 8: Build 1. Run `pnpm run dev:prepare`. @@ -282,6 +265,7 @@ For example, `nuxt-module-build build --stub && nuxt-module-build prepare && nux Run your module's type-check script, and fix what it reports. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) lists the errors that it can report. - The build doesn't type check, so run the script as a step of its own in CI, after `dev:prepare`. +- A runtime file that imports module code by a relative path or an alias, such as `~/src/types`, brings it into that part's type check, with that part's types. [Errors from checking each part](./5.typescript.md#errors-from-checking-each-part) shows the fix. - The build types aliases such as `#build/*` as `any`. Annotate each export whose type is inferred through one, as [Other type errors](./5.typescript.md#other-type-errors) shows. ### Step 10: Check the package @@ -342,7 +326,7 @@ The build also fails when another plugin changes one of these options, or the fi `builder()` takes no `hooks`. Use tsdown's own `hooks` in the config: `build:prepare` in place of `build:before`, and `build:done`. To add a file to `dist`, emit it from a plugin. See [Hooks](./4.configuration.md#hooks). -tsdown's hooks and plugins don't run for `nuxt-module-build prepare`, or for a playground that loads your sources. Commit the files they generate that module code imports, as in step 4. +They don't run for `nuxt-module-build prepare` either: commit the files they generate that module code imports, as in step 4. ### Bundled packages @@ -362,13 +346,13 @@ The build empties the output directory, and `tsdown --watch` removes the files t ### Stub builds -`nuxt-module-build build --stub` and `nuxt-module-build prepare --stub` are removed. [Step 6](#step-6-choose-how-the-playground-loads-the-module) says how the playground loads your module instead, and [step 7](#step-7-update-the-scripts) what replaces the stub build in your scripts. With `package.json` pointing at `dist`, run `tsdown --watch` next to `nuxt dev playground`. +`nuxt-module-build build --stub` and `nuxt-module-build prepare --stub` are removed. [Step 7](#step-7-update-the-scripts) replaces them in your scripts. With `package.json` pointing at `dist`, run `tsdown --watch` next to `nuxt dev playground`. ### Preparing `.nuxt` The build generates declarations with the tsconfigs in `.nuxt`, so it runs `nuxt-module-build prepare` first when `.nuxt` is missing. It does the same when the tsconfigs in `.nuxt` map a package into a directory that no longer exists, as after a dependency update. Run `nuxt-module-build prepare` first in your `dev:prepare` script instead, as in [step 7](#step-7-update-the-scripts), and again after you update dependencies. -The `.nuxt` that the build prepares can leave out the declarations that the module's own type check reads. Run `nuxt-module-build prepare` yourself before `vue-tsc -b --noEmit`. +The `.nuxt` that the build prepares can leave out the declarations that the module's own type check reads, so run `prepare` yourself before `vue-tsc -b --noEmit`. ### Declaration errors @@ -376,7 +360,7 @@ The build fails when a declaration would be missing or wrong: - An export whose inferred type can't be named (TS2742, TS2883, or TS4023) needs a type annotation. See [Portability errors](#portability-errors). - If `.nuxt` is out of date, run `nuxt-module-build prepare` and build again. -- A component without a `generic` script setup is declared with the types of `vue`, so the build fails when `vue` doesn't resolve from it. Add `vue` to `devDependencies`, or make the tsconfig that includes the component extend or reference the tsconfigs in `.nuxt`, which map `vue` in `paths`. +- A component without a `generic` script setup is declared with the types of `vue`, so the build fails when `vue` doesn't resolve from it. Add `vue` to `devDependencies`, or make the tsconfig that includes the component extend or reference the tsconfigs in `.nuxt`, as [Declarations](./5.typescript.md#declarations) shows. - Syntax errors, and files for which no declaration can be emitted, fail the build too. The build doesn't type check otherwise, so your own type check reports every other type error. See [Declarations](./5.typescript.md#declarations). @@ -405,7 +389,7 @@ See [Checks on published paths](./3.develop-and-publish.md#checks-on-published-p ### Other tsconfig layouts -These messages appear when the module doesn't have the tsconfigs of step 5. Either its tsconfig extends another app's `.nuxt/tsconfig.json`, such as a monorepo root's, or the build reads the nearest tsconfig above the module. Give the module a `tsconfig.json` in its own directory, as in [step 5](#step-5-switch-to-the-modules-own-tsconfigs). If one of the module's own tsconfigs can't be read, fix it, or generate the file it names before you run `prepare`. See [Monorepos](./5.typescript.md#monorepos). +These messages appear when the module doesn't have the tsconfigs of step 5. Give the module a `tsconfig.json` in its own directory, as in [step 5](#step-5-switch-to-the-modules-own-tsconfigs). If one of the module's own tsconfigs can't be read, fix it, or generate the file it names before you run `prepare`. See [Monorepos](./5.typescript.md#monorepos). ### tsconfig `paths` @@ -415,7 +399,7 @@ Remove `paths` from that tsconfig, whether it is `tsconfig.json` extending `./.n ### Tests and config files -Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no tsconfig in `references` includes, which `vue-tsc -b` doesn't type check, such as tests and config files. +Outside CI, `nuxt-module-build prepare` lists the TypeScript files that no tsconfig in `references` includes, such as tests and config files. To type check them, reference a `tsconfig.test.json` that includes them, as in [Tests and config files](./5.typescript.md#tests-and-config-files). Otherwise, add them to the `exclude` of `tsconfig.json`. [Step 5](#step-5-switch-to-the-modules-own-tsconfigs) says which to choose when you upgrade. From 50070127e200920ced3ed232ada6fa4c11f0410f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 16:16:09 +0000 Subject: [PATCH 08/19] test: cover module code in the parts that check it --- test/builder.spec.ts | 21 +++++++++++++++++++++ test/cli.spec.ts | 1 + 2 files changed, 22 insertions(+) diff --git a/test/builder.spec.ts b/test/builder.spec.ts index f432a42d..abc29260 100644 --- a/test/builder.spec.ts +++ b/test/builder.spec.ts @@ -229,6 +229,12 @@ export default { }, }) dirs.serverImports = await serverImports('server-imports', { 'tsconfig.json': REFERENCES('./.nuxt/tsconfig.node.json') }) + dirs.aliasModuleCode = await serverImports('alias-module-code', { + 'tsconfig.json': REFERENCES('./.nuxt/tsconfig.node.json'), + 'src/types.ts': `import type { NuxtApp } from '#app'\n\nexport type Payload = NuxtApp['payload']\n`, + 'src/runtime/composables/payload.ts': `import type { Payload } from '~/src/types'\n\nexport const payloadFromApp = (value: Payload): Payload => value\n`, + 'src/runtime/server/api/alias.get.ts': `import type { Payload } from '~/src/types'\n\nexport const payload = (value: Payload): Payload => value\n`, + }) dirs.erasableRoot = await serverImports('erasable-root', { 'tsconfig.json': JSON.stringify({ ...JSON.parse(REFERENCES('./.nuxt/tsconfig.node.json')), compilerOptions: { erasableSyntaxOnly: false } }) }) dirs.tsconfigOption = await fixture('.temp-builder/tsconfig-option', { config: `export default { tsconfig: 'tsconfig.override.json', plugins: [builder()] }\n`, module: source => source + FIELDS, files: moduleProject }) dirs.appProject = await fixture('.temp-builder/app-project', { @@ -308,6 +314,10 @@ export default { await writeFile(join(dirs.serverImports!, 'tsconfig.json'), REFERENCES('./tsconfig.module.json')) }) .then(() => step('serverImports:own', run(bin('vue-tsc'), ['-b', '--noEmit', '--force'], { cwd: dirs.serverImports }))), + step('aliasModuleCode:prepare', cli(['prepare', dirs.aliasModuleCode!])) + .then(() => step('aliasModuleCode:typecheck', run(bin('vue-tsc'), ['-b', '--noEmit', '--force'], { cwd: dirs.aliasModuleCode! }))) + .then(() => step('aliasModuleCode:app', run(bin('vue-tsc'), ['-p', '.nuxt/tsconfig.app.json', '--noEmit'], { cwd: dirs.aliasModuleCode! }))) + .then(() => step('aliasModuleCode:appFiles', run(bin('vue-tsc'), ['-p', '.nuxt/tsconfig.app.json', '--noEmit', '--listFilesOnly'], { cwd: dirs.aliasModuleCode! }))), step('erasableRoot:prepare', cli(['prepare', dirs.erasableRoot!])) .then(() => step('erasableRoot:typecheck', run(bin('vue-tsc'), ['-b', '--noEmit', '--force'], { cwd: dirs.erasableRoot }))), step('isolatedAsDocumented:prepare', cli(['prepare', dirs.isolatedAsDocumented])) @@ -506,6 +516,17 @@ describe('with a root tsconfig that only lists references', () => { expect((await options(dirs.erasableRoot!, 'node')).erasableSyntaxOnly).toBe(false) }) + it('should type check module code that a runtime file imports through an alias with the types of that file\'s part', () => { + const dir = dirs.aliasModuleCode! + expectBuilt('aliasModuleCode:prepare') + expect(results['aliasModuleCode:typecheck']!.output.trim().split('\n')).toEqual([ + expect.stringMatching(/^src\/standalone\.ts\(1,13\): error TS1294:/), + expect.stringMatching(/^src\/types\.ts\(1,30\): error TS2307: Cannot find module '#app'/), + ]) + expect(expectBuilt('aliasModuleCode:app')).toBe('') + expect(expectBuilt('aliasModuleCode:appFiles')).toContain(join(dir, 'src/types.ts')) + }) + it('should leave the server auto-imports out of the module project, and keep the types of server routes', () => { const files = expectBuilt('serverImports:files') expect(files).not.toContain('src/runtime/server/utils/counter.ts') diff --git a/test/cli.spec.ts b/test/cli.spec.ts index 161acc93..36bde7c9 100644 --- a/test/cli.spec.ts +++ b/test/cli.spec.ts @@ -1374,6 +1374,7 @@ describe('tsdown configs', () => { expect(expectBuilt(`${shape}:prepare`)).not.toContain('ERROR') expect(exists(dirs[shape]!, '.nuxt/tsconfig.json')).toBe(true) expect(exists(dirs[shape]!, '.nuxt/module-builder/types.d.mts')).toBe(true) + expect(JSON.parse(await read(dirs[shape]!, '.nuxt/tsconfig.app.json')).include).toContain('../src/shared') expectBuilt(shape) const out = join(dirs[shape]!, outDir) expect(await read(out, 'shared/value.mjs')).toContain(`from "./other.mjs"`) From 167710d97d4dc2d75d885162f937ce79b4769e75 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:05:02 +0000 Subject: [PATCH 09/19] docs: list the node10 problems of a module that supports node10 --- docs/3.develop-and-publish.md | 2 +- docs/upgrade-v2.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index ac53c857..512a2016 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -252,7 +252,7 @@ To check the package before you publish it, follow these steps: jq -r '.analysis.problems[] | select(.kind == "InternalResolutionError" and .resolutionOption != "node10") | "\(.moduleSpecifier) \(.fileName)"' attw.json | sort -u | cut -d' ' -f1 | uniq -c ``` - Each one has to be an alias that only a Nuxt app provides, such as `#app`, `#imports`, `#build/my-module/options`, or an alias that the module adds to the app. The command leaves out `node10`, which the `esm-only` profile doesn't check either. Under `node10`, a module without `typesVersions` also can't resolve its own `imports` and subpaths, such as `my-module/utils` in `dist/runtime/types.d.ts`. A module that supports `node10` consumers imports those files by relative path in the code that the declarations come from. Fix any other, such as a relative import of a file that the package doesn't contain. An app's `nuxt.config.ts` context reads the module's options type without the app's aliases, and types an alias that doesn't resolve as `any`, so move a type that the options type takes from such a file into one that doesn't import the aliases. + Each one has to be an alias that only a Nuxt app provides, such as `#app`, `#imports`, `#build/my-module/options`, or an alias that the module adds to the app. The command leaves out `node10`, which the `esm-only` profile doesn't check either. Under `node10`, a module without `typesVersions` also can't resolve its own `imports` and subpaths, such as `my-module/utils` in `dist/runtime/types.d.ts`. A module that supports `node10` consumers imports those files by relative path in the code that the declarations come from. To list the `node10` problems of such a module, run the command with `== "node10"` in place of `!= "node10"`: an import of the module's own `imports` entry, such as `#internal`, appears only there. Fix any other, such as a relative import of a file that the package doesn't contain. An app's `nuxt.config.ts` context reads the module's options type without the app's aliases, and types an alias that doesn't resolve as `any`, so move a type that the options type takes from such a file into one that doesn't import the aliases. 4. Run Are the Types Wrong? with those imports ignored: diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 024bed96..902f73b0 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -287,7 +287,7 @@ Run your module's type-check script, and fix what it reports. [Errors from check pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` -5. If you kept `types` and `typesVersions` in step 3, check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error` and no `--profile`. +5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error` and no `--profile`. 6. Fix any path that publint or attw reports. 7. In a fresh Nuxt app, install the tarball: From 4d1b7a867d6c0a4fb21d23c04913f48894c9818d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:05:37 +0000 Subject: [PATCH 10/19] docs: run the attw script through a runner --- docs/3.develop-and-publish.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index 512a2016..4f77244f 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -273,11 +273,13 @@ For a module that is correct, Are the Types Wrong? reports three kinds of proble ```json [package.json] { "scripts": { - "test:attw": "pnpm pack --out node_modules/.cache/attw.tgz && attw node_modules/.cache/attw.tgz --profile esm-only --ignore-rules internal-resolution-error" + "test:attw": "pnpm pack --out node_modules/.cache/attw.tgz && pnpm dlx @arethetypeswrong/cli node_modules/.cache/attw.tgz --profile esm-only --ignore-rules internal-resolution-error" } } ``` +With `@arethetypeswrong/cli` in `devDependencies`, write `attw` in place of `pnpm dlx @arethetypeswrong/cli`. + tsdown's own `attw` option packs the module with its package manager and checks the tarball after each build. It needs `@arethetypeswrong/core` in the module's `devDependencies`: ```bash [Terminal] From e868f5d381adcce1e37626baad7690d093b313ff Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:06:03 +0000 Subject: [PATCH 11/19] docs: correct how TypeScript resolves an exports target --- docs/7.exports.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/7.exports.md b/docs/7.exports.md index ab4b6680..2d1219c4 100644 --- a/docs/7.exports.md +++ b/docs/7.exports.md @@ -185,7 +185,7 @@ A subpath points at its source when only Nuxt loads it: an app, through the modu Each path of `exports` mirrors its path in `publishConfig.exports`: -- **A wildcard subpath with an extension in its `dist` target** gets the source's extension: `"./runtime/*": "./dist/runtime/*.js"` becomes `"./src/runtime/*.ts"`, for its `types` condition too. TypeScript doesn't swap the extension of an `exports` target. +- **A wildcard subpath with an extension in its `dist` target** gets the source's extension: `"./runtime/*": "./dist/runtime/*.js"` becomes `"./src/runtime/*.ts"`, for its `types` condition too. TypeScript substitutes the extension after it resolves the target, so a `*.js` target also finds the `.ts` files, but Node loads the target as written and finds no `.js` file there. - **A wildcard subpath without an extension in its `dist` target**, such as `"./dist/runtime/components/*"`, takes the import's own extension. It points at the same path under `src` when every file it reaches keeps its name in `dist`, such as `.vue` and `.css` files, for its `types` condition too. When it also reaches scripts, which `dist` names `.js`, such as `"./*": "./dist/*"` or a `"./runtime/*"` for components and composables, it stays at `dist`. - **A subpath written as a string** stays a string: `"./registry": "./dist/registry.mjs"` becomes `"./registry": "./src/registry.ts"`. - **A subpath with only a `types` condition** that points at a file outside `dist`, such as `./client.d.ts`, stays as it is, in both fields. From 925382315dcb315c15e9325eac81a24c5d1001c5 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:07:54 +0000 Subject: [PATCH 12/19] docs: keep the options that an extended tsconfig supplies --- docs/upgrade-v2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 902f73b0..5da0a352 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -163,7 +163,7 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod ### Step 5: Switch to the module's own tsconfigs -`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Remove its `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example, and add no `exclude` if it has none: +`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Before you remove its `extends`, copy into `compilerOptions` the options that the tsconfig it extends supplies and that module code needs, such as `types`: `prepare` copies the options of `tsconfig.json`, with those of the tsconfigs it extends, into every part. Then remove `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example, and add no `exclude` if it has none: ```json [tsconfig.json] { From a23b50d7f72f03b700bad4407e1de8480c04e28e Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:45:49 +0000 Subject: [PATCH 13/19] docs: say that the parts do not get `typeRoots` --- docs/5.typescript.md | 2 +- docs/upgrade-v2.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/5.typescript.md b/docs/5.typescript.md index 527651de..0350fba6 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -60,7 +60,7 @@ To change the options of one part, reference a tsconfig of your own that extends `prepare` adapts the tsconfigs that Nuxt generates to a module: - The app part covers `src/runtime/` and the module's other runtime directories, rather than the whole module root, less the root's `exclude`. -- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths` and `rootDir`. Its `types` are added to those that Nuxt sets. +- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688: move those declarations into a `.d.ts` file in `src`, which every part reads, or set `typeRoots` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. - The module's `.d.ts` files are added, apart from those in the runtime directories, in the output directory, in the root's `exclude`, and in the `playground*`, `docs`, `test`, `tests`, `fixtures`, `example` and `examples` directories. A `.d.ts` with no top-level `import` or `export`, which only declares globals and ambient modules, goes to all four parts. A module augmentation, with a top-level `import` or `export` and a `declare module` or `declare global`, goes to the app part and to module code. Other `.d.ts` modules apply only to the files that import them. - Module code gets the app's aliases, declarations and JSX options, for the runtime files that it imports. It also gets `.nuxt/module-builder/types.d.mts`, so the [type augmentations](/docs/api/module-builder/project-structure#srcmodulets) of `src/module.ts` apply to it. - With a root `tsconfig.json` that has `references`, module code gets `erasableSyntaxOnly: true`, unless the root's `compilerOptions` set it. diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 5da0a352..e5b9dfee 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -163,7 +163,7 @@ Then run `pnpm exec nuxt-module-build prepare`. Until module code loads with Nod ### Step 5: Switch to the module's own tsconfigs -`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Before you remove its `extends`, copy into `compilerOptions` the options that the tsconfig it extends supplies and that module code needs, such as `types`: `prepare` copies the options of `tsconfig.json`, with those of the tsconfigs it extends, into every part. Then remove `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example, and add no `exclude` if it has none: +`prepare` generates a tsconfig in `.nuxt` for each part of the module. Change `tsconfig.json` to reference them, as follows. Before you remove its `extends`, copy into `compilerOptions` the options that the tsconfig it extends supplies and that module code needs, such as `types`. `prepare` copies the options of `tsconfig.json`, with those of the tsconfigs it extends, into every part, apart from path options such as `paths` and `typeRoots`, which [The generated tsconfigs](./5.typescript.md#the-generated-tsconfigs) covers. Then remove `extends` and `include`. Keep its `compilerOptions` and `exclude` as they are, even where they differ from this example, and add no `exclude` if it has none: ```json [tsconfig.json] { From 9b6c88e2021eafe0230b68a9579c9bf4903bf9de Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:45:56 +0000 Subject: [PATCH 14/19] docs: keep an attw profile out of the node10 check --- docs/3.develop-and-publish.md | 2 +- docs/upgrade-v2.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index 4f77244f..70d09ad9 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -260,7 +260,7 @@ To check the package before you publish it, follow these steps: npx @arethetypeswrong/cli ./my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` - The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error` and no `--profile`. + The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets `profile`: every profile but `strict` marks the `node10` resolutions ignored and leaves them out of the exit code. For a module that is correct, Are the Types Wrong? reports three kinds of problem, which these steps leave out: diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index e5b9dfee..4ea2d7d5 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -287,7 +287,7 @@ Run your module's type-check script, and fix what it reports. [Errors from check pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` -5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error` and no `--profile`. +5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets one. 6. Fix any path that publint or attw reports. 7. In a fresh Nuxt app, install the tarball: From c7cc7581e5b5876d88e69e9de09b1a82ccb7a771 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:55:32 +0000 Subject: [PATCH 15/19] docs: keep attw ignore rules out of the node10 check --- docs/3.develop-and-publish.md | 2 +- docs/upgrade-v2.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index 70d09ad9..d47c2f56 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -260,7 +260,7 @@ To check the package before you publish it, follow these steps: npx @arethetypeswrong/cli ./my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` - The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets `profile`: every profile but `strict` marks the `node10` resolutions ignored and leaves them out of the exit code. + The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets `profile` or `ignoreRules`: every profile but `strict` marks the `node10` resolutions ignored, and a config's `ignoreRules` are added to the command's, so either one hides a `node10` failure from both the table and the exit code. For a module that is correct, Are the Types Wrong? reports three kinds of problem, which these steps leave out: diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 4ea2d7d5..67fb6fff 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -287,7 +287,7 @@ Run your module's type-check script, and fix what it reports. [Errors from check pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` -5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets one. +5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets a profile or `ignoreRules`. 6. Fix any path that publint or attw reports. 7. In a fresh Nuxt app, install the tarball: From 75a0016bcd2853c4d74ec8251d6ef64fbecf21b7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:55:39 +0000 Subject: [PATCH 16/19] docs: say that the node10 check needs its own row --- docs/3.develop-and-publish.md | 2 +- docs/upgrade-v2.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/3.develop-and-publish.md b/docs/3.develop-and-publish.md index d47c2f56..5824a89a 100644 --- a/docs/3.develop-and-publish.md +++ b/docs/3.develop-and-publish.md @@ -260,7 +260,7 @@ To check the package before you publish it, follow these steps: npx @arethetypeswrong/cli ./my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` - The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets `profile` or `ignoreRules`: every profile but `strict` marks the `node10` resolutions ignored, and a config's `ignoreRules` are added to the command's, so either one hides a `node10` failure from both the table and the exit code. + The `esm-only` profile leaves out `node10` resolution, and `require` under `node16`, which can't load an ES module. A module that sets `types` and `typesVersions` for `node10` consumers checks `node10` too, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets `profile` or `ignoreRules`: every profile but `strict` marks the `node10` resolutions ignored, and a config's `ignoreRules` are added to the command's, so either one hides a `node10` failure from both the table and the exit code. Read the `node10` row too: with the types and the imports inside an `import` condition, `require` resolves nothing, so the `node16 (from CJS)` row reports `NoResolution`, and the only rule that hides it, `no-resolution`, hides `node10` as well. For a module that is correct, Are the Types Wrong? reports three kinds of problem, which these steps leave out: diff --git a/docs/upgrade-v2.md b/docs/upgrade-v2.md index 67fb6fff..0766a3b6 100644 --- a/docs/upgrade-v2.md +++ b/docs/upgrade-v2.md @@ -287,7 +287,7 @@ Run your module's type-check script, and fix what it reports. [Errors from check pnpm dlx @arethetypeswrong/cli my-module-1.0.0.tgz --profile esm-only --ignore-rules internal-resolution-error ``` -5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets a profile or `ignoreRules`. +5. If you kept `types` and `typesVersions` in step 3, list the problems that item 3 leaves out under `node10`, as [Checking the package](./3.develop-and-publish.md#checking-the-package) shows, then check `node10` resolution as well, with `--ignore-rules cjs-resolves-to-esm internal-resolution-error`, no `--profile`, and no `.attw.json` that sets a profile or `ignoreRules`. Read its `node10` row: the `node16 (from CJS)` row can report `NoResolution` of its own, when the types sit inside an `import` condition that `require` can't reach, so the exit code doesn't settle `node10`. 6. Fix any path that publint or attw reports. 7. In a fresh Nuxt app, install the tarball: From 5ad3acca6649dc64c7c5f1cb215b1ccd5456421a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 17:55:59 +0000 Subject: [PATCH 17/19] docs: drop a types entry along with its declarations --- docs/5.typescript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/5.typescript.md b/docs/5.typescript.md index 0350fba6..cbaff55e 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -60,7 +60,7 @@ To change the options of one part, reference a tsconfig of your own that extends `prepare` adapts the tsconfigs that Nuxt generates to a module: - The app part covers `src/runtime/` and the module's other runtime directories, rather than the whole module root, less the root's `exclude`. -- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688: move those declarations into a `.d.ts` file in `src`, which every part reads, or set `typeRoots` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. +- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688. Move those declarations into a `.d.ts` file in `src`, which every part reads, and drop the entry from `types`. Or set `typeRoots` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. - The module's `.d.ts` files are added, apart from those in the runtime directories, in the output directory, in the root's `exclude`, and in the `playground*`, `docs`, `test`, `tests`, `fixtures`, `example` and `examples` directories. A `.d.ts` with no top-level `import` or `export`, which only declares globals and ambient modules, goes to all four parts. A module augmentation, with a top-level `import` or `export` and a `declare module` or `declare global`, goes to the app part and to module code. Other `.d.ts` modules apply only to the files that import them. - Module code gets the app's aliases, declarations and JSX options, for the runtime files that it imports. It also gets `.nuxt/module-builder/types.d.mts`, so the [type augmentations](/docs/api/module-builder/project-structure#srcmodulets) of `src/module.ts` apply to it. - With a root `tsconfig.json` that has `references`, module code gets `erasableSyntaxOnly: true`, unless the root's `compilerOptions` set it. From 03c7d96bbe753f7f7214c35626cf721d1645562b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 18:14:57 +0000 Subject: [PATCH 18/19] docs: say which parts read a declaration file in src --- docs/5.typescript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/5.typescript.md b/docs/5.typescript.md index cbaff55e..b5c99ea9 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -60,7 +60,7 @@ To change the options of one part, reference a tsconfig of your own that extends `prepare` adapts the tsconfigs that Nuxt generates to a module: - The app part covers `src/runtime/` and the module's other runtime directories, rather than the whole module root, less the root's `exclude`. -- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688. Move those declarations into a `.d.ts` file in `src`, which every part reads, and drop the entry from `types`. Or set `typeRoots` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. +- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688. Move those declarations into a `.d.ts` file in `src` and drop the entry from `types`. Such a file reaches all four parts only if it has no top-level `import` or `export`: one that declares its globals inside `declare global` and ends with `export {}` is a module augmentation, which reaches the app part and module code only, and a server file that uses it reports TS2304. Write such a file as a script, with top-level `declare` statements, or keep the package and set `typeRoots` and `types` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. - The module's `.d.ts` files are added, apart from those in the runtime directories, in the output directory, in the root's `exclude`, and in the `playground*`, `docs`, `test`, `tests`, `fixtures`, `example` and `examples` directories. A `.d.ts` with no top-level `import` or `export`, which only declares globals and ambient modules, goes to all four parts. A module augmentation, with a top-level `import` or `export` and a `declare module` or `declare global`, goes to the app part and to module code. Other `.d.ts` modules apply only to the files that import them. - Module code gets the app's aliases, declarations and JSX options, for the runtime files that it imports. It also gets `.nuxt/module-builder/types.d.mts`, so the [type augmentations](/docs/api/module-builder/project-structure#srcmodulets) of `src/module.ts` apply to it. - With a root `tsconfig.json` that has `references`, module code gets `erasableSyntaxOnly: true`, unless the root's `compilerOptions` set it. From 4f2103ce838654bd043501ad2852187ac4ec002d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 29 Nov 2026 18:30:24 +0000 Subject: [PATCH 19/19] docs: keep module augmentations out of the script file --- docs/5.typescript.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/5.typescript.md b/docs/5.typescript.md index b5c99ea9..339dacfc 100644 --- a/docs/5.typescript.md +++ b/docs/5.typescript.md @@ -60,7 +60,7 @@ To change the options of one part, reference a tsconfig of your own that extends `prepare` adapts the tsconfigs that Nuxt generates to a module: - The app part covers `src/runtime/` and the module's other runtime directories, rather than the whole module root, less the root's `exclude`. -- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688. Move those declarations into a `.d.ts` file in `src` and drop the entry from `types`. Such a file reaches all four parts only if it has no top-level `import` or `export`: one that declares its globals inside `declare global` and ends with `export {}` is a module augmentation, which reaches the app part and module code only, and a server file that uses it reports TS2304. Write such a file as a script, with top-level `declare` statements, or keep the package and set `typeRoots` and `types` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. +- The root's `compilerOptions`, with those of the tsconfigs that it extends, are copied into all four parts, apart from path options such as `paths`, `rootDir` and `typeRoots`. Its `types` are added to those that Nuxt sets. A `types` entry that resolves through `typeRoots` is therefore missing in every part, with TS2688. Move those declarations into a `.d.ts` file in `src` and drop the entry from `types`. Such a file reaches all four parts only if it has no top-level `import` or `export`: one that declares its globals inside `declare global` and ends with `export {}` is a module augmentation, which reaches the app part and module code only, and a server file that uses it reports TS2304. Write such a file as a script, with top-level `declare` statements, but keep a `declare module 'vue'` in a file of its own with a top-level `import` or `export`: in a script, that declaration replaces the module rather than augmenting it, and `vue`'s exports fail with TS2305. Or keep the package and set `typeRoots` and `types` in a tsconfig of your own for each part that needs it, as [Options of one part](#options-of-one-part) shows. - The module's `.d.ts` files are added, apart from those in the runtime directories, in the output directory, in the root's `exclude`, and in the `playground*`, `docs`, `test`, `tests`, `fixtures`, `example` and `examples` directories. A `.d.ts` with no top-level `import` or `export`, which only declares globals and ambient modules, goes to all four parts. A module augmentation, with a top-level `import` or `export` and a `declare module` or `declare global`, goes to the app part and to module code. Other `.d.ts` modules apply only to the files that import them. - Module code gets the app's aliases, declarations and JSX options, for the runtime files that it imports. It also gets `.nuxt/module-builder/types.d.mts`, so the [type augmentations](/docs/api/module-builder/project-structure#srcmodulets) of `src/module.ts` apply to it. - With a root `tsconfig.json` that has `references`, module code gets `erasableSyntaxOnly: true`, unless the root's `compilerOptions` set it.