@akfm/vite-plugin-storybook-mock-server-functions

中文文档
0.0.0发布于 2026/07/24MITTypeScriptES Module构建来源已验证近 30 天下载 1.2万

Vite plugin that replaces Next.js Server Actions ("use server") with Storybook fn() mocks.

原始使用文档
文档目录11 节
  1. Features
  2. Supported patterns
  3. Installation
  4. Setup
  5. 1. Register the plugin
  6. 2. Register the decorator
  7. Injecting mock implementations from a story
  8. Options
  9. Example
  10. Known limitations
  11. License

@akfm/vite-plugin-storybook-mock-server-functions

npm version license

English | 日本語

A Vite plugin that lets Next.js App Router Server Actions ("use server") run standalone inside Storybook.

Next.js Server Actions rely on Next.js's own bundler transform and server runtime at execution time, so loading them as-is in Storybook (which is Vite-based) results in build or runtime errors. This plugin detects functions carrying a "use server" directive and replaces them with storybook/test's fn()-based mock functions, so components that use Server Actions can be rendered and tested in Storybook without a real server.

Features

  • Detects both module-level and function-level "use server" directives — no need to restructure existing action files.
  • Replaces only the directive-bearing functions with fn() mocks; everything else in the file (including non-action exports) is left untouched.
  • Lets each story inject its own mock implementation via parameters.serverFunctions, with mocks automatically reset between stories.
  • Ships as a single Vite plugin + one decorator — no extra Storybook addon to install.

Supported patterns

  • Module-level directive: "use server" at the top of a file. Every exported function in the file is treated as a Server Action.
  • Function-level (inline) directive: "use server" at the top of an individual function body, with no module-level directive required. This works on a standalone function declaration/expression just as well as on a property/method of an object or a field/method of a class — so a single directive-bearing function among otherwise-plain exports (e.g. only api.create is a Server Action, while api.formatLabel stays a regular function) is detected correctly either way.
// actions/todo.ts (module-level — every export in the file is a Server Action)
"use server";
export async function createTodo(formData: FormData) {
  /* real server-side work */
}

// actions/delete-todo.ts (function-level, no module-level directive — only this
// function is a Server Action)
export async function deleteTodo(id: string) {
  "use server";
  /* real server-side work */
}

// actions/api.ts (function-level, only part of the object is a Server Action)
export const api = {
  create: async (formData: FormData) => {
    "use server";
    /* real server-side work */
  },
  formatLabel: (todo: Todo) => todo.title.toUpperCase(), // regular function, left untouched
};

Either way, at Storybook build time these functions are replaced with mock functions created via storybook/test's fn().

Installation

npm install -D @akfm/vite-plugin-storybook-mock-server-functions
pnpm add -D @akfm/vite-plugin-storybook-mock-server-functions
yarn add -D @akfm/vite-plugin-storybook-mock-server-functions

Requires vite and storybook as peer dependencies.

Setup

1. Register the plugin

Add the plugin to .storybook/main.ts's viteFinal.

import { storybookMockServerFunctions } from "@akfm/vite-plugin-storybook-mock-server-functions";

const config = {
  framework: "@storybook/nextjs-vite",
  // ...
  viteFinal: async (viteConfig) => {
    viteConfig.plugins ??= [];
    viteConfig.plugins.push(storybookMockServerFunctions());
    return viteConfig;
  },
};

export default config;

2. Register the decorator

Add the decorator to .storybook/preview.ts. This resets all mocks before every story runs.

import { withServerFunctionMocks } from "@akfm/vite-plugin-storybook-mock-server-functions/runtime";

const preview = {
  decorators: [withServerFunctionMocks],
};

export default preview;

Injecting mock implementations from a story

Pass a Map<mock function, implementation function> via parameters.serverFunctions to inject any return value or behavior on a per-story basis. Since Vite's module graph is a singleton, the functions a story imports are the exact mock functions the plugin generated, so they can be used directly as Map keys.

import { createTodo } from "../actions/todo";
import { api } from "../actions/api";

export const Success: Story = {
  parameters: {
    serverFunctions: new Map([
      [createTodo, async () => ({ ok: true, id: "1" })],
      [api.create, async () => ({ ok: true })],
    ]),
  },
};

If parameters.serverFunctions is not set, each mock defaults to async () => undefined.

Non-Server-Action functions like api.formatLabel are left untransformed and can be imported and used with their original implementation.

Options

storybookMockServerFunctions({
  include?: FilterPattern; // default: [/\.[jt]sx?$/]
  exclude?: FilterPattern; // default: [/node_modules/]
})

include / exclude are passed straight through to @rollup/pluginutils's createFilter.

Example

example/ contains a verification Storybook app built with @storybook/nextjs-vite.

pnpm install
pnpm build
pnpm --filter example storybook

Known limitations

  • Re-exports of the form export { x } from './y' are not supported.
  • The function-level directive is only recognized on functions with a BlockStatement body (a single-expression arrow function like () => "use server" can't hold a directive in its body, matching Next.js's own constraint).
  • parameters.serverFunctions is designed around passing a Map of functions directly. If you rely on story serialization (e.g. Storybook's composed stories feature), be aware that functions cannot be serialized.
  • oxc-parser depends on a platform-specific native (napi) binary. This is normally resolved automatically via npm/pnpm optionalDependencies for your OS/architecture, but it may not work in unsupported runtime environments.

License

MIT

版本与发布记录

@akfm/vite-plugin-storybook-mock-server-functions 共发布 2 个版本,首次发布于 2026/07/24;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.0latest2026/07/243 个月前
0.0.0-beta02026/07/243 个月前

关于 @akfm/vite-plugin-storybook-mock-server-functions 的常见问题

@akfm/vite-plugin-storybook-mock-server-functions 是什么?

Vite plugin that replaces Next.js Server Actions ("use server") with Storybook fn() mocks.

如何安装 @akfm/vite-plugin-storybook-mock-server-functions?

在项目目录运行 npm install @akfm/vite-plugin-storybook-mock-server-functions,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:storybook(^9.0.0 || ^10.0.0)、vite(^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@akfm/vite-plugin-storybook-mock-server-functions 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@akfm/vite-plugin-storybook-mock-server-functions 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@akfm/vite-plugin-storybook-mock-server-functions 可以商用吗?

@akfm/vite-plugin-storybook-mock-server-functions 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@akfm/vite-plugin-storybook-mock-server-functions 的下载量有多少?

根据 npm 官方下载统计,@akfm/vite-plugin-storybook-mock-server-functions 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,146 次,日均约 405 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@akfm/vite-plugin-storybook-mock-server-functions 还在维护吗?

最新版本 0.0.0 发布于 2026/07/24(3 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/07/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。