prepare
nuxt-prepare

Initialization steps at build time, like nuxtServerInit

Nuxt Prepare module

Nuxt Prepare

Turn API responses into typed imports when Nuxt builds.

Features

  • 🦦 Zero configuration
  • 🦎 Import build-time data from #nuxt-prepare, typed with its literal values
  • ✂️ Conditionally overwrite runtime config or app config values
  • 🍡 Run scripts in series or parallel
  • 🥢 Ready for Nitro server routes
  • 🦾 Typed handlers with defineNuxtPrepareHandler

Setup

npx nuxt module add prepare

Basic Usage

By default, Nuxt Prepare looks for a server.prepare.ts file in your project root. To run synchronous or asynchronous code when Nuxt builds your app, create it and default-export the result of defineNuxtPrepareHandler:

// `server.prepare.ts`
import { defineNuxtPrepareHandler } from 'nuxt-prepare/config'

export default defineNuxtPrepareHandler(async () => {
  // Fetch data from an API, read a file, query a database …

  return {
    // Import `foo` anywhere from `#nuxt-prepare`
    data: {
      foo: 'bar'
    },

    // Overwrite the runtime config variable `foo`
    runtimeConfig: {
      public: {
        foo: 'Overwritten by "server.prepare" script'
      }
    }
  }
})

The documentation covers the handler API, prepare data and every module option.

💻 Development

  1. Clone this repository
  2. Enable Corepack using corepack enable
  3. Install dependencies using pnpm install
  4. Run pnpm run dev:prepare
  5. Start development server using pnpm run dev

Credits

License

MIT License © 2023-PRESENT Johann Schopplich