Git Product home page Git Product logo

module-builder's Introduction

๐Ÿ“ฆ 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.

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 to dist/runtime/.

package.json:

A minimum package.json should look like this:

{
  "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"
  },
  "dependencies": {
    "@nuxt/kit": "npm:@nuxt/kit-edge@latest"
  },
  "devDependencies": {
    "@nuxt/module-builder": "latest"
  }
}

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 ๐Ÿ’š

module-builder's People

Contributors

renovate[bot] avatar danielroe avatar pi0 avatar bobbiegoede avatar dargmuesli avatar atinux avatar cpreston321 avatar userquin avatar adamdehaven avatar antfu avatar harlan-zw avatar lihbr avatar pionxzh avatar ricardogobbosouza avatar sadeghbarati avatar alphacastor avatar gehbt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.