Bundler plugins

Add Rolldown plugins to your app's build, write one, and ship a NitroUI build of a library.

NitroUI bundles your app with Rolldown. You can add Rolldown plugins to every build: Debug and Release, the dev server on each platform, and nitro-ui test.

Add a plugin

List plugins under bundler.plugins in app.json, in the order they should run. Each entry is a path from the app folder or a package name. To pass options, use [module, options], where options is any JSON value.

app.json
{
  "bundler": {
    "plugins": ["./bundler/markdownFiles.ts", ["some-library/nitro-ui", { "verbose": true }]]
  }
}

Restart the dev server after you change the list or a plugin's code.

Write a plugin

A plugin module default-exports a function. NitroUI calls it once for each build and passes the build's context and the entry's options. The function returns Rolldown plugins.

bundler/markdownFiles.ts
import { readFileSync } from 'node:fs'
import type { BundlerPluginFactory } from 'nitro-ui-bundler'

const markdownFiles: BundlerPluginFactory = () => ({
  name: 'markdown-files',
  load(id) {
    if (!id.endsWith('.md')) return null
    this.addWatchFile(id)
    const text = readFileSync(id, 'utf8')
    return { code: `export default ${JSON.stringify(text)}`, moduleType: 'js' }
  },
})

export default markdownFiles
ContextValue
rootThe app folder.
platform'ios' or 'android'. undefined for nitro-ui bundle without --platform.
mode'development' or 'production'.

Use the hooks that Rollup and Vite also support, such as resolveId, load and transform. A load hook that turns another file type into JavaScript returns moduleType: 'js'.

To import the new file type in TypeScript, declare it in a .d.ts file in your app.

src/markdown.d.ts
declare module '*.md' {
  const text: string
  export default text
}

Update the app when a file changes

The dev server updates the app only for files it watches. Call this.addWatchFile with each file your plugin reads. Otherwise the app keeps the file's old contents until you restart the dev server. To pick up new files, watch their folder.

Ship a NitroUI build of a library

A library can include a separate JavaScript file for NitroUI apps. Point to it with a nitro-ui entry in the library's exports. When an app imports the library, NitroUI loads that file instead of the usual one.

List nitro-ui before import and default. NitroUI uses the first entry that matches, and import matches too.

package.json
{
  "exports": {
    ".": {
      "nitro-ui": "./dist/nitro-ui.js",
      "import": "./dist/index.js",
      "default": "./dist/index.js"
    }
  }
}

TypeScript in a NitroUI app also reads the nitro-ui entry, so it finds the library's types the same way.

On this page