Modules

prepare
prepare

Initialization steps at build time – like Nuxt 2 server init

nuxt-prepare

nuxt-prepare

npm version

Nuxt module to run initialization steps at build time.

Features

  • 🦦 Zero-Config
  • 🦎 Run sync or async operations when Nuxt builds your app
  • ✂️ Conditionally overwrite runtime config or app config variables
  • 🦾 Better DX with defineNuxtPrepareHandler

Setup

📖 Read the documentation

# pnpm
pnpm add -D nuxt-prepare

# npm
npm i -D nuxt-prepare

# yarn
yarn add -D nuxt-prepare

Basic Usage

📖 Read the documentation

Add the nuxt-prepare module to your nuxt.config.ts:

// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ['nuxt-prepare']
})

By default, nuxt-prepare will look for a server.prepare.ts file in your project root. To run synchronous or asynchronous code when Nuxt builds your app, define a handler in your project root and export a default function:

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

export default defineNuxtPrepareHandler(async () => {
  // Do some async magic here, e.g. fetch data from an API

  return {
    // Overwrite the runtime config variable `foo`
    runtimeConfig: {
      public: {
        foo: 'overwritten by prepare script'
      }
    },

    // Pass custom state to Nuxt and import it
    // anywhere from `#nuxt-prepare`
    state: {
      foo: 'bar',
    },
  }
})

!NOTE You can run as many prepare scripts as you want – add them to the prepareKit.scripts module configuration.

💻 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