Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nuxt/module-builder

nuxt124.5kMIT0.8.4TypeScript support: included

Complete solution for building Nuxt modules

readme

📦 Nuxt Module Builder

npm version License npm downloads Volta board

The complete solution to build and ship Nuxt modules.

Features

  • Compatible with Nuxt 3 and Nuxt Kit
  • Unified build with unjs/unbuild
  • Automated build config using last module spec
  • Typescript and ESM support
  • Auto generated CommonJS stubs
  • Auto generated types and shims for @nuxt/schema

We recommend to checkout the Nuxt modules author guide before starting with the module-builder.

Requirements

For a user to use a module generated from module-builder, it's recommended they have:

  • Node.js >= 16.x. Latest Node LTS preferred
  • Nuxt 3 or Nuxt Bridge. Nuxt 2 is functional but not advised

Quick start

Get started with our module starter:

npx nuxi init -t module my-module

Project structure

The module builder requires a special project structure. You can check out the module template.

src/module.ts

The entrypoint for module definition.

A default export using defineNuxtModule and ModuleOptions type export is expected.

You could also optionally export ModuleHooks or ModuleRuntimeHooks to annotate any custom hooks the module uses.

`ts [src/module.ts] import { defineNuxtModule } from '@nuxt/kit'

export interface ModuleOptions { apiKey: string }

export interface ModuleHooks { 'my-module:init': any }

export interface ModuleRuntimeHooks { 'my-module:runtime-hook': any }

export interface ModuleRuntimeConfig { PRIVATE_NAME: string }

export interface ModulePublicRuntimeConfig { NAME: string }

export default defineNuxtModule<ModuleOptions>({ meta: { name: 'my-module', configKey: 'myModule' }, defaults: { apiKey: 'test' }, async setup (moduleOptions, nuxt) { // Write module logic in setup function } })


### `src/runtime/`

Any runtime file and code that we need to provide by module including plugins, composables and server api, should be in this directory.

Each file will be transformed individually using [unjs/mkdist](https://github.com/unjs/mkdist) to `dist/runtime/`.

<!-- TODO: Docs about how to address runtime from within setup -->

### `package.json`:

A minimum `package.json` should look like this:

```json [package.json]
{
  "name": "my-module",
  "license": "MIT",
  "version": "1.0.0",
  "exports": {
    ".": {
      "types": "./dist/types.d.ts",
      "import": "./dist/module.mjs",
      "require": "./dist/module.cjs"
    }
  },
  "main": "./dist/module.cjs",
  "types": "./dist/types.d.ts",
  "files": [
    "dist"
  ],
  "scripts": {
    "prepack": "nuxt-module-build build"
  },
  "dependencies": {
    "@nuxt/kit": "npm:@nuxt/kit-edge@latest"
  },
  "devDependencies": {
    "@nuxt/module-builder": "latest"
  }
}

build.config.ts (optional)

Module builder is essentially a preset for unjs/unbuild, check out the build command for reference.

To customize/extend the unbuild configuration you can add a build.config.ts in the root of your project:

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  // set additional configuration or customize using hooks
})

Dist files

Module builder generates dist files in dist/ directory:

  • module.mjs: Module entrypoint build from src/module
  • module.json: Module meta extracted from module.mjs + package.json
  • module.cjs: ESM proxy to allow require module in CommonJS context
  • types.d.ts: Exported types in addition to shims for nuxt.config auto completion.
  • runtime/*: Individually transformed files using unjs/mkdist
    • Javascript and .ts files will be transformed to .js with extracted types on .d.ts file with same name
    • .vue files will be transformed with extracted .d.ts file
    • Other files will be copied as is

💻 Development

  • Clone repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Try building example module using pnpm example:build

License

MIT - Made with 💚

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v0.8.4

compare changes

🩹 Fixes

  • Remove nuxt/schema augment (2a20ed0)

❤️ Contributors

v0.8.3

compare changes

🩹 Fixes

  • build: Handle missing named type exports (#331)

❤️ Contributors

v0.8.2

compare changes

🔥 Performance

  • Removed unnecessary hasTypeExport check (#310)

🩹 Fixes

  • prepare: Override compatibilityDate (b9b27c3)

❤️ Contributors

v0.8.1

compare changes

🩹 Fixes

  • build: Export all types in stub mode (6b1970d)

🏡 Chore

  • Add @danielroe to code owners (c39cc75)

❤️ Contributors

v0.8.0

compare changes

🩹 Fixes

  • ⚠️ Do not augment nuxt options inside module entry (#295)
  • build: Do not export default as a type (d29337c)
  • build: Only generate import type statement if required (190bff4)

⚠️ Breaking Changes

  • ⚠️ Do not augment nuxt options inside module entry (#295)

❤️ Contributors

v0.7.1

compare changes

🩹 Fixes

  • build: Declare ModuleOptions in correct place (#283)

📖 Documentation

  • Update path for runtime js files (#282)

❤️ Contributors

v0.7.0

compare changes

🚀 Enhancements

  • Auto generate module options from schema meta (#33)

🩹 Fixes

  • Use tsconfck to resolve tsconfig compilerOptions (#274)
  • ⚠️ Use .js extension for files in runtime/ directory (dbd05bb)
  • Resolve full path to runtime externals (#275)
  • Include dist/runtime in externals list (0946c04)

📖 Documentation

  • Add types condition to export subpath (#265)

🏡 Chore

✅ Tests

  • Add additional test for validity of types shared from runtime (afc4374)

🤖 CI

  • Adds reproduction workflow (0dc73bb)

⚠️ Breaking Changes

  • ⚠️ Use .js extension for files in runtime/ directory (dbd05bb)

❤️ Contributors

v0.6.0

compare changes

🚀 Enhancements

  • Generate runtime/ dts based on nuxt tsconfig options (#255)
  • Add builder versions to module.json (f8567a3)
  • Support transforming jsx (4841f2e)

🩹 Fixes

  • ⚠️ Remove support for deprecated RuntimeModuleHooks interface (#228)
  • Add -nightly versions to externals (0a88a87)
  • Ignore exporting type if it is not defined (c308cc5)
  • Mark runtime/ directory as external (7a68e1e)

🏡 Chore

  • release: V0.5.5 (f158ffa)
  • Dedupe kit/schema/vue versions (aa0a710)
  • Add root dev:prepare command (c308a68)
  • Migrate to eslint v9 (#250)
  • Improve internal type safety and enable strict mode (78aa088)
  • Tweak tsconfig settings (404aae7)
  • Add more type annotations (ba0614b)

✅ Tests

  • Update type testing step (#256)
  • Add inline snapshots for runtime/ transforms (#257)
  • Update snapshot (a39c183)

🎨 Styles

⚠️ Breaking Changes

  • ⚠️ Remove support for deprecated RuntimeModuleHooks interface (#228)

❤️ Contributors

v0.5.5

compare changes

🩹 Fixes

  • Rename RuntimeModuleHooks to ModuleRuntimeHooks (#194)
  • Use import with extension in generated .d.mts file (#202)

📖 Documentation

  • Remove reference to ModulePrivateRuntimeConfig (34ee148)

🏡 Chore

  • Add shell emulator for Windows (#191)
  • Fix example package.json (#192)

✅ Tests

  • Add root + module type tests (#198)

🤖 CI

❤️ Contributors

v0.5.4

compare changes

🚀 Enhancements

  • Support writing RuntimeNuxtHooks types (#183)

🩹 Fixes

  • Generate explicit type import/exports (#184)

📖 Documentation

  • Import nuxt composables from #imports (#186)

❤️ Contributors

v0.5.3

compare changes

🩹 Fixes

  • Esnext-compatible output (#181)

❤️ Contributors

v0.5.2

compare changes

💅 Refactors

  • Migrate to citty (#169)

📖 Documentation

🏡 Chore

❤️ Contributors

v0.5.1

compare changes

🚀 Enhancements

  • Support --sourcemap option (#163)

❤️ Contributors

v0.5.0

compare changes

🚀 Enhancements

  • Expose prepareModule function (76b5654)
  • Upgrade to unbuild@2 (#161)

🏡 Chore

  • Add type safety to prepare overrides (9d0804a)
  • Switch to changelogen for release (ba811da)

❤️ Contributors

0.4.0 (2023-05-26)

Features

  • add prepare command for local types support (#124) (612c6b2)

0.3.1 (2023-05-01)

Bug Fixes

  • extend nuxt/schema as well (796d6ab)

0.3.0 (2023-04-11)

0.2.1 (2022-11-16)

0.2.0 (2022-10-15)

Features

  • export all module types in types.d.ts (#46) (64f3ba5)
  • support ModuleRuntimeConfig and ModulePublicRuntimeConfig type exports (#45) (a552495)
  • support outDir option (#37) (6453ed0)

Bug Fixes

  • pkg: export ./package.json subpath (#47) (82ab400)

0.1.7 (2022-01-18)

Bug Fixes

  • ensure nuxt packages are externalized (ebad137)

0.1.6 (2022-01-17)

Bug Fixes

  • typo meta.json => module.json (55863c1)

0.1.5 (2022-01-17)

Bug Fixes

  • add default export for types.d.ts (885c02f)

0.1.4 (2022-01-17)

Features

  • parse module.d exports to generate ModuleOptions conditionally (9cd8282)
  • support --stub (746d59b)
  • support ModuleHooks type export (269ce20)

0.1.3 (2022-01-17)

Features

  • generate module.json file (ac12805)

Bug Fixes

0.1.2 (2021-12-23)

Bug Fixes

0.1.1 (2021-12-23)

Features

  • poc for schema types generation (4991907)

Bug Fixes

  • emit cjs stub before validation (0154bc5)
  • improve types generation (68c02e6)