@aurelia/storybook

中文文档
3.0.0发布于 2026/08/06MITTypeScriptES Module构建来源已验证近 30 天下载 5132

A Storybook plugin to render Aurelia 2 components using Vite, Webpack, or Rsbuild/Rspack

原始使用文档
文档目录16 节
  1. Compatibility
  2. What works
  3. Install
  4. Vite
  5. Webpack 5
  6. Rsbuild/Rspack
  7. Configure Storybook
  8. Write stories
  9. Custom templates and projected content
  10. Aurelia configuration
  11. Mount from a play function
  12. Docs and MDX
  13. Vitest browser tests
  14. Portable stories
  15. Development
  16. License

@aurelia/storybook

@aurelia/storybook runs Aurelia 2 components inside Storybook 10. It provides the renderer, builder presets, CSF types, bindable controls, docs support, portable stories, and test integration expected from a first-class Storybook framework.

The framework works with Vite, Webpack 5, and Rsbuild/Rspack. You set component in the story metadata and write normal Storybook stories; there is no renderer boilerplate in .storybook/preview.ts.

Compatibility

PartSupported version
Aurelia2.0.0-rc.2
Storybook10.5.x
Node.js^20.19.0 or >=22.12.0
Vite builderVite 7.x or 8.x and @storybook/builder-vite 10.5.x
Webpack builder@storybook/builder-webpack5 10.5.x
Rsbuild builderRsbuild 2.x and storybook-builder-rsbuild 3.4.x

Aurelia 2.0.0-rc.2 supports Vite 8's Oxc pipeline, including TC39 decorators. No extra Babel or SWC configuration is required.

What works

  • CSF 3 and typed CSF Factories with decorators, loaders, globals, tags, story extension, and run
  • automatic component rendering with bindables mapped to Storybook args and controls
  • actions and interaction tests through storybook/test
  • play-function mount
  • autodocs, MDX, bindable arg types, component descriptions, and dynamic Aurelia source markup
  • story-level and global Aurelia resources, DI registrations, containers, and setup hooks
  • live arg updates without restarting the Aurelia app; structural changes remount cleanly
  • portable stories through composeStory, composeStories, and setProjectAnnotations
  • Vite, Webpack 5, and Rsbuild/Rspack development and static builds
  • Storybook's Vitest addon running real Chromium tests

Install

Start with the command for your builder. An existing Aurelia app will usually have the Aurelia builder plugin or loader already.

Vite

npm install --save-dev @aurelia/storybook storybook @storybook/builder-vite @aurelia/vite-plugin

Vite 7 and Vite 8 are supported. With Vite 8, use @aurelia/vite-plugin 2.0.0-rc.2 or newer.

Webpack 5

npm install --save-dev @aurelia/storybook storybook @storybook/builder-webpack5 @aurelia/webpack-loader ts-loader

Rsbuild/Rspack

npm install --save-dev @aurelia/storybook storybook storybook-builder-rsbuild @rsbuild/core @aurelia/webpack-loader

Add @storybook/addon-docs, @storybook/addon-a11y, and other addons as needed.

Configure Storybook

For Vite, create .storybook/main.ts:

import { defineMain } from '@aurelia/storybook/node';

export default defineMain({
  stories: ['../src/**/*.@(mdx|stories.@(ts|js))'],
  addons: ['@storybook/addon-docs', '@storybook/addon-a11y'],
  framework: {
    name: '@aurelia/storybook',
    options: {},
  },
  core: {
    builder: '@storybook/builder-vite',
  },
});

The framework preset adds the Aurelia Vite plugin when the Storybook config does not already have it. It also keeps Aurelia's runtime packages out of Vite dependency pre-bundling so the renderer and components use one runtime instance.

Webpack uses the same file with a different builder:

core: {
  builder: '@storybook/builder-webpack5',
},

The preset adds ts-loader and @aurelia/webpack-loader rules once, preserving any rules already in the config.

For Rsbuild, use:

core: {
  builder: 'storybook-builder-rsbuild',
},

The Rsbuild preset adds the matching Aurelia Rspack rules.

Your .storybook/preview.ts carries project settings and addon types:

import { definePreview } from '@aurelia/storybook';
import addonA11y from '@storybook/addon-a11y';
import addonDocs from '@storybook/addon-docs';

export default definePreview({
  addons: [addonA11y(), addonDocs()],
  tags: ['autodocs'],
  parameters: {
    controls: {
      matchers: {
        color: /(background|color)$/i,
        date: /Date$/i,
      },
    },
  },
});

Do not export render or renderToCanvas from the preview file. Storybook loads them from the framework preset.

The factory form above enables Storybook's typed preview.meta and meta.story APIs. A regular object using satisfies Preview remains supported for CSF 3 projects.

Add the usual scripts:

{
  "scripts": {
    "storybook": "storybook dev -p 6006",
    "build-storybook": "storybook build"
  }
}

Write stories

Storybook's CSF Factories API infers the Aurelia component instance, addon parameters, decorators, and story args:

import preview from '../../.storybook/preview';
import { expect, fn, userEvent } from 'storybook/test';
import { HelloWorld } from '../hello-world';

const meta = preview.meta({
  title: 'Example/HelloWorld',
  component: HelloWorld,
  args: {
    message: 'Hello from Storybook',
    onIncrement: fn(),
  },
});

export const Default = meta.story({
  play: async ({ args, canvas }) => {
    await userEvent.click(canvas.getByRole('button', { name: 'Increment' }));
    await expect(args.onIncrement).toHaveBeenCalledWith(1);
  },
});

export const Renamed = Default.extend({
  args: { message: 'Extended story' },
});

Factory stories also expose Storybook's composed metadata, run, extend, and test APIs. CSF 3 remains available when a project prefers object exports.

Automatic rendering covers the common case:

import type { Meta, StoryObj } from '@aurelia/storybook';
import { fn, userEvent, within } from 'storybook/test';
import { HelloWorld } from '../hello-world';

const meta = {
  title: 'Example/HelloWorld',
  component: HelloWorld,
  args: {
    message: 'Hello from Storybook',
    onIncrement: fn(),
  },
  argTypes: {
    message: { control: 'text' },
    onIncrement: { action: 'increment' },
  },
} satisfies Meta<typeof HelloWorld>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default = {} satisfies Story;

export const Interaction = {
  play: async ({ canvasElement }) => {
    const canvas = within(canvasElement);
    await userEvent.click(canvas.getByRole('button', { name: 'Increment' }));
  },
} satisfies Story;

The generated Aurelia template binds only the args that are present. Omitted bindables keep the component's own default values. Adding or removing a bindable arg remounts the generated template; changing an existing value updates the running view model.

Custom templates and projected content

Use defineAureliaStory when the component needs specific markup, local resources, or projected content:

import {
  defineAureliaStory,
  type Meta,
  type StoryObj,
} from '@aurelia/storybook';
import { CardHeading } from '../card-heading';
import { ProductCard } from '../product-card';

const meta = {
  component: ProductCard,
} satisfies Meta<typeof ProductCard>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Detailed = {
  render: (args) =>
    defineAureliaStory({
      template: `
        <product-card product.bind="product">
          <card-heading>Featured</card-heading>
        </product-card>
      `,
      props: args,
      register: [ProductCard, CardHeading],
    }),
  args: {
    product: { name: 'Desk lamp' },
  },
} satisfies Story;

A story result can contain:

FieldPurpose
templateAurelia markup for the story
Componentcomponent override for this story
propsvalues merged after Storybook args
innerHtmlprojected markup for an automatically generated component element
register or componentscustom elements, attributes, value converters, plugins, and other registries
itemsDI registrations and services
containeran existing Aurelia container

The render function may also return a template string or an Aurelia custom-element class directly.

Aurelia configuration

Register resources for a preview, component group, or individual story with parameters.aurelia:

import { Registration } from 'aurelia';
import type { Preview } from '@aurelia/storybook';
import { FeatureFlags } from '../src/feature-flags';
import { MoneyValueConverter } from '../src/money-value-converter';

const preview = {
  parameters: {
    aurelia: {
      register: [MoneyValueConverter],
      configureContainer: (container) => {
        container.register(
          Registration.instance(FeatureFlags, { newCheckout: true })
        );
      },
      configure: (aurelia, context) => {
        // Configure the app before it starts.
      },
    },
  },
} satisfies Preview;

export default preview;

For package-wide setup, use setup from .storybook/preview.ts:

import { setup } from '@aurelia/storybook';

setup(async (aurelia, context) => {
  // Runs for every newly mounted story app.
});

setup returns an unregister function, which is useful in test environments.

Mount from a play function

Destructure mount when a play function controls the initial render:

import { expect } from 'storybook/test';

export const MountedByPlay = {
  play: async ({ mount }) => {
    const canvas = await mount({
      Component: HelloWorld,
      props: { message: 'Mounted from play' },
    });

    await expect(canvas.getByText('Mounted from play')).toBeVisible();
  },
};

Storybook detects the destructured parameter and waits for this mount before running post-story checks.

Docs and MDX

Add @storybook/addon-docs to enable autodocs and MDX. Aurelia bindables appear under a bindables category, and dynamic source blocks show the Aurelia markup used by the story. Set parameters.docs.source.code when a story needs a hand-written source example.

Regular Storybook MDX works without an Aurelia-specific wrapper:

import { Meta, Canvas } from '@storybook/addon-docs/blocks';
import * as Stories from './hello-world.stories';

<Meta of={Stories} />

# Hello World

<Canvas of={Stories.Default} />

Vitest browser tests

Storybook's Vitest addon can run the same stories in Chromium. Extend the app's Vite config so the test project receives exactly one Aurelia plugin instance:

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { storybookTest } from '@storybook/addon-vitest/vitest-plugin';
import { playwright } from '@vitest/browser-playwright';
import { defineConfig } from 'vitest/config';

const dirname = path.dirname(fileURLToPath(import.meta.url));

export default defineConfig({
  test: {
    projects: [
      {
        extends: './vite.config.ts',
        plugins: [
          storybookTest({ configDir: path.join(dirname, '.storybook') }),
        ],
        test: {
          name: 'storybook',
          browser: {
            enabled: true,
            headless: true,
            provider: playwright({}),
            instances: [{ browser: 'chromium' }],
          },
        },
      },
    ],
  },
});

Do not add another aurelia() call to this project when the extended Vite config already has it. Transforming Aurelia convention templates twice turns the first generated module into template text.

With Vite 8 and a factory preview, pre-bundle the addon entry points imported by that preview. This prevents Vite from reloading the first browser-test run while it discovers them:

optimizeDeps: {
  include: [
    '@storybook/addon-a11y',
    '@storybook/addon-docs',
    '@storybook/addon-vitest',
    'storybook/internal/csf',
  ],
},

Portable stories

The package exports Aurelia-aware portable-story helpers:

import { composeStories } from '@aurelia/storybook/portable-stories';
import * as stories from './hello-world.stories';

const { Default } = composeStories(stories);

await Default.run();

When .storybook/preview.ts exports a CSF 3 configuration object, use setProjectAnnotations once in a test setup file so portable stories receive it:

import { setProjectAnnotations } from '@aurelia/storybook/portable-stories';
import preview from '../.storybook/preview';

setProjectAnnotations(preview);

CSF Factory stories already carry their preview annotations. Import one and call await Story.run() directly instead of composing it again.

Development

This repository is an npm workspace containing the framework and Vite, Webpack, and Rsbuild examples.

npm ci
npm run sync:versions:check
npm run lint
npm run typecheck
npm test
npm run build
npm run lint:examples
npm run typecheck:examples
npm run test:examples
npm run build:examples
npm run build:storybooks
npm run test:storybook

npm run test:storybook requires a Chromium binary. Install it with npx playwright install chromium if Playwright has not already done so.

License

MIT

版本与发布记录

@aurelia/storybook 共发布 10 个版本,首次发布于 2025/02/05;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/08/063 个月前
2.3.02026/06/025 个月前
2.2.12026/01/239 个月前
2.1.02026/01/189 个月前
2.0.02025/11/1711 个月前
1.0.22025/06/281.4 年前

关于 @aurelia/storybook 的常见问题

@aurelia/storybook 是什么?

A Storybook plugin to render Aurelia 2 components using Vite, Webpack, or Rsbuild/Rspack

如何安装 @aurelia/storybook?

在项目目录运行 npm install @aurelia/storybook,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:aurelia(^2.0.0-rc.2)、storybook(^10.5.6)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aurelia/storybook 支持 TypeScript 吗?

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

@aurelia/storybook 支持 ESM 还是 CommonJS?

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

@aurelia/storybook 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aurelia/storybook 可以商用吗?

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

@aurelia/storybook 的下载量有多少?

根据 npm 官方下载统计,@aurelia/storybook 在 2026/10/07 – 2026/11/05 这 30 天内被下载 5,132 次,日均约 171 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aurelia/storybook 还在维护吗?

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